These are chat archives for ramda/ramda

8th
Mar 2019
Dustin Raimondi
@raimondi1337
Mar 08 22:03

Not sure if ramda has a slick solution to this problem but I'll ask anyways

//input
[
    ['a', 0],
    ['b', 0],
    ['c', 20],
    ['d', 50],
    ['e', 0],
    ['f', 10],
    ['g', 0],
]

//output
[
    ['c', 20],
    ['d', 50],
    ['e', 0],
    ['f', 10],
]

basically I'm trying to trim 0's off the ends of a 2D array
Any ideas how I can accomplish this?

Karsten Pruehss
@kpruehss
Mar 08 22:36
I'm pretty new to FP in general, but I'm wondering how to do something like this: I need to filter an array of objects with multiple different filters. I'm thinking along the lines of using compose/pipe to run multiple predicate functions through a filter, thus allowing for extending the types of filters that are through functional composition. I'm very unfamiliar with Ramda, so any pointers on how to get started would be appreciated :)
Riku Tiira
@rikutiira
Mar 08 22:47
@kpruehss you can use R.allPass or R.anyPass, ie: R.filter(R.allPass([filterFn1, filterFn2, …])(list)
@raimondi1337 is performance critical?
Karsten Pruehss
@kpruehss
Mar 08 22:49
@rikutiira thank you! I'll give that a shot right now :)
Karsten Pruehss
@kpruehss
Mar 08 23:10
@rikutiira awesome!!! thank you
Riku Tiira
@rikutiira
Mar 08 23:11
no problem!

@raimondi1337 I ended up writing it like this, basically there’s very little ramda specific here except using R.reduced to stop reducing as soon as possible:

const stripZeroes = (arr) => R.reduce((acc) => {
  const start = acc[0][1] === 0 ? 1 : 0
  const end = acc[acc.length-1][1] === 0 ? -1 : Infinity

  return start === 0 && end === Infinity
    ? R.reduced(acc)
    : R.slice(start, end, acc)
}, arr, arr)

Same could be easily achieved with while loop :smile:

Riku Tiira
@rikutiira
Mar 08 23:20
oh hah
there’s actually way cleaner way to do this with ramda
const isZero = R.propSatisfies(R.identical(0), 1)

R.pipe(
  R.dropWhile(isZero),
  R.dropLastWhile(isZero)
)(arr)
Karsten Pruehss
@kpruehss
Mar 08 23:26
hey @rikutiira given the following:
managerFilter = x => x.matterManager === 'some string'; nameFilter = x => x.name === 'some name'; filterPipe = R.filter(R.anyPass([this.managerFilter, this.nameFilter]));
How can i make the predicate functions take a string parameter as well?
would something like managerFilter = x => manager => x.matterManager === manager work and how would i call it?
i feel like im missing something really obvious
Riku Tiira
@rikutiira
Mar 08 23:28
I gotta go now but if you switch the arguments around (ie managerFilter = manager => x => …) then you could do R.filter(R.anyPass([manageFilter(‘name’), …])) :p
Karsten Pruehss
@kpruehss
Mar 08 23:29
thanks @rikutiira sorry i spammed you :). Thanks for all the help
Riku Tiira
@rikutiira
Mar 08 23:29
no problem, good luck with it