Skip to content

Commit

Permalink
Fixed foldWhile code style thanks to Sam feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
sadasant committed Mar 14, 2017
1 parent 52358df commit 7773611
Showing 1 changed file with 6 additions and 16 deletions.
22 changes: 6 additions & 16 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,12 @@ export const map = _.curry((f, x) => (_.isArray(x) ? _.map : _.mapValues)(f, x))
export const deepMap = _.curry((fn, obj, _map = map, is = isTraversable) =>
_map(e => is(e) ? deepMap(fn, fn(e), _map, is) : e, obj))

// y-combinator, as in: http://kestas.kuliukas.com/YCombinatorExplained
// f is the function that returns the function we want to make recursive
// n indicates the next call of f
export const y = f => (n => n(n))(n => f(x => n(n)(x))) // eslint-disable-line lodash-fp/no-extraneous-function-wrapping

// Like haskell's: fold takeWhile
/* eslint-disable lodash-fp/no-chain */
export const foldWhile = _.curry((fn, obj, r = []) =>
y(next => o => isTraversable(o)
? _.every(k => fn(r, o[k], k) && next(o[k]), _.keys(o))
: o)(obj)
? r : r)
/* eslint-enable lodash-fp/no-chain */

// Queries a traversable data structure
// query(fn, limit, obj)
// Recursive fold takeWhile (like in haskell)
export const foldWhile = _.curry((fn, obj, r = []) => {
let innerFold = o => !isTraversable(o) ? o : _.every(k => fn(r, o[k], k) && innerFold(o[k]), _.keys(o))
innerFold(obj)
return r
})

// Misc
// ----
Expand Down

0 comments on commit 7773611

Please sign in to comment.