## o.coffee | |
---|---|

ø is a library that eases functional programming in JavaScript and
CoffeeScript. It contains common generic functions such as At the moment, ø only supports using it with Node.js. Contributions are more than welcome! | |

| `ø = (f, args...) -> (args_...) -> f(args..., args_...)` |

## Identity functions | |

| `ø.id = (x) -> x` |

This function is an alternative to | `ø.list = (xs...) -> xs` |

## Generic higher-order functions | |

| `ø.flip = (f) -> (x, y) -> f(y, x)` |

| `ø.curry = ø` |

| `ø.apply = (f, args...) -> f(args...)` |

| `ø.comp = (f, g) -> (args...) -> f(g(args...))` |

## Manipulating lists | |

| `ø.unshift = (x, xs) -> ø.list(x, xs...)` |

| `ø.head = (xs) -> xs[0]` |

| `ø.tail = (xs) -> xs[1..]` |

| `ø.last = (xs) -> xs[xs.length - 1]` |

| `ø.init = (xs) -> xs[..xs.length - 2]` |

| `ø.null = (xs) -> xs.length is 0` |

| `ø.elem = (x, xs) -> ø.any(ø.curry(ø.eq, x), xs)` |

| `ø.length = (xs) -> xs.length` |

| `ø.glue = (xs, ys) -> ø.list(xs..., ys...)` |

| `ø.map = (f, xs) -> f(x) for x in xs` |

| `ø.filter = (f, xs) -> x for x in xs when f(x)` |

| ```
ø.land = (xs) ->
for x in xs
return false unless x
true
``` |

| ```
ø.lor = (xs) -> not ø.null(ø.filter(ø.id, xs))
ø.all = (f, xs) -> ø.comp(ø.land, ø.curry(ø.map, f))(xs)
ø.any = (f, xs) -> ø.comp(ø.lor, ø.curry(ø.map, f))(xs)
``` |

## Folding | |

| ```
ø.foldl = (f, z0, xs0) ->
lgo = (z, xs) ->
if ø.null(xs) then z
else lgo(f(z, ø.head(xs)), ø.tail(xs))
lgo(z0, xs0)
``` |

| `ø.foldl1 = (f, xs) -> ø.foldl(f, ø.head(xs), ø.tail(xs))` |

| ```
ø.foldr = (f, z, xs) ->
go = (ys) ->
if ys.length is 0 then z
else f(ø.head(ys), go(ø.tail(ys)))
go(xs)
``` |

| ```
ø.foldr1 = (f, xs) ->
if xs.length is 1
ø.head(xs)
else
f(ø.head(xs), foldr1(f, ø.tail(xs)))
``` |

| ```
ø.sum = (xs) -> ø.foldl1(ø.add, xs)
ø.product = (xs) -> ø.foldl1(ø.multiply, xs)
``` |

| `ø.reverse = (xs) -> ø.foldl(ø.flip(ø.unshift), [], xs)` |

| ```
ø.concat = (xss) -> ø.foldr(ø.glue, [], xss)
ø.concatMap = (f, xs) -> ø.foldr(ø.comp(ø.glue, f), [], xs)
``` |

## Basic mathematics | |

| ```
ø.succ = (x) -> x + 1
ø.pred = (x) -> x - 1
``` |

| ```
ø.even = (a) -> a % 2 is 0
ø.odd = (a) -> a % 2 isnt 0
``` |

## Operators | |

These functions map to JavaScript's operators. They are useful in
certain higher-order situations. For example, if you want to compare
all elements in a list and return a new list of Booleans, you can use
| ```
ø.and = (a, b) -> a and b
ø.or = (a, b) -> a or b
ø.not = (a) -> not a
ø.add = (a, b) -> a + b
ø.subtract = (a, b) -> a - b
ø.multiply = (a, b) -> a * b
ø.divide = (a, b) -> a / b
ø.mod = (a, b) -> a % b
ø.negate = (a) -> -a
ø.eq = (x, y) -> x is y
ø.neq = (x, y) -> x isnt y
ø.gt = (x, y) -> x > y
ø.gte = (x, y) -> x >= y
ø.lt = (x, y) -> x < y
ø.lte = (x, y) -> x <= y
module.exports = ø
``` |