Created
March 3, 2016 11:10
-
-
Save voltrevo/d8928591242c611eb3a0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
reduce = [init, arr, combine] => match(arr) { | |
[head, tail...] => ( | |
reduce([combine([init, head]), tail, combine]) | |
); | |
[] => ( | |
init | |
); | |
}; | |
sum = (arr) => ( | |
reduce(0, arr, [a, b] => a + b); | |
); | |
sum = (arr) => { | |
res = 0; | |
arr.each(x => res += x); | |
return res; | |
}; | |
reduce = [v, arr, combine] => { | |
arr.each(x => v = combine(v, x)); | |
return init; | |
}; | |
each = [arr, fn] => { | |
match(arr) { | |
[head, tail...] => { | |
fn(head); | |
each(tail, fn); | |
}; | |
[] => (); | |
}; | |
}; | |
map = (() => { | |
impl = [curr, rem, fn] => match(rem) { | |
[head, tail...] => impl[[curr..., head], tail, fn]; | |
}; | |
return [arr, fn] => impl[[], arr, fn]; | |
})(); | |
qMap = [curr = [], arr, fn] => match(arr) { | |
[head, tail...] => qMap[[curr..., fn(head)], tail, fn]; | |
}; | |
mapImpl = [curr, rem, fn] => match(rem) { | |
[head, tail...] => mapImpl[[curr..., fn(head)], tail, fn]; | |
[] => []; | |
}; | |
map = [arr, fn] => mapImpl([[], arr, fn]); | |
lazyMap = [arr, fn] => match(arr) { | |
[] => []; | |
[head, tail...] => [fn(head), lazyMap[tail, fn]...]; | |
}; | |
map = [arr, fn] => match(arr) { | |
[head, tail...] => ( | |
[fn(head), map[tail, fn]...]; | |
); | |
[] => []; | |
}; | |
sum = arr => match(arr) { | |
[head, tail...] => head + sum(tail); | |
[] => 0; | |
}; | |
reduce = [acc, arr, join] => match(arr) { | |
[head, tail...] => reduce[join[acc, head], arr, join]]; | |
[] => acc; | |
}; | |
arrToList = (() => { | |
impl = [list, arr] => match(arr) { | |
[] => list; | |
[head..., tail] => impl[{value: tail, next: list}, head]; | |
}; | |
return arr => impl[(), arr]; | |
})(); | |
sum = (() => { | |
impl = [s, arr] => match(arr) { | |
[] => s; | |
[head, tail...] => impl[s + head, tail]; | |
}; | |
return arr => impl[0, arr]; | |
})(); | |
arrToList = arr => revReduce[(), arr, [list, x] => {value: x, next: list}]; | |
listReduce = [acc, list, join] => match(list) { | |
() => acc; | |
{value, next} => listReduce[join[acc, value], next, join]; | |
}; | |
match(getAnswer()) { | |
'yes' => console.log('You said yes.'); | |
'no' => console.log('boo'); | |
}; | |
if ([a, b] matches values) { | |
console.log('Got two values', a, b); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment