0

I'm trying to use cond() instead of switch, here is how I set it

const {forEachObjIndexed, equals, cond} = R;

const query = {
  hardSkills : ['119928392'],
  softSkills : null,
  country: null,
  status: null,
  freeQuery: null
}

forEachObjIndexed((value, key) => {
  if(value) {
    console.log('before cond', key)
    cond([
      [equals('hardSkills', key), console.log('in hardSkills')],
      [equals('softSkills', key), console.log('in softSkills')]
    ])
  }
}, query)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.1/ramda.min.js"></script>

output

before cond hardSkills
in hardSkills
in softSkills

According to my understanding, 'in softSkills' shouldn't be displayed, because the second equals statement return false. But Ramda does not look ok with that.

Any idea ?

Thanks in advance to everyone

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
x2cheese
  • 273
  • 5
  • 17
  • 1
    `console.log('in hardSkills')` -> `() => console.log('in hardSkills')`? – VLAZ Mar 23 '21 at 14:16
  • Aside from @VLAZ's excellent point, you're not using [`cond`](https://ramdajs.com/docs/#cond) or [`equals`](https://ramdajs.com/docs/#equals) correctly. They *return* functions (this is FP, after all). It would be `cond([[equals("hardSkills"), () => console.log("in hardSkills")], [equals("softSkills"), () => console.log("in softSkills"]])(key)` (but ideally, call `cond` and remember the function, then reuse it, rather than recreating it every iteration). – T.J. Crowder Mar 23 '21 at 14:24
  • you are right, thanks a lot @VLAZ – x2cheese Mar 23 '21 at 14:30
  • thanks for the explenation @T.J.Crowder – x2cheese Mar 23 '21 at 14:31
  • @T.J.Crowder: Did you close the wrong question as a duplicate? Those seem to have nothing to do with this one. – Scott Sauyet Mar 23 '21 at 15:19
  • 1
    @ScottSauyet - The primary symptom in the question was both `console.log` statements running rather than only running when they should, which is what the duplicate targets address (calling the functions where passing in functions is expected). The rest is effectively typo / not reading documentation so I didn't see any reason to change it when that came to light. (I got curious about the functions, resulting in the comment above.) Others can, though, if it seems appropriate to them. I try to strike a balance between how SE claims it's supposed to work and...what actually seems to happen. :-) – T.J. Crowder Mar 23 '21 at 15:24
  • 1
    @T.J.Crowder: I guess I didn't dig deep enough. I can see that, although I probably wouldn't have chosen to do so. And I wonder if Ramda needs better documentation for `cond`, as this is far from the first time we've seen questions that misunderstand the basic design. I'm part of that team and will try to find a way to make this more clear. – Scott Sauyet Mar 23 '21 at 15:34
  • 1
    @ScottSauyet - That's great! :-) FWIW, when I got curious and looked at the documentation, I found it clear (and I don't even do FP, though balancing that I'm pretty strong with JS generally). But it may well be possible to make it *more* clear. Anyway, it's great of you think of doing it. Best, :-) – T.J. Crowder Mar 23 '21 at 15:55
  • 1
    @ScottSauyet - (Also: The Ramda docs are gorgeous. I'm not a huge fan of lavenders and purples though I have nothing against them either, but the design and font -- or something -- just really works for me, really nice.) – T.J. Crowder Mar 23 '21 at 15:59
  • 1
    @T.J.Crowder: Thanks. I don't even recall who created the color scheme and the restt of that design some years back. (I really doubt it was the non-artistic me!) I'm pretty happy with them, although there always seems to be more to add. ;-) – Scott Sauyet Mar 23 '21 at 16:04
  • @T.J.Crowder: https://github.com/ramda/ramda/pull/3139. Does that sound like enough of a warning? – Scott Sauyet Mar 23 '21 at 23:52
  • 1
    @ScottSauyet - You can't do much more. :-) – T.J. Crowder Mar 24 '21 at 07:40

1 Answers1

2

the second item in the pair of each cond entry should be a function.

Putting a console log there will always be executed immediately.

Apart from that the conditions themselves should also be functions that are applied to the argument which is passed to the function returned by cond.

https://ramdajs.com/docs/#cond

const query = {
  hardSkills : ['119928392'],
  softSkills : null,
  country: null,
  status: null,
  freeQuery: null
}

R.forEachObjIndexed((value, key) => {
  if(value) {
    console.log('before cond', key)
    R.cond([
      [R.equals('hardSkills'), () => console.log('in hardSkills')],
      [R.equals('softSkills'), () => console.log('in softSkills')]
    ])(key)
  }
}, query)
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>
Tiago Coelho
  • 5,023
  • 10
  • 17