PureScript

PureScript is a small strongly typed programming language that compiles to JavaScript.

Examples

Modifying the DOM

PureScript’s expressive type system and lightweight syntax make it simple to define domain-specific languages, which can be used to solve problems like templating the DOM. Bindings also exist for libraries such as React and Virtual DOM.

You can try this example online.

import Flare
import Flare.Smolder
 
ui = greet <$> string "Name" "World"
  where
  greet name = h1 $ text $ "Hello, " <> name <> "!"

HTML5 Canvas

Higher-order functions allow the developer to write fluent, expressive code. Here, the higher-order function thrice is being used to simplify some code while working with the canvas.

You can try this example online.

import Flare
import Flare.Drawing

thrice f x = f (f (f x))

scene = thrice subdivide
      $ outlined (outlineColor black)
      $ circle 0.0 0.0 1.0

Callback Hell

The problem of callback hell can be solved by using PureScript’s type system to capture complex control flow as functions in a safe way. Here, the continuation monad is used to hide the boilerplate code associated with handling callbacks.

import Control.Monad.Aff

data Model = Model (List Product)

loadModel = do
  popular <- get "/products/popular"
  products <- parTraverse (\product -> get product.uri) popular
  pure (Model products)

Generative Testing

PureScript provides a form of ad-hoc polymorphism in the form of type classes, inspired by Haskell. Type classes are used in the QuickCheck and StrongCheck libraries to support generative testing, which separates test definitions from the generation of test cases.

You can try this example online.

import Test.QuickCheck

main = do
  quickCheck $ \xs ys ->
    isSorted $ merge (sort xs) (sort ys)
  quickCheck $ \xs ys ->
    xs `isSubarrayOf` merge xs ys

Features