These are chat archives for ramda/ramda

2nd
Jan 2017
Martin Broder
@mrtnbroder
Jan 02 2017 14:23
Hey folks, happy new year!
Martin Broder
@mrtnbroder
Jan 02 2017 14:30

Does someone have a better implementation for the following than what I have right now:

const input = { foo: true, bar: false, baz: true }
const output = R.evolve({
  foo: R.ifElse(R.equals(true), R.flip(R.defaultTo)('Coffee'), R.identity),
  bar: R.ifElse(R.equals(true), R.flip(R.defaultTo)('Tea'), R.identity),
  baz: R.ifElse(R.equals(true), R.flip(R.defaultTo)('Water'), R.identity),
})(input)

R.values(output).filter(R.identity).join(' ')
// output: 'Coffee Water'

so I have an object that has some keys in it that are either true or false, based on that I transform truthy values into a string, and at the end I join them together.

Martin Broder
@mrtnbroder
Jan 02 2017 14:35
the keys are bound to specific default strings I should add.
Aldwin Vlasblom
@Avaq
Jan 02 2017 14:58
I think I would go for a solution that uses mergeWith on a mapping of the defaults and the input object.
Aldwin Vlasblom
@Avaq
Jan 02 2017 15:04
@ram-bot
const input = { foo: true, bar: false, baz: true }
R.mergeWith((a, b) => b ? a : undefined, {
  foo: 'Coffee',
  bar: 'Tea',
  baz: 'Water'
}, input);
ram-bot
@ram-bot
Jan 02 2017 15:04
{ foo: 'Coffee', bar: undefined, baz: 'Water' }
Aldwin Vlasblom
@Avaq
Jan 02 2017 15:05

Oddly the result contains undefined, which the documentation states shouldn't happen:

The key will be excluded from the returned object if the resulting value is undefined

Also the signature for mergeWith seems wrong. It says (a → a → a) → {a} → {a} → {a} but I'm doing (a → b → c) → {a} → {b} → {c}, which works fine.
Martin Broder
@mrtnbroder
Jan 02 2017 15:13
@Avaq much better, thanks!
since I filter empty values out anyway, this works as before.
Aldwin Vlasblom
@Avaq
Jan 02 2017 15:15
Ah. The signature is not wrong, because the resulting object will contain the pairs from either object which wasn't in the other object, so the merger function is only called for overlapping keys.
This may be a problem for you @mrtnbroder .
eg:
Martin Broder
@mrtnbroder
Jan 02 2017 15:16
nope thats fine
Aldwin Vlasblom
@Avaq
Jan 02 2017 15:16
@ram-bot
const input = { foo: true, bar: false, baz: true }
R.mergeWith((a, b) => b ? a : undefined, {
  nyerk: 'Snarl'
}, input);
ram-bot
@ram-bot
Jan 02 2017 15:16
{ nyerk: 'Snarl', foo: true, bar: false, baz: true }
Aldwin Vlasblom
@Avaq
Jan 02 2017 15:16
It's not XOR.
Martin Broder
@mrtnbroder
Jan 02 2017 15:16
it's actually good, so nothing unwanted goes through it
nothing 'defined' I should say
Aldwin Vlasblom
@Avaq
Jan 02 2017 15:17
Everything unwanted passes through unchanged.
Martin Broder
@mrtnbroder
Jan 02 2017 15:18
ah, I mean like, thats fine because if I didn't add a merging method for the key, then I don't need it.
Aldwin Vlasblom
@Avaq
Jan 02 2017 15:18
Right
Hardy Jones
@joneshf
Jan 02 2017 20:59
You could also use the fact that '' is the identity for strings.
@ram-bot
const input = { foo: true, bar: false, baz: true }
const merged = R.mergeWith((a, b) => b ? a : '', {
  foo: 'Coffee',
  bar: 'Tea',
  baz: 'Water'
}, input);
R.join(' ', R.values(merged));
ram-bot
@ram-bot
Jan 02 2017 21:01
'Coffee  Water'
Hardy Jones
@joneshf
Jan 02 2017 21:01
Oh, I guess not, whatever :)
Aldwin Vlasblom
@Avaq
Jan 02 2017 21:21
@joneshf I tried that too ;)