These are chat archives for ramda/ramda

19th
Jan 2019
ataskine
@ataskine
Jan 19 12:00
Hi! Can anyone tell me what "partial copy" means on the R.pick documentation? On the basis of following test pick seems to return a reference to original object?
$ node
> const { pick } = require('ramda');
> const o = {a: [1, 2], b: 1};
> const p = pick(['a'], o);
> p.a.push(3);
> o
{ a: [ 1, 2, 3 ], b: 1 }
One line missing :)
$ node
> const { pick } = require('ramda');
> const o = {a: [1, 2], b: 1};
> const p = pick(['a'], o);
> p.a.push(3);
> o
{ a: [ 1, 2, 3 ], b: 1 }
> p
{ a: [ 1, 2, 3 ] }
Rakesh Pai
@rakeshpai
Jan 19 12:02
It's a partial copy in the sense that o now only has a, and not b. However, it's only a shallow copy, in the sense that a isn't cloned, it's just copied by reference.
Sorry, I mean p only hasa, and not b.
ataskine
@ataskine
Jan 19 12:06

Yes, but it looks like depending on the object property type. Sometimes it really clones the properties and sometimes it uses references.

const o = {a: 0, b: 1, c: 2};
const p = pick(['a', 'b'], o);
> p.a = 3;
3
> o
{ a: 0, b: 1, c: 2 }
> p
{ a: 3, b: 1 }

And on my previous example number 3 was pushed to original object too?

Rakesh Pai
@rakeshpai
Jan 19 12:12
That's just regular JS behaviour. Strings, numbers, boolean, etc, the primitive types are always copied by value. Arrays, objects, etc. - the complex types are copied by reference. In your case, the array was copied by reference, so it's the same reference in both cases. However, setting p.a to 3 is just setting the value to a primitive.
If you wanted a deep copy you can do:
const p = compose(pick(['a', 'b']), clone)(o)
ataskine
@ataskine
Jan 19 12:15
Actually that might not work because clone might return a references too: "Creates a deep copy of the value which may contain (nested) Arrays and Objects, Numbers, Strings, Booleans and Dates. Functions are assigned by reference rather than copied"
Ahh, true, that works
Thanks :)
Ben Briggs
@ben-eb
Jan 19 12:18
You're welcome