Skip to content

Instantly share code, notes, and snippets.

@wilmoore
Last active September 26, 2024 14:31
Show Gist options
  • Save wilmoore/9ea21f14e1b6fae6ac29e4e47655a087 to your computer and use it in GitHub Desktop.
Save wilmoore/9ea21f14e1b6fae6ac29e4e47655a087 to your computer and use it in GitHub Desktop.
Software Engineering :: Programming :: Languages :: JavaScript :: Example :: Functional Programming Fundamentals Code
'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]
)

Software Engineering :: Programming :: Languages :: JavaScript :: Example :: Functional Programming Fundamentals Code

⪼ Made with 💜 by Polyglot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment