These are chat archives for ramda/ramda

29th
Mar 2017
Johnny Hauser
@m59peacemaker
Mar 29 2017 03:38
my curryN benchmark 9,235,863 ops/sec
but if I add f.toString = () => fn.name ? fn.name : fn.toString() it goes down to 1,139,643 ops/sec
f.toString = reduces performance by 900% :/
Pasi Hirvonen
@pasih
Mar 29 2017 07:14

hey guys. I've been wondering if there's a neat way to append to a deeply nested list. say I have:

  const state = {
    foo: "bar",
    quux: "blergh",
    innerObject: {
      id: "1",
      listOfStuff: [{ id: "0", title: "foo" }, { id: "1", title: "bar" }]
   }
};

and I want to append to listOfStuff. This comes up a lot in Redux reducers (at least for me ;)) and I usually just object spread my way out of it

I guess I could set up a lens
Denis Stoyanov
@xgrommx
Mar 29 2017 07:17
what is a field do you need to append data?
Pasi Hirvonen
@pasih
Mar 29 2017 07:17
say I want to append { id: "5", title: "blergh" } to listOfStuff
Denis Stoyanov
@xgrommx
Mar 29 2017 07:19
@pasih for example use evolve https://goo.gl/gYOTE0
Pasi Hirvonen
@pasih
Mar 29 2017 07:19
that is.. about a million times cleaner than nested object spreads.
Denis Stoyanov
@xgrommx
Mar 29 2017 07:20
@pasih or lens over(lensPath(['innerObject', 'listOfStuff']), append({ id: "5", title: "blergh" }))(state)
Pasi Hirvonen
@pasih
Mar 29 2017 07:21
I'm starting to think lenses are very useful for writing redux reducers
Denis Stoyanov
@xgrommx
Mar 29 2017 07:21
maybe :smile:
Pasi Hirvonen
@pasih
Mar 29 2017 07:22
that said, I could occasionally partition my reducers a bit further but that's a topic for another day
Denis Stoyanov
@xgrommx
Mar 29 2017 07:22
@pasih but evolve also good approach
Pasi Hirvonen
@pasih
Mar 29 2017 07:22
anyway, many thanks. I'll read up a bit more on evolve
Aaron Mc Adam
@aaronmcadam
Mar 29 2017 11:10
Is this the best way to create an object from an array, transforming each index as the value? https://goo.gl/9UuHEU
Denis Stoyanov
@xgrommx
Mar 29 2017 11:43
@aaronmcadam ap(zipObj, map(transform))(userRoles)
Denis Stoyanov
@xgrommx
Mar 29 2017 11:48
@aaronmcadam another, but uglier reduce(useWith(merge, [identity, ap(objOf, toUpper)]), {})(userRoles) :smile:
Denis Stoyanov
@xgrommx
Mar 29 2017 11:55
@aaronmcadam or this bull shit :smile: compose(fromPairs, map(over(lensIndex(1), toUpper)), map(repeat(__, 2)))(userRoles)
so, too much ideas
Aaron Mc Adam
@aaronmcadam
Mar 29 2017 11:59
hahah thanks @xgrommx :D
Piet Vandeput
@piet-v
Mar 29 2017 12:27
Can anyone tell me how to flip the 2nd and 3rd arg of R.assoc ? :D
Aaron Mc Adam
@aaronmcadam
Mar 29 2017 12:30
Could you use placeholders?
to help? I wonder
Piet Vandeput
@piet-v
Mar 29 2017 12:33
this is what I have now but was wondering if I could somehow get it pointfree >.<
const assocOf = R.curry((key, object, value) => {
      return R.assoc(key, value, object);
    });
ah maybe withusewith
László Vadász
@maxinteger
Mar 29 2017 12:34
key => R.flip(R.assoc(key))
Denis Stoyanov
@xgrommx
Mar 29 2017 12:34
compose(flip, assoc)
Piet Vandeput
@piet-v
Mar 29 2017 12:34
that would fail though if u pass all 2 or 3 params at same time right?
example call: assocOf('page', CAMPAIGN_CONSTANTS.oneTimeParams)(2)
Denis Stoyanov
@xgrommx
Mar 29 2017 12:37
@ram-bot
compose(flip, assoc)('c')({a:1, b:2}, 3)
ram-bot
@ram-bot
Mar 29 2017 12:37
{ a: 1, b: 2, c: 3 }
Piet Vandeput
@piet-v
Mar 29 2017 12:40
found curried and pointfree but ugly :O
    const assocOf = R.curryN(3, R.converge(R.assoc, [R.identity, R.nthArg(2), R.nthArg(1)]));
Bravi
@Bravilogy
Mar 29 2017 13:12
is there a function that fetches the index of all elements that satisfy a certain predicate?
basically i'm trying to update elements, whose index satisfies a certain predicate
Denis Stoyanov
@xgrommx
Mar 29 2017 13:16
@Bravilogy what do u mean?
Bravi
@Bravilogy
Mar 29 2017 13:17

I have an array

[1, 2, 3, 4, 5]

and I need to update the first 2 elements that have even indexes

Denis Stoyanov
@xgrommx
Mar 29 2017 13:17
addIndex(map)((v, i) => pred(i) ? someFn(v) : v)
Bravi
@Bravilogy
Mar 29 2017 13:18
yeah, but I was trying to do it using compose and stuff :D
basically, equivalent to this:
const fn = arr =>
      arr.map((v, k) => k < 7 && k % 2 ? v * 2 : v);
Denis Stoyanov
@xgrommx
Mar 29 2017 13:19
@Bravilogy addIndex(add)(ifElse((x, i) => ..., )
Bravi
@Bravilogy
Mar 29 2017 13:20
and I thought I would use range(1, 7) and then filter(isEven) and then adjust(multiply(2)) somewhere in there
I've got these pieces and I just need to put them together
lol
Shane Keulen
@SeeThruHead
Mar 29 2017 13:51
is there a pipeWith type function? thinking useWith but instead of passing the args to the first function you pass the result the the next function in the arg list (after applying it the first time)
Pasi Hirvonen
@pasih
Mar 29 2017 13:56
hm, update() changes a list item based on its index. Can I do this based on property value? `[ { id: "1", value: "sth" }, { id: "2", value: "else" } ]. I want to change value where id is 2 . I usually do find(Index) first and then update, is there a more straightforward way?
Denis Stoyanov
@xgrommx
Mar 29 2017 13:57
@SeeThruHead what do u mean?
Brad Compton (he/him)
@Bradcomp
Mar 29 2017 15:13
@pasih Use
R.when
Brad Compton (he/him)
@Bradcomp
Mar 29 2017 15:14
map(when(propEq('id', 2, f)), data);
Shane Keulen
@SeeThruHead
Mar 29 2017 18:01
const pipeWith = R.curryN(2, R.pipe(
  R.zip,
  R.reduce((prevVal, [func, arg]) => func(prevVal)(arg), undefined)
));

const pickBySlice = pipeWith([
  () => R.slice(0, 2),
  list => R.pipe(
    R.pick(list),
    R.filter(x => x > 2)
  )
]);

console.log(pickBySlice(
  [
    ['a', 'b', 'c', 'd', 'e'],
    {
      a: 2,
      b: 3,
      c: 4,
      d: 20,
      e: 21
    }
  ]
));
// {b: 3}


so should I make a pr for this? any interest?
Pasi Hirvonen
@pasih
Mar 29 2017 18:32
@Bradcomp cool, thanks