Leanpub Header

Skip to main content

PureScript by Example

Functional Programming for the Web

Learn functional programming for the web by solving practical problems using the PureScript programming language. "PureScript By Example" will motivate functional programming techniques like maps, folds, type classes and monads by applying them to real-world problems such as form validation, AJAX callbacks and drawing with the canvas. 100% of the author royalties on purchases of this book are donated to Code Club, a nationwide network of volunteer-led after school coding clubs.
Free With Membership

With Membership

Free!

$9.99

You pay

$9.99

Cause gets

$7.99
$

...Or Buy With Credits!

You can get credits monthly with a Reader Membership
PDF
EPUB
WEB
8,679
Readers
235
Pages
About

About

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

Share this book

License

Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License

Author

About the Author

Phil Freeman

I am a developer living in Los Angeles, CA. I work principally in Scala, Haskell and TypeScript.

I am the original developer of the PureScript programming language and compiler, and I am currently working on the PureScript by Example book.

Translations

Translations

Languages

Contents

Table of Contents

1.Introduction

  1. 1.1Functional JavaScript
  2. 1.2Types and Type Inference
  3. 1.3Polyglot Web Programming
  4. 1.4Prerequisites
  5. 1.5About You
  6. 1.6How to Read This Book
  7. 1.7Getting Help
  8. 1.8About the Author
  9. 1.9Acknowledgements

2.Getting Started

  1. 2.1Chapter Goals
  2. 2.2Introduction
  3. 2.3Installing PureScript
  4. 2.4Installing Tools
  5. 2.5Hello, PureScript!
  6. 2.6Compiling for the Browser
  7. 2.7Removing Unused Code
  8. 2.8Compiling CommonJS Modules
  9. 2.9Tracking Dependencies with Bower
  10. 2.10Computing Diagonals
  11. 2.11Testing Code Using the Interactive Mode
  12. Exercises
  13. 2.12Conclusion

3.Functions and Records

  1. 3.1Chapter Goals
  2. 3.2Project Setup
  3. 3.3Simple Types
  4. 3.4Quantified Types
  5. 3.5Notes On Indentation
  6. 3.6Defining Our Types
  7. 3.7Type Constructors and Kinds
  8. 3.8Displaying Address Book Entries
  9. 3.9Test Early, Test Often
  10. 3.10Creating Address Books
  11. 3.11Curried Functions
  12. 3.12Querying the Address Book
  13. 3.13Infix Function Application
  14. 3.14Function Composition
  15. 3.15Tests, Tests, Tests …
  16. Exercises
  17. 3.16Conclusion

4.Recursion, Maps And Folds

  1. 4.1Chapter Goals
  2. 4.2Project Setup
  3. 4.3Introduction
  4. 4.4Recursion on Arrays
  5. Exercises
  6. 4.5Maps
  7. 4.6Infix Operators
  8. 4.7Filtering Arrays
  9. Exercises
  10. 4.8Flattening Arrays
  11. 4.9Array Comprehensions
  12. 4.10Do Notation
  13. 4.11Guards
  14. Exercises
  15. 4.12Folds
  16. 4.13Tail Recursion
  17. 4.14Accumulators
  18. 4.15Prefer Folds to Explicit Recursion
  19. Exercises
  20. 4.16A Virtual Filesystem
  21. 4.17Listing All Files
  22. Exercises
  23. 4.18Conclusion

5.Pattern Matching

  1. 5.1Chapter Goals
  2. 5.2Project Setup
  3. 5.3Simple Pattern Matching
  4. 5.4Simple Patterns
  5. 5.5Guards
  6. Exercises
  7. 5.6Array Patterns
  8. 5.7Record Patterns and Row Polymorphism
  9. 5.8Nested Patterns
  10. 5.9Named Patterns
  11. Exercises
  12. 5.10Case Expressions
  13. 5.11Pattern Match Failures and Partial Functions
  14. 5.12Algebraic Data Types
  15. 5.13Using ADTs
  16. 5.14Record Puns
  17. Exercises
  18. 5.15Newtypes
  19. 5.16A Library for Vector Graphics
  20. 5.17Computing Bounding Rectangles
  21. Exercises
  22. 5.18Conclusion

6.Type Classes

  1. 6.1Chapter Goals
  2. 6.2Project Setup
  3. 6.3Show Me!
  4. Exercises
  5. 6.4Common Type Classes
  6. Exercises
  7. 6.5Type Annotations
  8. 6.6Overlapping Instances
  9. 6.7Instance Dependencies
  10. Exercises
  11. 6.8Multi Parameter Type Classes
  12. 6.9Functional Dependencies
  13. 6.10Nullary Type Classes
  14. 6.11Superclasses
  15. Exercises
  16. 6.12A Type Class for Hashes
  17. Exercises
  18. 6.13Conclusion

7.Applicative Validation

  1. 7.1Chapter Goals
  2. 7.2Project Setup
  3. 7.3Generalizing Function Application
  4. 7.4Lifting Arbitrary Functions
  5. 7.5The Applicative Type Class
  6. 7.6Intuition for Applicative
  7. 7.7More Effects
  8. 7.8Combining Effects
  9. Exercises
  10. 7.9Applicative Validation
  11. 7.10Regular Expression Validators
  12. Exercises
  13. 7.11Traversable Functors
  14. Exercises
  15. 7.12Applicative Functors for Parallelism
  16. 7.13Conclusion

8.The Eff Monad

  1. 8.1Chapter Goals
  2. 8.2Project Setup
  3. 8.3Monads and Do Notation
  4. 8.4The Monad Type Class
  5. 8.5Monad Laws
  6. 8.6Folding With Monads
  7. 8.7Monads and Applicatives
  8. Exercises
  9. 8.8Native Effects
  10. 8.9Side-Effects and Purity
  11. 8.10The Eff Monad
  12. 8.11Extensible Effects
  13. 8.12Interleaving Effects
  14. 8.13The Kind of Eff
  15. 8.14Records And Rows
  16. 8.15Fine-Grained Effects
  17. 8.16Handlers and Actions
  18. 8.17Mutable State
  19. Exercises
  20. 8.18DOM Effects
  21. 8.19An Address Book User Interface
  22. Exercises
  23. 8.20Conclusion

9.Canvas Graphics

  1. 9.1Chapter Goals
  2. 9.2Project Setup
  3. 9.3Simple Shapes
  4. 9.4Putting Row Polymorphism to Work
  5. Exercises
  6. 9.5Drawing Random Circles
  7. 9.6Transformations
  8. 9.7Preserving the Context
  9. 9.8Global Mutable State
  10. Examples
  11. 9.9L-Systems
  12. Exercises
  13. 9.10Conclusion

10.The Foreign Function Interface

  1. 10.1Chapter Goals
  2. 10.2Project Setup
  3. 10.3A Disclaimer
  4. 10.4Calling PureScript from JavaScript
  5. 10.5Understanding Name Generation
  6. 10.6Runtime Data Representation
  7. 10.7Representing ADTs
  8. 10.8Representing Quantified Types
  9. 10.9Representing Constrained Types
  10. Exercises
  11. 10.10Using JavaScript Code From PureScript
  12. 10.11Wrapping JavaScript Values
  13. 10.12Defining Foreign Types
  14. 10.13Functions of Multiple Arguments
  15. 10.14Representing Side Effects
  16. 10.15Defining New Effects
  17. Exercises
  18. 10.16Working With Untyped Data
  19. 10.17Handling Null and Undefined Values
  20. 10.18Generic JSON Serialization
  21. Exercises
  22. 10.19Conclusion

11.Monadic Adventures

  1. 11.1Chapter Goals
  2. 11.2Project Setup
  3. 11.3How To Play The Game
  4. 11.4The State Monad
  5. Exercises
  6. 11.5The Reader Monad
  7. Exercises
  8. 11.6The Writer Monad
  9. Exercises
  10. 11.7Monad Transformers
  11. 11.8The ExceptT Monad Transformer
  12. 11.9Monad Transformer Stacks
  13. Exercises
  14. 11.10Type Classes to the Rescue!
  15. 11.11Alternatives
  16. 11.12Monad Comprehensions
  17. 11.13Backtracking
  18. Exercises
  19. 11.14The RWS Monad
  20. 11.15Implementing Game Logic
  21. 11.16Running the Computation
  22. Exercises
  23. 11.17Handling Command Line Options
  24. Exercises
  25. 11.18Conclusion

12.Callback Hell

  1. 12.1Chapter Goals
  2. 12.2Project Setup
  3. 12.3The Problem
  4. 12.4The Continuation Monad
  5. Exercises
  6. 12.5Putting ExceptT To Work
  7. Exercises
  8. 12.6A HTTP Client
  9. Exercises
  10. 12.7Parallel Computations
  11. Exercises
  12. 12.8Conclusion

13.Generative Testing

  1. 13.1Chapter Goals
  2. 13.2Project Setup
  3. 13.3Writing Properties
  4. 13.4Improving Error Messages
  5. Exercises
  6. 13.5Testing Polymorphic Code
  7. Exercises
  8. 13.6Generating Arbitrary Data
  9. Exercises
  10. 13.7Testing Higher-Order Functions
  11. 13.8Writing Coarbitrary Instances
  12. 13.9Testing Without Side-Effects
  13. Exercises
  14. 13.10Conclusion

14.Domain-Specific Languages

  1. 14.1Chapter Goals
  2. 14.2Project Setup
  3. 14.3A HTML Data Type
  4. 14.4Smart Constructors
  5. Exercises
  6. 14.5Phantom Types
  7. Exercises
  8. 14.6The Free Monad
  9. 14.7Interpreting the Monad
  10. Exercises
  11. 14.8Extending the Language
  12. Exercises
  13. 14.9Conclusion

Causes

Causes Supported

Code Club
A nationwide network of volunteer-led after school coding clubs for children aged 9-11.
We create projects for our volunteers to teach at after school coding clubs or at non-school venues such as libraries. The projects we make teach children how to program by showing them how to make computer games, animations and websites. Our volunteers go to their local club for an hour a week and teach one project a week. Each term the students will progress and learn more whilst at the same time using their imaginations and making creative projects. Terms 1 & 2 use Scratch to teach the basics of programming. Term 3 teaches the basics of web development using HTML and CSS. Term 4 teaches Python and so on. We’d like to put a Code Club in every single primary school in the country. There are over 21,000 primary schools in the UK, it’s a big task but we think we can do it!

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 earned over $14 million writing, 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

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub