These are chat archives for ramda/ramda

5th
Dec 2016
Johnny Hauser
@m59peacemaker
Dec 05 2016 02:53
Is there a spec for JavaScript futures that people should follow when implementing them?
And moreover, so that we can have isFuture()
Aldwin Vlasblom
@Avaq
Dec 05 2016 09:25
@m59peacemaker What you're really asking is whether there is a specification for "unpacking" monadic containers. Such a specification does exist. It's Promise/A+ and under this specification, we cannot make other useful Monads that share API's such as map. In FantasyLand, the "unpack" part is the only piece where implementations --and thereby use-cases-- for different Monads are allowed to differ. That said, there is discussion about a generic way of unpacking containers via catamorphism fantasyland/fantasy-land#154 and some related discussion fantasyland/fantasy-land#185 but it goes somewhat beyond my understanding.
Examples of different ways of unpacking, as an indication that this is where the usefulness lives:
maybe :: b -> (a -> b) -> Maybe a -> b
either :: (a -> c) -> (b -> c) -> Either a b -> c
fork :: (a -> ()) -> (b -> ()) -> Future a b -> () -> ()
evalState :: a -> State a b -> b
Aldwin Vlasblom
@Avaq
Dec 05 2016 09:35

All of the above "unpacking" functions have to know about the internals of the data-structure they are given for them to work, you can't use fork to unpack an Either, for example. It would be a shame to write specifications for them*, because that limits the amount of possible Monads we can make to the amount of specifications that exist.

* Opinion

Martin Lechner
@martinlechner1
Dec 05 2016 09:35
@ram-bot
R.pipe(dissoc('a'), dissoc('b')({a: 1, b: 2, c:3})
Is there a way to write such stuff in a better way?
Aldwin Vlasblom
@Avaq
Dec 05 2016 09:36
R.omit
Martin Lechner
@martinlechner1
Dec 05 2016 09:36
thanks :-)
Slađan Ristić
@sladiri
Dec 05 2016 10:33
Hi, I just tried intersectionWith and is it possible to specify which list the item should be taken from for the result? I think it always takes the one from the shorter list?
Aldwin Vlasblom
@Avaq
Dec 05 2016 10:34
@sladiri Why does it matter?
Slađan Ristić
@sladiri
Dec 05 2016 10:35
@Avaq, I have lists of objects, and I want to compare by a property. But one list does not have the version of the object I want in the result.
var a = [
  {foo: 42, bar: 666},
  {foo: 123},
];
var b = [
  {foo: 42},
];

