PureScript by Example
PureScript by Example
Functional Programming for the Web
About the Book
PureScript is a small strongly, statically typed programming language with expressive types, written in and inspired by Haskell, and compiling to Javascript.
Functional programming in JavaScript has seen quite a lot of popularity recently, but large-scale application development is hindered by the lack of a disciplined environment in which to write code. PureScript aims to solve that problem by bringing the power of strongly-typed functional programming to the world of JavaScript development.
This book will show you how to get started with the PureScript programming language, from the basics (setting up a development environment) to the advanced.
Each chapter will be motivated by a particular problem, and in the course of solving that problem, new functional programming tools and techniques will be introduced. Here are some examples of problems that will be solved in this book:
- Transforming data structures with maps and folds
- Form field validation using applicative functors
- Testing code with QuickCheck
- Using the canvas
- Domain specific language implementation
- Working with the DOM
- JavaScript interoperability
- Dealing with callback hell
Translations
Table of Contents
-
1. Introduction
- 1.1 Functional JavaScript
- 1.2 Types and Type Inference
- 1.3 Polyglot Web Programming
- 1.4 Prerequisites
- 1.5 About You
- 1.6 How to Read This Book
- 1.7 Getting Help
- 1.8 About the Author
- 1.9 Acknowledgements
-
2. Getting Started
- 2.1 Chapter Goals
- 2.2 Introduction
- 2.3 Installing PureScript
- 2.4 Installing Tools
- 2.5 Hello, PureScript!
- 2.6 Compiling for the Browser
- 2.7 Removing Unused Code
- 2.8 Compiling CommonJS Modules
- 2.9 Tracking Dependencies with Bower
- 2.10 Computing Diagonals
- 2.11 Testing Code Using the Interactive Mode
- 2.12 Conclusion
-
3. Functions and Records
- 3.1 Chapter Goals
- 3.2 Project Setup
- 3.3 Simple Types
- 3.4 Quantified Types
- 3.5 Notes On Indentation
- 3.6 Defining Our Types
- 3.7 Type Constructors and Kinds
- 3.8 Displaying Address Book Entries
- 3.9 Test Early, Test Often
- 3.10 Creating Address Books
- 3.11 Curried Functions
- 3.12 Querying the Address Book
- 3.13 Infix Function Application
- 3.14 Function Composition
- 3.15 Tests, Tests, Tests …
- 3.16 Conclusion
-
4. Recursion, Maps And Folds
- 4.1 Chapter Goals
- 4.2 Project Setup
- 4.3 Introduction
- 4.4 Recursion on Arrays
- 4.5 Maps
- 4.6 Infix Operators
- 4.7 Filtering Arrays
- 4.8 Flattening Arrays
- 4.9 Array Comprehensions
- 4.10 Do Notation
- 4.11 Guards
- 4.12 Folds
- 4.13 Tail Recursion
- 4.14 Accumulators
- 4.15 Prefer Folds to Explicit Recursion
- 4.16 A Virtual Filesystem
- 4.17 Listing All Files
- 4.18 Conclusion
-
5. Pattern Matching
- 5.1 Chapter Goals
- 5.2 Project Setup
- 5.3 Simple Pattern Matching
- 5.4 Simple Patterns
- 5.5 Guards
- 5.6 Array Patterns
- 5.7 Record Patterns and Row Polymorphism
- 5.8 Nested Patterns
- 5.9 Named Patterns
- 5.10 Case Expressions
- 5.11 Pattern Match Failures and Partial Functions
- 5.12 Algebraic Data Types
- 5.13 Using ADTs
- 5.14 Record Puns
- 5.15 Newtypes
- 5.16 A Library for Vector Graphics
- 5.17 Computing Bounding Rectangles
- 5.18 Conclusion
-
6. Type Classes
- 6.1 Chapter Goals
- 6.2 Project Setup
- 6.3 Show Me!
- 6.4 Common Type Classes
- 6.5 Type Annotations
- 6.6 Overlapping Instances
- 6.7 Instance Dependencies
- 6.8 Multi Parameter Type Classes
- 6.9 Functional Dependencies
- 6.10 Nullary Type Classes
- 6.11 Superclasses
- 6.12 A Type Class for Hashes
- 6.13 Conclusion
-
7. Applicative Validation
- 7.1 Chapter Goals
- 7.2 Project Setup
- 7.3 Generalizing Function Application
- 7.4 Lifting Arbitrary Functions
- 7.5 The Applicative Type Class
- 7.6 Intuition for Applicative
- 7.7 More Effects
- 7.8 Combining Effects
- 7.9 Applicative Validation
- 7.10 Regular Expression Validators
- 7.11 Traversable Functors
- 7.12 Applicative Functors for Parallelism
- 7.13 Conclusion
-
8. The Eff Monad
- 8.1 Chapter Goals
- 8.2 Project Setup
- 8.3 Monads and Do Notation
- 8.4 The Monad Type Class
- 8.5 Monad Laws
- 8.6 Folding With Monads
- 8.7 Monads and Applicatives
- 8.8 Native Effects
- 8.9 Side-Effects and Purity
- 8.10 The Eff Monad
- 8.11 Extensible Effects
- 8.12 Interleaving Effects
- 8.13 The Kind of Eff
- 8.14 Records And Rows
- 8.15 Fine-Grained Effects
- 8.16 Handlers and Actions
- 8.17 Mutable State
- 8.18 DOM Effects
- 8.19 An Address Book User Interface
- 8.20 Conclusion
-
9. Canvas Graphics
- 9.1 Chapter Goals
- 9.2 Project Setup
- 9.3 Simple Shapes
- 9.4 Putting Row Polymorphism to Work
- 9.5 Drawing Random Circles
- 9.6 Transformations
- 9.7 Preserving the Context
- 9.8 Global Mutable State
- 9.9 L-Systems
- 9.10 Conclusion
-
10. The Foreign Function Interface
- 10.1 Chapter Goals
- 10.2 Project Setup
- 10.3 A Disclaimer
- 10.4 Calling PureScript from JavaScript
- 10.5 Understanding Name Generation
- 10.6 Runtime Data Representation
- 10.7 Representing ADTs
- 10.8 Representing Quantified Types
- 10.9 Representing Constrained Types
- 10.10 Using JavaScript Code From PureScript
- 10.11 Wrapping JavaScript Values
- 10.12 Defining Foreign Types
- 10.13 Functions of Multiple Arguments
- 10.14 Representing Side Effects
- 10.15 Defining New Effects
- 10.16 Working With Untyped Data
- 10.17 Handling Null and Undefined Values
- 10.18 Generic JSON Serialization
- 10.19 Conclusion
-
11. Monadic Adventures
- 11.1 Chapter Goals
- 11.2 Project Setup
- 11.3 How To Play The Game
- 11.4 The State Monad
- 11.5 The Reader Monad
- 11.6 The Writer Monad
- 11.7 Monad Transformers
- 11.8 The ExceptT Monad Transformer
- 11.9 Monad Transformer Stacks
- 11.10 Type Classes to the Rescue!
- 11.11 Alternatives
- 11.12 Monad Comprehensions
- 11.13 Backtracking
- 11.14 The RWS Monad
- 11.15 Implementing Game Logic
- 11.16 Running the Computation
- 11.17 Handling Command Line Options
- 11.18 Conclusion
-
12. Callback Hell
- 12.1 Chapter Goals
- 12.2 Project Setup
- 12.3 The Problem
- 12.4 The Continuation Monad
- 12.5 Putting ExceptT To Work
- 12.6 A HTTP Client
- 12.7 Parallel Computations
- 12.8 Conclusion
-
13. Generative Testing
- 13.1 Chapter Goals
- 13.2 Project Setup
- 13.3 Writing Properties
- 13.4 Improving Error Messages
- 13.5 Testing Polymorphic Code
- 13.6 Generating Arbitrary Data
- 13.7 Testing Higher-Order Functions
- 13.8 Writing Coarbitrary Instances
- 13.9 Testing Without Side-Effects
- 13.10 Conclusion
-
14. Domain-Specific Languages
- 14.1 Chapter Goals
- 14.2 Project Setup
- 14.3 A HTML Data Type
- 14.4 Smart Constructors
- 14.5 Phantom Types
- 14.6 The Free Monad
- 14.7 Interpreting the Monad
- 14.8 Extending the Language
- 14.9 Conclusion
Causes Supported
Code Club
https://www.codeclub.org.ukA nationwide network of volunteer-led after school coding clubs for children aged 9-11.
The Leanpub 60 Day 100% Happiness Guarantee
Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.
Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.
You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!
So, there's no reason not to click the Add to Cart button, is there?
See full terms...
Earn $8 on a $10 Purchase, and $16 on a $20 Purchase
We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.
(Yes, some authors have already earned much more than that on Leanpub.)
In fact, authors have earnedover $13 millionwriting, publishing and selling on Leanpub.
Learn more about writing on Leanpub
Free Updates. DRM Free.
If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).
Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.
Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.
Learn more about Leanpub's ebook formats and where to read them