Created
January 28, 2016 10:03
-
-
Save christiantakle/a79a49e11ef0a46020d7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//-- 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