Skip to content

Instantly share code, notes, and snippets.

@halan
Created November 21, 2016 22:33
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save halan/c3c0ec1142b8d1bbf242939c238fbcab to your computer and use it in GitHub Desktop.
Save halan/c3c0ec1142b8d1bbf242939c238fbcab to your computer and use it in GitHub Desktop.
const reduce = (reducer, initial, [head, ...tail]) =>
head // condition to go or stop
? reduce(reducer, reducer(initial, head), tail) // recursion
: initial // stop
const map = (mapper, [head, ...tail]) =>
head // condition to go or stop
? [ mapper(head), ...map(mapper, tail) ] //recursion
: [] // stop
const filter = (predicate, [head, ...tail]) =>
head // condition to go or stop
? [ ...(predicate(head) ? [head] : []), ...filter(predicate, tail) ] // recursion
: [] // stop
const zip = ([head, ...tail], [head2, ...tail2]) =>
head // condition to go or stop
? [ [head, head2], ...zip(tail, tail2)] // recursion
: [] // stop
const find = (predicate, [head, ...tail]) =>
head // condition to go or stop
? predicate(head) ? head : find(predicate, tail) // recursion
: undefined // stop
const sort = ([head, ...tail]) => // quick sort (https://pt.wikipedia.org/wiki/Quicksort)
head // condition to go or stop
? [...sort(filter(n => n <= head, tail)), head, ...sort(filter(n => n > head, tail))] // recursion (depends of filter)
: [] // stop
@suissa
Copy link

suissa commented Nov 22, 2016

Fodaaaaaaaaaaa

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment