These are chat archives for ramda/ramda

14th
Jun 2018
Tim Kye
@tyrsius
Jun 14 2018 01:57
Why in the Rambda docs do the top two functions, _keys and _trim start with an underscore? They look like they are on the R object like all the other functions
Mike Lambert
@lax4mike
Jun 14 2018 02:26
That's a good question. I feel like it's a mistake
Rocky Madden
@rockymadden
Jun 14 2018 02:30
Fairly sure it’s a bug. It’s been like that for a while. I recall using one just fine.
Brad Compton (he/him)
@Bradcomp
Jun 14 2018 03:12
There are some issues open about it. It's on the radar...
Sergiu Ghenciu
@1024gs
Jun 14 2018 18:56
Hi guys. I always struggle to create a reducer for reduce using ramda arsenal. What I came up with is a simle factory like this: const reducerFactory = (operand, fn) => (a, b) => operand(a, fn(b));
How would you call such a function, if you had it in ramda?
I would appreciate any criticism.
Eugene Krayni
@eakray
Jun 14 2018 20:57
Here’s a question: let’s say i’m mapping over an array that contains objects which have some property that I want to pull out. But this property is itself an array that contains objects. In my array processing pipeline I’m descending further down into a series of nested objects where each level is wrapped in an array so I’d like each map to return a flat array so that i can write a bunch of functions that get passed to map, compose them all, map over the data once and pass in that composed function. Is there a functional idea to handle this?
Mike Lambert
@lax4mike
Jun 14 2018 20:59
@eakray do you have a simple example?

@1024gs

const reducerFactory = (operand, fn) => (a, b) => operand(a, fn(b));

R.reduce(reducerFactory(R.add, R.multiply(2)), 0, [1, 2])
// 6

something like this?

this would be the same thing
R.compose(
  R.reduce(R.add, 0),
  R.map(R.multiply(2))
)([1, 2])
or just do it inline
R.reduce((a, b) => R.add(a, R.multiply(2, b)), 0)([1, 2])
Eugene Krayni
@eakray
Jun 14 2018 21:04
const data = [
  {
    folders: [
      {
        sheets: [
          {
            data:["d"]
          }
        ]
      }
    ],
    folders: [
      {
        sheets: [
          {
            data:["a"]
          }
        ]
      }
    ]
  }
];
suppose the object looks like that. I want to map once to grab all foldes
Folders*
then map over folders to grab all sheets etc.
Mike Lambert
@lax4mike
Jun 14 2018 21:05
so the end result would be ["a", "d"]?
Eugene Krayni
@eakray
Jun 14 2018 21:05
yeah
Mike Lambert
@lax4mike
Jun 14 2018 21:05
you probably need a recursive function
but, is the structure the same?
Eugene Krayni
@eakray
Jun 14 2018 21:06
actually, in the actual data the “a” is a json blog of settings that i recursively walk down and extract a specific prop
but the structure above the last level is fixed
i can flatten the the resulting nested arrays with this

const flatten = (value) => {
  return Array.isArray(value) ? [].concat(...value.map(x => flatten(x))) : value;
}
but I’d like to be able to flatten the things I return in the function that I pass to the map
so that I can compose all of the map steps. I realize I might not be explaining this clearly.
Mike Lambert
@lax4mike
Jun 14 2018 21:09
hmm, well, you can pass a composition to map
R.map(R.compose(flatten, doSomeStuff))
Eugene Krayni
@eakray
Jun 14 2018 21:09
yeah, that’s my plan
but the issue is that I have to flatten between map functions
Mike Lambert
@lax4mike
Jun 14 2018 21:11
.chain will flatten as it goes
Eugene Krayni
@eakray
Jun 14 2018 21:11
oh really?
Mike Lambert
@lax4mike
Jun 14 2018 21:11
aka flatMap
Eugene Krayni
@eakray
Jun 14 2018 21:11
that might do it
can you import an individual ramda module without installing the whole package?
Mike Lambert
@lax4mike
Jun 14 2018 21:14
import identity from 'ramda/src/identity'
Eugene Krayni
@eakray
Jun 14 2018 21:14
Nice. Thanks a lot for your help.
Vesa Karvonen
@polytypic
Jun 14 2018 21:45
@eakray Manipulation of nested data structures is typically easy with optics, e.g. like this with Partial Lenses.
Eugene Krayni
@eakray
Jun 14 2018 21:47
nice. I was sort of hoping to not add dependencies if I could get away with it but thanks for that link.
Sergiu Ghenciu
@1024gs
Jun 14 2018 22:21
this would be the same thing
R.compose( R.reduce(R.add, 0), R.map(R.multiply(2)) )([1, 2]) the advantage of reduce + a reducer is that it does the computation in one go
Mike Lambert
@lax4mike
Jun 14 2018 22:27
You can also check out transduce
Eugene Krayni
@eakray
Jun 14 2018 22:30
interesting. that’s pretty close to my compose mapper functions approach
Sergiu Ghenciu
@1024gs
Jun 14 2018 22:30
So the question is basically would you guys consider to have such a simple factory function in your arsenal or it is already too much?
Eugene Krayni
@eakray
Jun 14 2018 22:30
except i guess you abstract the mapping
and use reduce
no, using reduce isn’t too much. I was referring to pulling down that lenses library.
Ben Briggs
@ben-eb
Jun 14 2018 22:44
@eakray You can also try https://github.com/megawac/babel-plugin-ramda for automating what gets included in your build