Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//-- Sum --------------------------------------------------------------
let
identity = 0,
mappend = (x,y) => x + y, // +
concatSum =
xs => xs.reduce(mappend, identity)
concatSum([1,2,3,4,5]) // => 15
//-- Product ----------------------------------------------------------
let
identity = 1,
mappend = (x,y) => x * y, // *
concatProduct =
xs => xs.reduce(mappend, identity)
concatProduct([1,2,3,4,5]) // => 120
//-- String -----------------------------------------------------------
let
identity = '',
mappend = (x,y) => x + y, // +
concatString =
xs => xs.reduce(mappend, identity)
concatString(['Shoreditch',' ', 'JS']) // => 'Shoreditch JS'
//-- Array ------------------------------------------------------------
let
identity = [],
mappend = (xs,ys) => xs.concat(ys), // Array.prototype.concat
concatArray =
xs => xs.reduce(mappend, identity)
concatArray([[1,2,3],[1,2,3]]) // => [ 1, 2, 3, 1, 2, 3 ]
//-- Function ---------------------------------------------------------
let
identity = x => x,
mappend = (f,g) => x => f(g(x)), // f ∘ g 
concatFunction =
xs => xs.reduce(mappend, identity)
concatFunction([it => it+10, it => it *2])(5) // => 20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.