Last active
June 23, 2017 19:16
-
-
Save wilmoore/9ea21f14e1b6fae6ac29e4e47655a087 to your computer and use it in GitHub Desktop.
Functional Programming Fundamentals Code
This file contains 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
'use strict' | |
const assert = require('assert') | |
function words (text) { | |
return text.split(/\s+/) | |
} | |
function reverse (list) { | |
return list.reverse() | |
} | |
function join (list) { | |
return list.join(' ') | |
} | |
function pipeline (text) { | |
return join(reverse(words(text))) | |
} | |
assert.strictEqual( | |
pipeline('dog lazy the over jumps fox brown quick The'), | |
'The quick brown fox jumps over the lazy dog' | |
) |
This file contains 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
'use strict' | |
const assert = require('assert') | |
const _ = require('lodash') | |
function words (text) { | |
return text.split(/\s+/) | |
} | |
function reverse (list) { | |
return list.reverse() | |
} | |
function join (list) { | |
return list.join(' ') | |
} | |
// pipeline is a composition of other functions without mention of the arguments they will be applied to. | |
const pipeline = _.flow([words, reverse, join]) | |
assert.strictEqual( | |
pipeline('dog lazy the over jumps fox brown quick The'), | |
'The quick brown fox jumps over the lazy dog' | |
) |
This file contains 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
'use strict' | |
const assert = require('assert') | |
const map = require('lodash/fp/map') | |
function add (n1, n2) { | |
return n1 + n2 | |
} | |
assert.deepStrictEqual( | |
map(n => add(10, n), [3, 4, 5]), | |
[13, 14, 15] | |
) |
This file contains 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
'use strict' | |
const assert = require('assert') | |
const map = require('lodash/fp/map') | |
function add (n1, n2) { | |
return n1 + n2 | |
} | |
function add10 (n) { | |
return add(10, n) | |
} | |
assert.deepStrictEqual( | |
map(add10, [3, 4, 5]), | |
[13, 14, 15] | |
) |
This file contains 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
'use strict' | |
const assert = require('assert') | |
const map = require('lodash/fp/map') | |
const curry = require('lodash').curry | |
const add = curry(function add (n1, n2) { | |
return n1 + n2 | |
}) | |
assert.deepStrictEqual( | |
map(add(10), [3, 4, 5]), | |
[13, 14, 15] | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment