Skip to content

Instantly share code, notes, and snippets.

@xaviervia
Last active January 14, 2017 17:53
Show Gist options
  • Save xaviervia/df97860047f7a0f2550c094694c0aec1 to your computer and use it in GitHub Desktop.
Save xaviervia/df97860047f7a0f2550c094694c0aec1 to your computer and use it in GitHub Desktop.
A verbose and redundant version of an imperative sum in fantasy-land functional style
const Add = x => ({
x,
concat: ({x: y}) => x + y,
fold: f => f(x)
})
const Box = x => ({
x,
map: f => Box(f(x)),
fold: f => f(x)
})
const identity = x => x
Box(1)
.map(Add)
.map(
Box(2)
.map(Add)
.fold(identity)
.concat
)
.fold(identity) === 1 + 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment