forked from kyleshevlin/just-enough-fp-lessons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshared.js
25 lines (25 loc) · 854 Bytes
/
shared.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
exports.compose = (...fns) => x =>
fns.reduceRight((acc, fn) => fn(acc), x)
exports.pipe = (...fns) => x =>
fns.reduce((acc, fn) => fn(acc), x)
exports.trace = msg => x => (
console.log(msg, x), x
)
exports.prop = key => obj => obj[key]
exports.map = fn => xs => xs.map(fn)
exports.filter = fn => xs => xs.filter(fn)
exports.reduce = fn => start => xs =>
xs.reduce((acc, x) => fn(acc)(x), start)
exports.add = x => y => x + y
exports.subtract = x => y => x - y
exports.multiply = x => y => x * y
exports.divide = x => y => x / y
exports.split = pattern => str =>
str.split(pattern)
exports.join = separator => xs =>
xs.join(separator)
exports.lowerCase = str => str.toLowerCase()
exports.upperCase = str => str.toUpperCase()
exports.scream = str => str.toUpperCase()
exports.exclaim = str => `${str}!`
exports.repeat = str => `${str} ${str}`