Skip to content

Instantly share code, notes, and snippets.

@gabro
Created November 21, 2016 10:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabro/5e96c70c92f0fa56a79b9b58aa5d9ae9 to your computer and use it in GitHub Desktop.
Save gabro/5e96c70c92f0fa56a79b9b58aa5d9ae9 to your computer and use it in GitHub Desktop.
Applicative abstraction in vanilla.js
const arr = {
ap(fab, fa) {
return fab.reduce((acc, f) => acc.concat(fa.map(f)), [])
}
}
const f = (n) => n * 2
const g = (n) => n + 1
const a1 = [f, g]
const a2 = [1, 2, 3]
arr.ap(a1, a2) // => [2, 4, 6, 2, 3, 4]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment