Software Engineering :: Programming :: Languages :: JavaScript :: Example :: Functional Programming Fundamentals Code
⪼ Made with 💜 by Polyglot.
| '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' | |
| ) |
| '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' | |
| ) |
| '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] | |
| ) |
| '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] | |
| ) |
| '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] | |
| ) |