Kick off your book project in 2 hours, get started with GhostAI in 2 hours, or do both! Free live workshops, on Zoom. You’ll leave with a real book project and a clear plan to keep going. Saturday, June 27, 2026.

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.
会员免费

有会员资格

免费!

$9.99

支付金额

原因获得

$

Also available for 1 book credit with a Reader Membership

PDF
EPUB
8,690
读者
235
页数
关于

关于

关于本书

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

分享此书

授权许可

Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License

作者

关于作者们

Phil Freeman

I am a developer living in Los Angeles, CA. I work principally in Scala, Haskell and TypeScript. I love using functional techniques to solve real world problems.

I am the original developer of the PureScript programming language and compiler, and have written several of its core libraries.

翻译

翻译

目录

目录

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

支持的原因

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!

Leanpub 无条件、零风险的100%满意保证

支付后的60天内,只需简单点击两下,您便可以退书并且取回先前支付的全部金额
查看完整条款

在10美元的购买中赚取8美元,在20美元的购买中赚取16美元

我们在7.99美元或以上的购买中支付80%的版税,在0.99美元到7.98美元之间的购买中支付80%的版税减去0.5美元固定费用在10美元的销售中您可赚取8美元,在20美元的销售中可赚取16美元。因此,如果我们以20美元的价格售出5000本未退款的图书,您将赚取80,000美元

(是的,一些作者在Leanpub上已经赚取了远超过这个数额的收入。)

事实上,作者们通过在Leanpub上写作、出版和销售已经赚取了超过1400万美元

了解更多关于在Leanpub上写作的信息

免费更新。无DRM。

如果你购买了Leanpub的书,只要作者更新这本书,你就可以免费获得更新!许多作者使用Leanpub在他们编写书籍的过程中发布他们的作品。所有读者都可以获得免费更新,无论他们何时购买的书或他们支付了多少钱(包括免费)。

大多数Leanpub书籍都提供PDF(适用于计算机)、EPUB(适用于手机和平板电脑)和MOBI(适用于Kindle)格式。书籍包含的格式会显示在此页面的右上角。

最后,Leanpub的书籍没有任何DRM版权保护的限制,所以你可以轻松地在任何支持的设备上阅读它们。

了解更多关于Leanpub的电子书格式以及在哪里阅读它们

在 Leanpub 上写作和出版

作者与出版社使用 Leanpub 来出版正在写作中和已完成的书籍,就像这本书一样。你也可以使用 Leanpub 来撰写、出版和销售你的作品!Leanpub 是功能强大的平台,非常适合认真的作者。它结合了简单、优雅的写作与出版流程,以及一个可销售正在写作中的电子书的线上商店。Leanpub 是作家的神奇之笔:只需编写纯文本,然后点击按钮即可出版你的电子书。真的就是这么简单。

学习更多关于在 Leanpub 上写作的信息