Skip to content

Instantly share code, notes, and snippets.

@christiantakle
Created January 28, 2016 10:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save christiantakle/a79a49e11ef0a46020d7 to your computer and use it in GitHub Desktop.
Save christiantakle/a79a49e11ef0a46020d7 to your computer and use it in GitHub Desktop.
//-- 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