diff --git a/src/fn.js b/src/fn.js index d672e1f..dd9128b 100644 --- a/src/fn.js +++ b/src/fn.js @@ -26,7 +26,7 @@ export const always = x => () => x export const orDefault = curry((value, defaultValue) => value || defaultValue) export const orElse = curry((value, fn) => orDefault(value, fn())) export const pipe = (...f) => v => reduce(applyTo, v, f) -export const apply = fn => args => fn.apply(null, args) +export const apply = curry((fn, args) => fn.apply(null, args)) export const applyTo = curry((v, fn) => fn(v)) export const useWith = curry((fn, tfns) => curryN( @@ -35,8 +35,8 @@ export const useWith = curry((fn, tfns) => )) export const converge = (fn, tfns) => curryN( - pipe(length, map, max)(tfns), - (...args) => fn(...map(tfn => tfn(...args), tfns)) + pipe(map(length), max)(tfns), + (...args) => pipe(map(flip(apply)(args)), apply(fn))(tfns) ) export const diverge = (fn, tfns) => pipe(applyTo, flip(map)(tfns), apply(fn)) @@ -67,8 +67,12 @@ export const split = curry((delim, v) => v.split(delim)) // math export const max = v => Math.max(...v) export const min = v => Math.min(...v) -export const inc = v => v + 1 -export const dec = v => v - 1 +export const add = curry((x, y) => x + y) +export const sub = curry((x, y) => x - y) +export const mul = curry((x, y) => x * y) +export const div = curry((x, y) => x / y) +export const inc = add(1) +export const dec = sub(1) // arrays export const map = curry((fn, v) => v.map(fn)) @@ -92,6 +96,7 @@ export const append = curry((value, iter) => [...iter, value]) export const enumerate = value => value.map((v, i) => ([i, v])) export const flatten = curry((n, v) => v.flat(n)) export const update = curry((index, value, arr) => arr.toSpliced(index, 1, value)) +export const sum = reduce(add, 0) // objects const makePath = unless(isArray, split('.')) @@ -103,18 +108,18 @@ export const assocPath = curry((key, value, obj) => pipe( mergeRightDeep(obj), )(key)) -export const mergeLeftDeep = curry((a, b) => +export const mergeRightDeep = curry((a, b) => reduce((acc, key) => pipe( ifElse( every(is(Object)), - apply(mergeLeftDeep), + apply(mergeRightDeep), last ), v => assoc(key, v, acc) )([acc[key], b[key]]), a, keys(b)) ) -export const mergeRightDeep = flip(mergeLeftDeep) +export const mergeLeftDeep = flip(mergeRightDeep) export const prop = curry((key, obj) => obj && key && obj[key]) export const path = curry((key, obj) => reduce(flip(prop), obj, makePath(key)))