lambda - pannous/warp GitHub Wiki

lambda see closures

The javascript closures notation x => x*x is beautiful but a bit hard to type
square = x :: x*x

square x := x*x only makes sense for named functions

Runs today (warp)

A lambda's parameters may declare their types; a function value picked from a list is called where it is picked:

half = (x:float) => x / 2
half(3)
fs = [x => x + 1, x => x * 2]
fs[1](3)

A library word is a function value too: passed to map, referenced with function, or named with := (a bare f = floor is no reference, as for any function: function-pointer):

[1.5 2.5].map(floor)      // [1 2]
f = function floor
f(2.5)                    // 2
g := floor                // g is floor
[0 0].map(cos)            // [1 1]: sin, cos, tan, exp and ln too

A block that uses it is a function of one item: {it.length} is x => x.length. sort with such a function orders by its value (Python key=len); a function of two items is a comparison (Swift by: >) or a comparator whose negative result means "before" (JavaScript (a, b) => a - b):

words = ["banana" "fig" "apple" "kiwi"]
sort(words, {it.length})           // ["fig" "kiwi" "apple" "banana"]
sort(words, (a, b) => a > b)       // ["kiwi" "fig" "banana" "apple"]
sort([3 1 2], (a, b) => b - a)     // [3 2 1]
words.sort_by(it => it.length)