Pun
A tiny functional programming library for Javascript/Coffeescript.
Now availiable on NPM!
Currying
pun.curry
allows you to partially apply some values to a function:
= abcdjoin', ' fab = puncurry f12 fab34 # 1, 2, 3, 4 fab"c""d" # 1, 2, c, d
pun.autocurry
allows you to create functions which automatically curry when you apply arguments to them:
# Using f from previous example autof = punautocurry f autofab = autof12 autofab34 # 1, 2, 3, 4 autofab"c""d" # 1, 2, c, d
Abstract Data Types
You can use ADTs like so:
List = punADT Cons: 'value''next' Nil: l = ListCons1ListCons2ListNil # Linked list for [1,2] lvalue # 1 lnextvalue # 2
And can apply pattern matching!
Pattern Matching
Pun allows for complex pattern matching in javascript/coffeescript. Let's dive in with an example:
Hello, factorial
Coffeescript
$ = pun$ fac = punmatch 0-> 1 $ n * fac n-1
Javascript
var $ = pun$ var fac = pun
This is equivalent to the Haskell:
fac 0 = 1fac n = n * fac (n-1)
pun.match
takes pairs of arguments, the first being the pattern
the second being the function applied if the pattern is matched. It will return undefined
if there is no match.
Basic Matching
Numbers, strings, bools and undefined/nulls are all matched simply by equality:
f = punmatch 110-> "one" "foobar"-> "two" true-> "three" f110 # "one" f"foobar" # "two" ftrue # "three"
Wildcard
The wild character pun._
can be used to match any value:
_ = pun_ f = punmatch 0-> 0 1-> 1 _-> "Other" f0 # 0 f1 # 1 f"foobarbaz" # "Other"
Type Matching
You can pass functions which will be interpreted as type constructors - this allows you to match your own "classes" or the builtin ones:
f = punmatch Number-> "Number" String-> "String" Boolean-> "Boolean" Cat-> "Cat" f1024 # "Number" f"foobar" # "String" ftrue # "Boolean" cf # "Cat"
Binding
The $
symbol can be used to 'bind' values so that you can use them in the matching function. You can use it without an argument and the bound value will be passed to match function as an argument, or with an argument of a pattern to match:
$ = pun$ # alias $ so we can use it more easily f = punmatch $Number "Num: " $ "Got: " f0 # "Num: 0" ftrue # "Got: true" f # "Got: [object Object]"
Or you can supply it with a string s
and it will be avaliable in this.s
/@s
of the match function:
f = punmatch $'a'-> "Got: "
Finally, you can bind to patterns:
f = punmatch $'a'Number-> "Got: " f1 # "Got 1" ffalse # undefined
Arrays
Arrays will be matched item by item and each element of the array is a pattern. The pattern array length and the value array length must be the same.
$ = pun$ f = punmatch 123-> "onetwothree" $$'a'$'b'Number ", , " f0"lol"2 # "0, lol, 2" f0"lol"true # undefined f123 # "onetwothree" f1234 # undefined
Objects
Objects as a pattern will match each key and value. The value of each key/value pair is a pattern:
$ = pun$ f = punmatch a: 0b: "lol"-> "One" a: 0b: $'n'Number-> "Two " a: 0-> "Three" fa:0 # "Three" fa:0b:"lol"c:1 # "One" fa:0b: 4 # "Two 4"
Abstract Data Types
ADTs can also be matched:
$ = pun$ from = punmatch ListNil-> ListCons$$ xconcat fromxs fromListCons 1ListCons 2ListCons 3ListNil # [1,2,3]