0

given the following data structure

var categories = [
  {_id: 1, order: 0, ancestors: []},
    {_id: 2, order: 0, ancestors: [1]},
    {_id: 3, order: 0, ancestors: [1]},
  {_id: 4, order: 0, ancestors: []},
  {_id: 5, order: 0, ancestors: [4]},
]

and the functions

var byLevel = R.curry((level) => R.filter(o => o.ancestors.length == level))
var levelZero = byLevel(0);

I'd like to be able to call both of these like so :-

console.log(byLevel(0,categories));
console.log(levelZero(categories));

and get the same result (an array of two categories with ids 1 and 4)

however byLevel returns a function. I can call it like

console.log(byLevel(0)(categories));

and it does what I want. But if I write byLevel like :-

var byLevel = R.curry((level, l) => R.filter(o => o.ancestors.length == level, l))

then

console.log(byLevel(0,categories));

works fine and curries fine. However, it seems not as "clean".

Question

Is there a "Ramda" way to make it so I can define the function so that I can do f(x,y) instead of f(x)(y)

Keith Nicholas
  • 43,549
  • 15
  • 93
  • 156
  • you pass to `curry` function with _one_ argument, so `curry` can't make from it function with _two_ arguments. So this variant seems normal `R.curry((level, l) => R.filter(o => o.ancestors.length == level, l))` – Grundy Oct 28 '15 at 04:24

1 Answers1

1

I think this is all it would take:

var byLevel = R.curry((level, categories) => 
    R.filter(o => o.ancestors.length == level, categories)
);

If you want to curry a function to allow yourself to supply the parameters that way, you have to have them listed.

Scott Sauyet
  • 49,207
  • 4
  • 49
  • 103