These are chat archives for ramda/ramda
map (when (...))pattern for this sort of thing
reduce. Iterate through the array until you find the item you're looking for and do whatever needs to be done with it, then use
reducedto immediately exit the
reducein order to avoid unnecessarily looping through other items in the array.
const emptyOrEq1 = locationId => either( compose(isNil, always(locationId)), propEq('locationId', locationId) ) const emptyOrEq2 = curryN(2, unapply(either( propSatisfies(isNil, 0), apply(propEq('locationId')) )))
idproperty than an array of objects, each with an
idproperty. This is a look at that option: https://goo.gl/FYkaVw <-- this solution is not going to be fast if you're making that round trip every time you need to update a single object in the array, but if you can switch over to using the indexed object rather than the array of objects most of the time, it's a good option in terms of ease of use and performance.
reducedagain, using a mix of Ramda and vanilla JS to get the safety of Ramda's
propEqwith the speed advantage of a ternary operator over an
R.ifElse, if performance is important: https://goo.gl/vNRy1M
const emptyOrEqU = either( compose(isNil, nthArg(0)), propEq('locationId') ) const emptyOrEq = R.curryN(2, emptyOrEqU)
nilOrEqgiven that Ramda also has
filteroperation completely if
const findMachines = ifElse( isNil, always(identity), o(filter, propEq('locationId')) )
emptyOrEqwas being used elsewhere
unless(isNil, o(filter, propEq('locationId')))
isNil(locationId), the function returns
locationId, which it then calls with
locationIdis not a function).
const x = l => unless(isNil, o(filter, propEq('locationId', l)))
unlessis unary. My bad, I'm used to making that conversion a lot with unary functions
unlessfirst (matching your suggestion, exactly), then realized what was happening.
const findMachines = uncurryN( 2, ifElse( isNil, always(identity), o(filter, propEq('id')) ) )
const findMachines = R.curry((locationId, machines) => R.isNil(locationId) ? machines : machines.filter(R.propEq('locationId', locationId)))
ifElsein all cases.
ifElsevs ternary, I know that the built-in is much more performant but I really love the declarative style
It does feel a bit wrong to use
always together, e.g.
const f = x => ifElse(equals(true), always(x), identity)
I kind of think it's best for the case when you wanted to test some predicate on a value and then run a function if true, or another if false
is much more readable
const f = x => y => equals(true, y) ? x : y
f is like this:
const f = ifElse(equals(true), success, error)
I think it's much more reasonable than
const f = x => equals(true, x) ? success(x) : error(x)
ifElseis fine if you have one input and want to choose between two different ways of processing the input based on the result of some predicate, but I don't think it's great in my solution above, where one branch is
ifElsefunction for plain values.