These are chat archives for ramda/ramda

4th
Dec 2017
Barry G
@bgits
Dec 04 2017 16:04
How can I get this to work as per the comment?
let obj = {
  standard: 'car',
  nice: {
    friend : 'bob'
  }
}

let safeInc = R.unless(R.path(['nice', 'friend']), R.pluck('standard'));
safeInc(obj); 

// should return bob instead of object
Barry G
@bgits
Dec 04 2017 16:21
I want something like pathOr but instead of a default value, it uses another path. How can this be implemented?
Brad Compton (he/him)
@Bradcomp
Dec 04 2017 17:38
Is there a recognized combinator for f => x => f(x, x)?
@bgits Can you use
R.either
Brad Compton (he/him)
@Bradcomp
Dec 04 2017 17:40
@ram-bot
let obj = {
  standard: 'car',
  nice: {
    friend : 'bob'
  }
}

let safeInc = R.either(R.path(['nice', 'friend']), R.prop('standard'));
safeInc(obj);
ram-bot
@ram-bot
Dec 04 2017 17:40
'bob'
Mike Lambert
@lax4mike
Dec 04 2017 19:56
so, working with pairs is kind annoying
eg. i have a function that makes sure that a list of percentages add up to 100% after being rounded, but the values are stored in an object
so I have to do R.toPairs ... lots of computations .. R.fromPairs
return R.compose(
  R.fromPairs,
  R.adjust(([k, v]) => [k, offBy + v], 0),
  R.map(([k, v]) => [k, Math.round(v)]),
  R.sortBy(([k, v]) => Math.round(v) - v),
  R.toPairs
)(percentages);
which yields a pretty ugly pipeline with [k, v] in everything
Alec
@alavkx
Dec 04 2017 20:23
it kind of bums me out too ha..
See below for a horrible idea
const objCompose  = (...args) => R.compose(R.fromPairs, ...args, R.toPairs)
const usingKey = (...args) => R.compose(...args, x => x[0])
const usingVal = (...args) => R.compose(...args, x => x[1])

return objCompose(
  R.adjust(([k, v]) => [k, offBy + v], 0),
  R.map(([k, v]) => [k, Math.round(v)]),
  usingVal(R.sortBy(v => Math.round(v) - v)),
)(percentages);
Brad Compton (he/him)
@Bradcomp
Dec 04 2017 21:04
Lenses (well, isomorphisms) can be a pretty good solution to dealing with that issue
Also, R.map works directly on objects
@ram-bot
const pairIso = R.lens(toPairs, fromPairs);
const valLens = R.lensIndex(1);

over(pairIso)(map(over(valLens, inc)))({a: 1, b: 2})
ram-bot
@ram-bot
Dec 04 2017 21:06
{ a: 2, b: 3 }
Brad Compton (he/him)
@Bradcomp
Dec 04 2017 21:07
@ram-bot
map(inc)({a: 1, b: 2});
ram-bot
@ram-bot
Dec 04 2017 21:07
{ a: 2, b: 3 }
Brad Compton (he/him)
@Bradcomp
Dec 04 2017 21:13
It might also be interesting to create functions withKey, withVal, and withPair that could abstract the array unpacking process...
Mike Lambert
@lax4mike
Dec 04 2017 22:21
yeah, i thought about doing that but wasn't sure how that would play with R.sortBy and R.adjust