R.intersectionWith(R.eqBy(R.prop('foo')), a, b);
// I would like to have {foo: 42, bar: 666} in the result.
Aldwin Vlasblom
@Avaq
Dec 05 2016 10:39
I suppose intersection implies that you are finding equal items, and it therefore shouldn't matter which list the items are taken from.
I'm curious why the lookup list is the shorter one though.
That makes the function un-usable for this case.
Slađan Ristić
@sladiri
Dec 05 2016 10:44
I think I saw that it was optimised and that is why it takes always values from the shorter list. But yes, the ordinary intersection may assume that the elements are identical, but I though about using the function for such a situation. :)
I can concat one list with R.repeat to get a result for now.
Aldwin Vlasblom
@Avaq
Dec 05 2016 10:46
Or you can just filter list a.
Slađan Ristić
@sladiri
Dec 05 2016 10:48
You mean filter and find in order to compare, correct? I think I like this better :)
Aldwin Vlasblom
@Avaq
Dec 05 2016 10:51
@ram-bot
var a = [
  {foo: 42, bar: 666},
  {foo: 123},
];
var b = [
  {foo: 42},
];
filter(x => any(eqProps('foo', x), b), a)
ram-bot
@ram-bot
Dec 05 2016 10:51
[ { foo: 42, bar: 666 } ]
Slađan Ristić
@sladiri
Dec 05 2016 10:56
@Avaq Thank you, will try any, I always forget about that one.
Tushar Mathur
@tusharmath
Dec 05 2016 11:09
Hi, I am facing this weird issue with TS
Error:(38, 29) TS2339:Property '__' does not exist on type 'Static'.
I am trying to use the Ramda placeholder in Typescript and getting the above error
Tushar Mathur
@tusharmath
Dec 05 2016 11:15
const mergeWith = R.merge(R.__)
//                          ^^
Here is where I am getting the error
Alastair Hole
@afhole
Dec 05 2016 12:28
Is there anything in ramda to help with creating instances? Would be nice to make Immutable Record instantiation (new) point-free
Tushar Mathur
@tusharmath
Dec 05 2016 12:35
@afhole how about using Object.create instead?
Alastair Hole
@afhole
Dec 05 2016 12:40
@tusharmath Ah good call - new Thing() is the same as Object.create(Thing) right? So I could curryN(1, Object.create(Thing))({a:1})?
Or curryN(2, Object.create)( Thing )( {a:1} )
Tushar Mathur
@tusharmath
Dec 05 2016 12:42
You need to pass the prototype
to the create function
Alastair Hole
@afhole
Dec 05 2016 12:43
Oh damn
So I'd still have to write code to extract the prototype from an object?
I want to instantiate an ImmutableJS Record
Alastair Hole
@afhole
Dec 05 2016 12:49
I'm baffled now
Maybe pointful isn't so bad
Tushar Mathur
@tusharmath
Dec 05 2016 13:04
How does the pointless version look like?
@afhole
Aldwin Vlasblom
@Avaq
Dec 05 2016 13:10
R.construct
Aldwin Vlasblom
@Avaq
Dec 05 2016 13:10
@afhole ^
Alastair Hole
@afhole
Dec 05 2016 14:21
Amazing! I tried all variations of 'instantiate', 'new' etc
Thanks @Avaq - that is directly equivalent to new SomeThing() right?
Aldwin Vlasblom
@Avaq
Dec 05 2016 15:13
@afhole Doing new Something() equals Something.call(Object.create(Something.prototype)) equals R.construct(Something)()
LeonineKing1199
@LeonineKing1199
Dec 05 2016 16:13

How does the pointless version look like?

Ha! Best slip-up I've seen yet. :P

Alastair Hole
@afhole
Dec 05 2016 16:13
@Avaq Very helpful, thank you :)
@Avaq Interesting implementation of constructN - supports arity <= 10
I guess you've got bigger problems if you have constructors with arity >10 than constructN supporting it...
LeonineKing1199
@LeonineKing1199
Dec 05 2016 16:22
Lol nvm, I was wrong :P
Looks like the limit is hard-coded into Ramda itself: https://github.com/ramda/ramda/blob/v0.22.1/src/internal/_arity.js
LeonineKing1199
@LeonineKing1199
Dec 05 2016 16:29
curryN is dependent on that file, for instance. That's strange. I'm looking at their implementation of _curryN, not be confused with curryN :P
Aldwin Vlasblom
@Avaq
Dec 05 2016 16:31
The reason some functions in Ramda can only go up to ten arguments is because in order to create a function with the right length-property, one must define the function accepting that amount of arguments. Ramda uses the _arity helper to create these functions, and the arity helper doesn't go further than defining a function with 10 input arguments.
LeonineKing1199
@LeonineKing1199
Dec 05 2016 16:32
Hmm...
I'm not seeing why that's a problem off the top of my head.
Aldwin Vlasblom
@Avaq
Dec 05 2016 16:33
@ram-bot
const f = (a, b, c) => c;
f.length = 10;
f.length
ram-bot
@ram-bot
Dec 05 2016 16:33
3
Aldwin Vlasblom
@Avaq
Dec 05 2016 16:33
Cannot override .length
LeonineKing1199
@LeonineKing1199
Dec 05 2016 16:34
That only seems problematic if you limit your implementation to relying solely on .length
Aldwin Vlasblom
@Avaq
Dec 05 2016 16:34
And Ramda wants curried functions to report the correct total arity, but really a curried function always has a length of 1.
So they need to pass it into _arity, to decorate the curried function with one reporting the correct .length
@ram-bot add.length
ram-bot
@ram-bot
Dec 05 2016 16:35
2
Aldwin Vlasblom
@Avaq
Dec 05 2016 16:35
@ram-bot add(1).length
ram-bot
@ram-bot
Dec 05 2016 16:35
1
Aldwin Vlasblom
@Avaq
Dec 05 2016 16:36
That's what it achieves.
Shows how many arguments are left.
LeonineKing1199
@LeonineKing1199
Dec 05 2016 16:36
Interesting. I'd have to try my own implemention
Actually, if you look at the rules for function length in JS, you'd have to be a madman to write code that depended on it O_o
If you use awesome things like default values, it ruins the count
It also doesn't detect variadic functions either.
Aldwin Vlasblom
@Avaq
Dec 05 2016 16:39
Yeah. And since all Ramda's functions are variadic (because of the way curried functions still allow you to pass multiple args at once), they need the _arity helper to report the right arity. It's basically a work-around the fact that JS cannot determine the length of variadic functions.
LeonineKing1199
@LeonineKing1199
Dec 05 2016 16:40
The hard limit of 10 is pretty, well, limiting
Aldwin Vlasblom
@Avaq
Dec 05 2016 16:44
If you really have that many args, you will probably not feel the pain of doing curry((a,b,c,d,e,f,g,h,i,j) => curry((k,l,m,n,o,p) => { ... }))
Though one might argue that reporting the right arity is not worth the limit.
LeonineKing1199
@LeonineKing1199
Dec 05 2016 16:46
Well, it's more about creating an arbitrarily curry-able function for use with applicatives.
Aldwin Vlasblom
@Avaq
Dec 05 2016 16:47
Perhaps we should just report 10 as being "any function with an arity over 9.000".
Alastair Hole
@afhole
Dec 05 2016 17:03
Ah so that's the only control one has over .length - setting the actual physical number of arguments in the definition
Alastair Hole
@afhole
Dec 05 2016 17:19
Here's the final version FWIW, for turning POJOs into ImmutableJS Records
const toRecord = compose( construct(__), fromJS);
Travis LaDuke
@laduke
Dec 05 2016 17:30
Is there a thing like evolve that leaves behind the untransformed properties? I'm doing pick then evolve.
Also, looking for any examples of cleaning up deeply nested json (just source, not a fancy article or anything).
Travis LaDuke
@laduke
Dec 05 2016 18:34
whoa
Alex Gordon
@alexggordon
Dec 05 2016 19:26
Need some advice if at all possible: I have an object, that, if a key exists, I need to replace the value of that key with a different value from a different object. Essentially, I need if obj1[a]: obj1[a] = obj2[obj1[a]]
I was trying to do this in Ramda, and I'd come up with R.evolve({ obj1: R.flip(R.prop)(obj2) }, obj1)), but this doesn't seem to work
LeonineKing1199
@LeonineKing1199
Dec 05 2016 19:31
Just stick with what you have
That's a one-liner in vanilla JS. Anything more than that ventures into over-engineering
Alex Gordon
@alexggordon
Dec 05 2016 19:33
The reason I wanted to use ramda is I have multiple transformations to make
The other ones are already working, but I can't seem to get this one to work
LeonineKing1199
@LeonineKing1199
Dec 05 2016 19:34
An efficient usage of Ramda would be to write your simple function here and then figure out how to fold that into your infastructure.
For example, you could map your function over an object.
przemoli
@przemoli
Dec 05 2016 21:09
If I want something like (a(b+1))/2 its fairly easy to do with converge. What if I need (n(n+1))/2 ? How to take that n and "split it" into two values? At worst I can write myfunction(n, n) but that is a bit confusing (and will not give expected results unless first argument equals second one!).
Urmas Talimaa
@urmastalimaa
Dec 05 2016 21:19
If you are looking for a function (a -> a -> b) -> (a -> b), then that's the W (warbler) combinator https://github.com/fantasyland/fantasy-birds/blob/master/README.md#warbler--a---a---b---a---b
Denis Stoyanov
@xgrommx
Dec 05 2016 21:49
@urmastalimaa this is monadic join