These are chat archives for ramda/ramda

27th
Feb 2018
Denis Stoyanov
@xgrommx
Feb 27 2018 07:17
also we can use Alternative https://goo.gl/SDGAv3
Julien Gonzalez
@customcommander
Feb 27 2018 11:09
interesting
Francisco
@franciscotln
Feb 27 2018 13:31
The problem with that @xgrommx is that all functions get called
it should stop the moment it finds one that returns a non-empty string and not call the remaining functions
joao@kapmug
@jay-jlm
Feb 27 2018 15:18
ouch! Just been bitten hard by R.repeat()
turns out if you use it with strings it will arr[0] === arr[1] will be true !
I wonder why they chose to implement it that way?
could it be for consistency with how 'repeat' functions work in other languages?
this is how I was using it, btw:
joao@kapmug
@jay-jlm
Feb 27 2018 15:23
const addRandomNumber = R.pipe(
  R.concat(R.__, ':' + Math.round(Math.random()*10)),
)
R.map(addRandomNumber,  [ 'a', 'b', 'c'] )  //   ["a:2", "b:2", "c:2"]   < ---  Surprise,  all numbers come out the same !!
joao@kapmug
@jay-jlm
Feb 27 2018 15:54
Looks like I'm also misunderstanding how the partial application inside the Pipe() is working thou. Turns out Math.random() ends up being a total of 1 times in the above code, which is not what I expected :-)
Brad Compton (he/him)
@Bradcomp
Feb 27 2018 16:30
@jay-jlm There is a fundamental limitation with writing point free functions in JS. Because it's eagerly evaluated, any function invocations are evaluated when the function is defined, not when it is invoked. If you want the Math.random call to be invoked on each function invocation, you would need to wrap it in a function to delay the evaluation until the function is called.
joao@kapmug
@jay-jlm
Feb 27 2018 16:31
that makes sense, yes. What about my previous question, do you happen to know?
wouldnt you say that R.repeat returning the same object multiple times goes agains Ramda's principle of not trying to mutate objects.. I mean.. it's not mutating it but since later there's a good change I will be modifying those objects it would make more sense if they were not the same string
Brad Compton (he/him)
@Bradcomp
Feb 27 2018 16:34

Returns a fixed list of size n containing a specified identical value.

It looks like that's how it's supposed to work

Mike Lambert
@lax4mike
Feb 27 2018 16:34
it works as expected when you don't use R.concat,
const addRandomNumber = k => k + ':' + Math.round(Math.random()*10)
oh, i see, because it's not invoked until the map invokes it
Brad Compton (he/him)
@Bradcomp
Feb 27 2018 16:34
:point_up: It will work as long as you wrap it in a function call to delay the invocation
Sorry for repeating what you said :stuck_out_tongue:
joao@kapmug
@jay-jlm
Feb 27 2018 16:35
btw, I'm aware of @CrossEye 's philosophy that Ramda "is a library for grown ups"... but still ;-)
Brad Compton (he/him)
@Bradcomp
Feb 27 2018 16:35
Yeah, definitely seems like it could bite you
joao@kapmug
@jay-jlm
Feb 27 2018 16:36
@lax4mike no I mean R.repeat() when used with strings will return the same string multiple times, this is even illustrated in example docs
Brad Compton (he/him)
@Bradcomp
Feb 27 2018 16:36
I suspect that it's because of performance, cloning stuff is really slow
In other cases as well (such as reduce) they've resisted suggestions to clone function input. While Ramda will never mutate your inputs, it also doesn't enforce immutability
joao@kapmug
@jay-jlm
Feb 27 2018 16:38
@Bradcomp , compton... sounds like a good guess... but choosing perf over intuitive outputs ugh... doesnt fly well with me .. unless I'm losing something here
Mike Lambert
@lax4mike
Feb 27 2018 16:39
whoa, you're right
const arr = R.repeat("hi", 2);
console.log(arr[0] === arr[1]); // true
is it using new String?
i thought strings were primitive...
Brad Compton (he/him)
@Bradcomp
Feb 27 2018 16:39
They are
joao@kapmug
@jay-jlm
Feb 27 2018 16:39
what happened with the "principle of less surprise"... guess it went out the window this time around :D
Brad Compton (he/him)
@Bradcomp
Feb 27 2018 16:39
@ram-bot
'hi' === 'hi'
ram-bot
@ram-bot
Feb 27 2018 16:39
true
Mike Lambert
@lax4mike
Feb 27 2018 16:39
oh yeah, that makes sense...
what's the problem with repeat then, @jay-jlm ?
Brad Compton (he/him)
@Bradcomp
Feb 27 2018 16:41
It does become an issue if you're using repeat with an object or an array
joao@kapmug
@jay-jlm
Feb 27 2018 16:41
maybe I'm lacking on my js skills, lol
'hi' === new String('hi') //false
Mike Lambert
@lax4mike
Feb 27 2018 16:41
const arr = R.repeat({ a: "a" }, 2);
console.log(arr[0] === arr[1]); // true
i see
Brad Compton (he/him)
@Bradcomp
Feb 27 2018 16:42
@ram-bot
const arrOfObj = R.repeat({}, 5);

arrOfObj[0].a = 1;

arrOfObj;
ram-bot
@ram-bot
Feb 27 2018 16:42
[ { a: 1 }, { a: 1 }, { a: 1 }, { a: 1 }, { a: 1 } ]
joao@kapmug
@jay-jlm
Feb 27 2018 16:42
wait let me do a test here
Brad Compton (he/him)
@Bradcomp
Feb 27 2018 16:44
One option if you want to make an array of objects is to use R.times instead;
@ram-bot
const arrOfObj = R.times(() => {}, 5);

arrOfObj[0].a = 1;

arrOfObj;
ram-bot
@ram-bot
Feb 27 2018 16:44
Cannot set property 'a' of undefined
Brad Compton (he/him)
@Bradcomp
Feb 27 2018 16:44
hmmm
joao@kapmug
@jay-jlm
Feb 27 2018 16:44
const addRandom = str => str + Math.round(Math.random()*10)
R.map(addRandom, ['a', 'b']) //  [ 'a3',  'b5' ]
Brad Compton (he/him)
@Bradcomp
Feb 27 2018 16:45
@ram-bot
R.times(() => {}, 5);
ram-bot
@ram-bot
Feb 27 2018 16:45
[ undefined, undefined, undefined, undefined, undefined ]
joao@kapmug
@jay-jlm
Feb 27 2018 16:45
ok, that works as expected :D
Brad Compton (he/him)
@Bradcomp
Feb 27 2018 16:45
ooohh
@ram-bot
const arrOfObj = R.times(() => ({}), 5);

arrOfObj[0].a = 1;

arrOfObj;
ram-bot
@ram-bot
Feb 27 2018 16:45
[ { a: 1 }, {}, {}, {}, {} ]
Brad Compton (he/him)
@Bradcomp
Feb 27 2018 16:45
derp
Bijoy Thomas
@bijoythomas
Feb 27 2018 16:45
@ram-bot
R.times(R.always({}), 5)
Mike Lambert
@lax4mike
Feb 27 2018 16:45
@jay-jlm yeah, that works as expected because Math.random isn't evaluated until map calls it
joao@kapmug
@jay-jlm
Feb 27 2018 16:46
so the problem I had before was just the partial application thing. So can I say that R.repeat is not returning the same string intance repeated multiple times right?
Brad Compton (he/him)
@Bradcomp
Feb 27 2018 16:46
Correct
Well, it is returning the same string each time, but because strings are primitive it doesn't matter
joao@kapmug
@jay-jlm
Feb 27 2018 16:46
phew! thanks , now the world makes sense again
ok, will keep that in mind. thank you
joao@kapmug
@jay-jlm
Feb 27 2018 16:52
I guess for a second I forgot that strings are immutable in js.. so any string operations (including str.concat ) will return a new string.. and that's what makes it all safe. In other words, even if you have a function operating a transformation on the same string multiple times you are guaranteed to get a new string back each time
Will Farley
@goldhand
Feb 27 2018 17:37
is there an idomatic way to curry a function that is a result of pipeing other functions?
My first function in pipe has an arity of 2 but pipe wont curry (which seems odd because I would expect pipe to be aware of the arity of the first function then wait for that many args to be provided).
Brad Compton (he/him)
@Bradcomp
Feb 27 2018 18:16

@goldhand There has been quite a bit of discussion on why the results of pipe and compose aren't curried. See ramda/ramda#1391, ramda/ramda#1318, ramda/ramda#1260 for instance.

If I need to curry a composition, I usually just manually curry a function and go from there:

const f = (a) => (b) => pipe(blah, meh(a))(b);

Something like that. It all depends on the specifics of what you're trying to do. You could also just wrap it with curry or curryN

Denis Stoyanov
@xgrommx
Feb 27 2018 20:22
Array.from({length: 5}, (v, i) => ({a: i}))