These are chat archives for ramda/ramda

21st
Sep 2015
hemanth.hm
@hemanth
Sep 21 2015 02:58
@CrossEye Oh ok, achieving laziness with ES6 generators would be fun?
Ignacio Chavez
@nashio
Sep 21 2015 07:58
is this a good place to ask questions?
Raine Virta
@raine
Sep 21 2015 07:59
definitely
Ignacio Chavez
@nashio
Sep 21 2015 07:59
:)
So this used to work in 0.15
var users = [ 
  {id: 1,  name: 'stuff',  age: 10}, 
  {id: 2,  name: 'stuff2',  age: 5}
];

var findUserIdx = R.compose( R.findIndex, R.propEq('id') );
console.log( findUserIdx(1)(users) );
in 0.17, seems like is asking for another argument
Raine Virta
@raine
Sep 21 2015 08:01
that should return 0?
Ignacio Chavez
@nashio
Sep 21 2015 08:01
yes
Raine Virta
@raine
Sep 21 2015 08:02
var findUserIdx = R.useWith(R.findIndex, R.propEq('id'));
findUserIdx(2, users)
Ludwig Magnusson
@TheLudd
Sep 21 2015 08:03
@nashio The clue might be here ramda/ramda#1318
Looks like changes to pipe and compose might be reverted in the next version but not clear yet.
Ignacio Chavez
@nashio
Sep 21 2015 08:04
gotcha,
@raine trying useWith,
Ludwig Magnusson
@TheLudd
Sep 21 2015 08:04
But as a side note, the way @raine constructed your function is the same way I do
Ignacio Chavez
@nashio
Sep 21 2015 08:04
how do you use compose now then?
Ludwig Magnusson
@TheLudd
Sep 21 2015 08:05
@nashio idk :)
I also experienced problems upgrading, I havent taken the time to really understand the implications and the changes might be reverted so I am staying at 0.15.1
Ignacio Chavez
@nashio
Sep 21 2015 08:06
ah,
I see
useWith works great, but I’m still interested in knowing whats the plan with R.compose now, I’m gonna read up some more
Scott Sauyet
@CrossEye
Sep 21 2015 11:04
This change should be reverted in v.18. See #1391.
hemanth.hm
@hemanth
Sep 21 2015 11:09
Looking for a neater way for https://twitter.com/GNUmanth/status/645904814270885888 (might be bit of off topic)
Ludwig Magnusson
@TheLudd
Sep 21 2015 11:13
@hemanth
What about R.find(cond, arr)?
Raine Virta
@raine
Sep 21 2015 11:13
I think using ramda would be "unfair" comparison
but yeah, why not use Array#find?
hemanth.hm
@hemanth
Sep 21 2015 11:20
@raine Even with babel you would need to require the polyfill to use Array#find
@TheLudd as @raine mentioned
Ludwig Magnusson
@TheLudd
Sep 21 2015 11:21
what is unfair?
Raine Virta
@raine
Sep 21 2015 11:22
$ babel-node -e 'console.log(typeof [].find)'
function
@TheLudd i suppose the fact that using ramda is equivalent to just wrapping the function on the left into a function, and not using language built-ins to achieve a more elegant solution
Martin Algesten
@algesten
Sep 21 2015 11:24
the not so nice way. efficient though.
const findFirst = (arr, cond) => { for (var r,i=0;i < arr.length; ++i) { if (cond(r = arr[i])) return r }; return null};
Raine Virta
@raine
Sep 21 2015 11:25
you have ES6 syntax there, might as well be using Array.prototype.find
Martin Algesten
@algesten
Sep 21 2015 11:25
:D !!! good point
hemanth.hm
@hemanth
Sep 21 2015 11:26
@algesten the whole idea was to avoid for loops ;)
Martin Algesten
@algesten
Sep 21 2015 11:27
@hemanth but your tweet is not equivalent since it doesn’t break early.
hemanth.hm
@hemanth
Sep 21 2015 11:28
@algesten yes, but as mentioned for my usecase there will be only 5 elems in the array :D
Scott Sauyet
@CrossEye
Sep 21 2015 11:34
I can't see the code well on my phone.
But it looks
hemanth.hm
@hemanth
Sep 21 2015 11:35
@raine babel-node has, but not when you use it in the build phase.
Scott Sauyet
@CrossEye
Sep 21 2015 11:35
As if the imperative solution would return the last match, no?
hemanth.hm
@hemanth
Sep 21 2015 11:36
imperative solution will break as soon as it finds the first match.
Scott Sauyet
@CrossEye
Sep 21 2015 11:36
ok. As I said, I can't really read it.
hemanth.hm
@hemanth
Sep 21 2015 11:36
if we were to use a forEach breaking it would be just returning a false i guess
Hardy Jones
@joneshf
Sep 21 2015 12:26

As with most things computer science, the elegant solution is to use the correct data type. Rather than using eager arrays, move to a lazy data structure.

So long as your structure has filter on the prototype and supports bracket indexing, you don't even have to change your code. Just pass in the correct thing and let duck typing take care of the rest.

When you want eager evaluation, like when you have five elements, just use an array.
when you want shortcutting, convert it to a lazy structure.
That could be transducers if they work with filter and indexing, but you'd have to check that yourself.
Or it could be a lazy list if you want that.
Or it could be something else entirely.
but the choice is now on the user of the code.
Rather, the user has the ability to make the choice!
you have, what they call, code reuse.
whereas the previous one was less reusable.

Not only that, easier to maintain.

Move that lambda out and findFirst becomes much more understandable.

Hardy Jones
@joneshf
Sep 21 2015 12:31
you can grasp what it's doing way quicker than the loop.
as demonstrated by @CrossEye ;)
Scott Sauyet
@CrossEye
Sep 21 2015 12:33
Hey, no pickling on the guy with the old eyes! :smile:
Hardy Jones
@joneshf
Sep 21 2015 12:38
I didn't mean it in a negative way. But if it tripped you up, it did the same for others already.
Of course, that filter isn't entirely clear what it's doing anyway, so this point might be moot.

Case in point,

why do

arr.filter((_val, i) => cond(arr[i]))

instead of

arr.filter((val) => cond(val))
or even better
arr.filter(cond)
Scott Sauyet
@CrossEye
Sep 21 2015 12:41
Oh no, just teasing. I definitely agree (although I might not entirely buy your data-type as always the most elegant contentiom )
Hardy Jones
@joneshf
Sep 21 2015 12:41
maybe elegant is the wrong word.
Martin Algesten
@algesten
Sep 21 2015 14:03
so. I'm thinking I want work off a generator function. I have a bunch of functions combined in say a pipe. I want to provide a generator and get a generator back and iterate over the result. does ramda have that? or some other lib?
I'm also thinking of the overlap of rxfp like bacon.js and generators. perhaps bacon and rx are not interesting given es 2015.
and then there must surely be an algebraic structure that abstracts the generator thing in a correct way?
and there's so direct question here. I'm trying to get my head around what generators do in the echo system of fp I start to know.
Raine Virta
@raine
Sep 21 2015 14:09
I want to recurse through an object and remove specific keys, any ideas or library suggestions?
Martin Algesten
@algesten
Sep 21 2015 14:15
recurse? like descend into child objects?
Raine Virta
@raine
Sep 21 2015 14:16
correct
Martin Algesten
@algesten
Sep 21 2015 14:26
untested. but i was thinking of that converge/zipobj thing we saw the other day
var filterKeys = R.pipe(R.pickBy(someCond),R.converge(R.zipObj,R.keys,R.pipe(R.values,R.ifElse(R.is(Object),filterKeys,R.identity)));
hemanth.hm
@hemanth
Sep 21 2015 15:11
trying to make a obj out of a string 'meow||a,b1,c3,d|a,d1,m1,r1|14'; the object would look like: {name:'meow', st1:'a,b1,c3,d',st2:'a,d1,m1,r1'}
if you as me why? hmm that's because that data is from a server on which i have no control on and the file format is a psv
so it's like psv to json
@joneshf Read your msg now, cool!
hemanth @hemanth wants to play with RE here ;)
Scott Sauyet
@CrossEye
Sep 21 2015 16:19
@raine: how would you identify the keys? Specific strings, regardless of location? Paths? A predicate?
Raine Virta
@raine
Sep 21 2015 16:20
specific strings or a predicate would work, regardless of location
Kevin Sivabalan
@timestep
Sep 21 2015 18:11
is there a good way to convert an array into an object via ramda
Raine Virta
@raine
Sep 21 2015 18:20
timestep: you'll have to be more specific
Scott Sauyet
@CrossEye
Sep 21 2015 18:24
@raine: nothing simple comes to mind. Obviously you could modify clone easily enough. But that seems like overkill.
Raine Virta
@raine
Sep 21 2015 18:26
yeah, turns out I don't even need it anymore. I was writing a script until I realized the same can be achieved with two shell commands and a pipe
Martin Algesten
@algesten
Sep 21 2015 18:30

Just caught up with #1315 and R.when sits interestingly at odds with what @TheLudd said yesterday. Regarding our discussion of a maybe function.

The reason there is no "only if" function in ramda is that the function needs to return something.

R.when(I,fn) is what I meant with maybe.

sort of.
more like R.not(R.isNil) as predicate. to avoid falsey
Raine Virta
@raine
Sep 21 2015 18:32
R.complement(R.isNil)
not is * → Boolean
Martin Algesten
@algesten
Sep 21 2015 18:33
oh yes.
but as predicate. does it matter?
am i being dense?
oh. the composition is wrong
R.pipe(R.isNil,R.not)
would that work?
Raine Virta
@raine
Sep 21 2015 18:36
I think so
Scott Sauyet
@CrossEye
Sep 21 2015 18:38
@timestep: fromPairs converts a particular style of arrays to objects. But there are many possible things you could want.
Raine Virta
@raine
Sep 21 2015 20:14
when you map a list inside a functor, is there a shortcut for map(map(f))?
David Chambers
@davidchambers
Sep 21 2015 20:14
I don't think so.
Shortcuts for map(chain(f)) and chain(map(f)) might also be useful.
Raine Virta
@raine
Sep 21 2015 21:31
first real use for ramda-fantasy Future
David Chambers
@davidchambers
Sep 21 2015 21:31
Yeah?
Raine Virta
@raine
Sep 21 2015 21:57
yeah, Promises were tempting, but perseverance paid off!
David Chambers
@davidchambers
Sep 21 2015 21:58
Well done for choosing simple rather than easy.
Scott Sauyet
@CrossEye
Sep 21 2015 23:06
I had already installed replem, but this was my first look at the code. Very cool!
David Chambers
@davidchambers
Sep 21 2015 23:09
@raine, any chance of replem applying R.toString to the output, or providing a flag to make it do so? It's nice to see Just(42) and Nothing() rather than { value: 42 } and {}.