These are chat archives for ramda/ramda

9th
Jul 2017
Johnny Hauser
@m59peacemaker
Jul 09 2017 12:13
I have a callback function that takes x, I need it to take lastX, x, which is a reduce. I can't think of the right way to turn the callback into that
Johnny Hauser
@m59peacemaker
Jul 09 2017 12:42
This is the function I'm looking for
const foo = (fn, initialValue) => {
  let lastValue = initialValue
  return (...args) => {
    lastValue = fn(lastValue, ...args)
  }
}
Which is reduce!
What the heck isn't clicking
Transducers?
Johnny Hauser
@m59peacemaker
Jul 09 2017 12:54
It must be the async that is confusing me
hkrutzer
@hkrutzer
Jul 09 2017 14:40

Is there a way to improve this?:

R.pipe(
  // 1. Select active categories
  R.filter(R.prop('active')),
  R.ifElse(
    // 2. If there is no active category
    R.isEmpty,
    // 2a. proceed with all categories with data layers because
    // if there is no category selected, all data layers should be active
    () => R.filter(R.has('datalayer'), categories),
    // 2b. Else (1) returned one or more active categories, ensure they
    // have a data layer
    R.filter(R.prop('datalayer')),
  ),
  // 3. Return only the fields we need
  R.map(R.pick(['id', 'datalayer']))
)(categories);

Would be good if I could make 2a pointfree

Kurt Milam
@kurtmilam
Jul 09 2017 15:07
@hkrutzer I believe this does the trick, but it filters twice on prop('active') if the first filter returns hits.
There's probably a cleaner way to do it.
Kurt Milam
@kurtmilam
Jul 09 2017 15:13
On the other hand, I'd recommend against striving for point-free functions at all costs.
hkrutzer
@hkrutzer
Jul 09 2017 15:27
Thank you. I agree, but I didn't like the wrapping function around the filter either
Kurt Milam
@kurtmilam
Jul 09 2017 15:29
I'm sure it could still be cleaned up. Maybe with reduce and reduced.
Kurt Milam
@kurtmilam
Jul 09 2017 15:48
@hkrutzer I think this is cleaner and avoids filtering twice on R.prop( 'active' ):
const toList = R.unapply( R.identity )
R.pipe(
  toList,
  R.ap( [ R.filter( R.prop('active') ), R.identity ] ),
  R.ifElse(
    R.o( R.isEmpty, R.head ),
    R.o( R.filter( R.has('datalayer') ), R.last ),
    R.o( R.filter( R.prop('datalayer') ), R.head )
  ),
  R.map(R.pick(['id', 'datalayer']))
)(categories)
hkrutzer
@hkrutzer
Jul 09 2017 16:08
Thanks! It is cleaner in execution but a little less readable than your other version I think. I'll consider both :)
Kurt Milam
@kurtmilam
Jul 09 2017 16:09
:thumbsup:
I think the second one is a good technique to have in your tool chest. It's similar to a Maybe.
hkrutzer
@hkrutzer
Jul 09 2017 16:20
It is. However not all of my colleagues are very FP-inclined :D
Kurt Milam
@kurtmilam
Jul 09 2017 16:31
Understood.