These are chat archives for ramda/ramda

3rd
Jun 2016
Lewis
@6ewis
Jun 03 2016 02:07
does ramda have anything that runs a function only and only if the object has the fcts implemented
Travis LaDuke
@laduke
Jun 03 2016 06:29
got a bunch of stuff correct on the first try in the last hour. it can only mean i'm working with a some kind of magic library.
Scott Sauyet
@CrossEye
Jun 03 2016 12:04
@laduke I'm convinced that even with a magical library, any such occurrences are a matter of luck.
Brad Compton (he/him)
@Bradcomp
Jun 03 2016 14:13

@RPallas92

I’m lifting a function to operate with folktale/data.task. Is it possible is there an error with lifting if the task rejects?

Can you post an example? If the Task rejects you should end up in the rejection half of task.fork

James Grayling
@jamesgrayling
Jun 03 2016 14:53
What’s the function to take multiple props from an object?
Brad Compton (he/him)
@Bradcomp
Jun 03 2016 14:54
R.pick
James Grayling
@jamesgrayling
Jun 03 2016 14:54
cheers
Brad Compton (he/him)
@Bradcomp
Jun 03 2016 14:54
R.props
Brad Compton (he/him)
@Bradcomp
Jun 03 2016 14:54
props will give you a list of the values, pick will give you a new object
Lewis
@6ewis
Jun 03 2016 18:08
This message was deleted
@ram-bot const object = {example: "test", id: 1}; R.keys(object)[0]
ram-bot
@ram-bot
Jun 03 2016 18:08
'example'
Lewis
@6ewis
Jun 03 2016 18:09
shortcut for a single object key?
@ram-bot const object = {example: "test"}; R.keys(object)[0]
I wish we had object.key, object.value
LeonineKing1199
@LeonineKing1199
Jun 03 2016 18:12
What do you mean by a "shortcut for a single object key"?
It looks like you're just extracting the first key of an object.
Lewis
@6ewis
Jun 03 2016 18:12
yes
do we have a shortcut for that
LeonineKing1199
@LeonineKing1199
Jun 03 2016 18:13
Maybe. Maybe not. But what you have is perfectly fine as is.
Lewis
@6ewis
Jun 03 2016 18:20
is there a function to deep merge? nested objects
LeonineKing1199
@LeonineKing1199
Jun 03 2016 18:21
Do you mean something like this? http://goo.gl/Usxh9G
It seems like merge is already pretty decently implemented when it comes to nested props.
Lewis
@6ewis
Jun 03 2016 19:00
@LeonineKing1199 http://goo.gl/2fnxZP like this
maybe @Bradcomp knows a good way to handle it
Lewis
@6ewis
Jun 03 2016 19:07
This message was deleted
Brad Compton (he/him)
@Bradcomp
Jun 03 2016 19:14
@6ewis If I were to handle it in the general sense I would probably use merge with recursion. If you know the shape of your data you could use lenses though: http://goo.gl/VldyRR
Lewis
@6ewis
Jun 03 2016 19:16
@Bradcomp ouhla it seems complicated. I have to familiarize myself with those functions
Brad Compton (he/him)
@Bradcomp
Jun 03 2016 19:17
Yeah, lenses take some getting used to. That was just a first shot, and I might be inclined to write a mergePath function if I had to do it often enough.
Lewis
@6ewis
Jun 03 2016 19:17
what do lenses mean?
It looks like an functions that has a getter and a setter attached to an object
Brad Compton (he/him)
@Bradcomp
Jun 03 2016 19:19
Yeah, exactly. Which doesn't seem that useful up front, but it lets you immutably modify pieces of your data
I won't typically reach for them unless I need to use over, which lets you apply a function to a piece of your data structure.
Gabe Johnson
@gabejohnson
Jun 03 2016 19:24
@Bradcomp do you know if there’s been any work on a zipper implementation for Ramda?
Brad Compton (he/him)
@Bradcomp
Jun 03 2016 19:24
//Lets us get and set the value at obj.prop0.prop1.prop2
const innerObjLens = lensPath(['prop0', 'prop1', 'prop2']);

//gets the data you would like to merge from the first object
let viewo1 = view(innerObjLens, o1);

//applies the function merge(viewo1) to the result of getting the view from o2, then sets the result back into the data structure
over(innerObjLens, merge(viewo1), o2)
@gabejohnson I have no idea. I'm not familiar with zipper, nor am I affiliated with Ramda
Gabe Johnson
@gabejohnson
Jun 03 2016 19:26
zippers are sort of duals of lenses
At least you could conceivably create a zipper from a lens and a tree or create a lens from a zipper
Lewis
@6ewis
Jun 03 2016 19:34
@Bradcomp http://goo.gl/5OlAkV , how about my solution?
Brad Compton (he/him)
@Bradcomp
Jun 03 2016 19:35
Looks fine to me
Lewis
@6ewis
Jun 03 2016 19:36
@Bradcomp ill stick to it, it seems more readable than with lenses. thank you!
Brad Compton (he/him)
@Bradcomp
Jun 03 2016 19:36
Cool!
Lewis
@6ewis
Jun 03 2016 19:39
@Bradcomp is there any advantages i'm unware of with your solution?
Brad Compton (he/him)
@Bradcomp
Jun 03 2016 19:47

I mean, it depends on what you are trying to do. The lens gives you the ability to reach into a data structure. Once there you can get the value, set the value, or set the value to the result of a function. This is a nice tool to have if you are going to be doing manipulations on a subtree in your data.

If you just want the ability to dynamically assign values to nested paths, assocPath is more focused on what you're doing. It has the benefit of simplicity, and is probably more clearly understandable at a glance. I liked that I was able to use the same code to both get the value out of the one object and merge it into the other object, which is something assocPath won't do for you.

There is definitely some conceptual overhead associated with lenses, and there is usually a non-lens solution you can use for places where you could use a lens. On the other hand, like with most things in FP, it's a nice way to abstract a pattern that comes up often when dealing with complicated data structures, so it's a nice thing to be able to reach for if you need it.

Brad Compton (he/him)
@Bradcomp
Jun 03 2016 20:04
@gabejohnson That's interesting. I'll have to look into that further when I have time.
Gabe Johnson
@gabejohnson
Jun 03 2016 20:05
It’s my data structure of the month
Lewis
@6ewis
Jun 03 2016 20:15
@Bradcomp thanks for your typical clear explanation
@Bradcomp here is what I ended up doing - https://gist.github.com/6ewis/30339f06c20544fa3132b256aa8830dc - hope the comments help
Scott Sauyet
@CrossEye
Jun 03 2016 20:17
@gabejohnson: I haven't heard of anyone working on zippers, although I'd love to see it done.
Gabe Johnson
@gabejohnson
Jun 03 2016 20:21
@CrossEye: actually, it looks like someone has
sort of
I had glanced at the project a couple of weeks ago, but hadn’t looked at the implementation until I started talking about it today
Scott Sauyet
@CrossEye
Jun 03 2016 20:33
Oh cool. I'll have to look more closely. Thanks for the heads-up.
James Grayling
@jamesgrayling
Jun 03 2016 20:40

I’m trying to grok lenses. I want to turn this:

{ a: [ 7, 8, 9 ]
, b: [ 10 ]
, c: [ 1, 2, 3, 4, 5, 6 ]
}

Into this:

{ a: [ 7, 8, 9 ]
, b: [ 10 ]
, c: [ 1, 2, 3, 4, 5, 6 ]
}

And it’s nested in an object. And I’m hoping lenses can help make my code a bit more straightforward.

oops
Lewis
@6ewis
Jun 03 2016 20:41
@jamesbrown0 I believe you copied pasted the wrong thing for your output
James Grayling
@jamesgrayling
Jun 03 2016 20:41
into this:
{ a: [ 4, 5, 6 ]
, b: [ 10, 7, 8, 9 ]
, c: [ 1, 2, 3 ]
}
:)
Lewis
@6ewis
Jun 03 2016 20:48
This message was deleted
ram-bot
@ram-bot
Jun 03 2016 20:48
object is not defined
Lewis
@6ewis
Jun 03 2016 20:49
@ram-bot
const object = { a: [ 7, 8, 9 ]
, b: [ 10 ]
, c: [ 1, 2, 3, 4, 5, 6 ]
}

var xLens = (x) => R.lens(R.prop(x), R.assoc(x));

const object1 = R.set(xLens('a'), [4,5,6], object)
const object2 = R.set(xLens('b'), [ 10, 7, 8, 9 ], object1)
R.set(xLens('c'), [ 1, 2, 3 ], object2)
ram-bot
@ram-bot
Jun 03 2016 20:49
{ a: [ 4, 5, 6 ], b: [ 10, 7, 8, 9 ], c: [ 1, 2, 3 ] }
James Grayling
@jamesgrayling
Jun 03 2016 20:50
I suppose I wasn’t very clear. All the numbers in my example represent a unique object which needs to be moved around in the data structure
I’m stuggling to do it with Ramda functions without it being a mess
LeonineKing1199
@LeonineKing1199
Jun 03 2016 20:52
You might be better off describing what your problem is first.
James Grayling
@jamesgrayling
Jun 03 2016 20:52
The goal is: Move a to b. Take 3 from c. Put them in a.
Concatenate a onto b. Take 3 from c. Put them in a.
The second line I wrote, not the first one. I corrected myself
LeonineKing1199
@LeonineKing1199
Jun 03 2016 21:01
That's a weird thing to want to do. What's the underlying motivation? Otherwise, you can construct code that literally does what you want in a "dumb" manner.
James Grayling
@jamesgrayling
Jun 03 2016 21:01
The motivation is to learn what I can do with ramda I suppose
I writing an app with Snabbdom and Elm architecture. It’s an action reducer
Raine Virta
@raine
Jun 03 2016 21:02
dumb is not bad if it's more readable
LeonineKing1199
@LeonineKing1199
Jun 03 2016 21:03
:plus1:
James Grayling
@jamesgrayling
Jun 03 2016 21:05
cool
LeonineKing1199
@LeonineKing1199
Jun 03 2016 21:06
There might be better ways of learning Ramda that aren't contrived object manipulations.
And part of learning any language or tool or library is also knowing when not to use it.
I would recommend starting a project and implementing it using Ramda.
Raine Virta
@raine
Jun 03 2016 21:09
i totally understand the desire to figure out if there's a better way to do something with a thing you are learning
James Grayling
@jamesgrayling
Jun 03 2016 21:10
I am. I’m making Solitaire. My example was contrived because the details were irrelevant. Thanks for your advice. I’m just trying to make it simple and find out what tools I can use to achieve that.
LeonineKing1199
@LeonineKing1199
Jun 03 2016 21:12
The details are never irrelevant!
Programming is about solving problems and a complete description can yield an implementation you would've never thought of!
Don't tether yourself to the abstractions that you created. Problems can be solved by many different abstractions and the beauty of gitter and this chatroom in particular is all the different ways you can solve something.
Gabe Johnson
@gabejohnson
Jun 03 2016 21:27
@ram-bot
const o = { 
  a: [ 7, 8, 9 ]
, b: [ 10 ]
, c: [ 1, 2, 3, 4, 5, 6 ]
};

const aLens = R.lensProp('a');
const bLens = R.lensProp('b');
const cLens = R.lensProp('c');

R.pipe(
  R.pipe(
    R.view(aLens),
    R.flip(R.concat),
    R.over(bLens)
  )(o),
  R.pipe(
    R.view(cLens),
    R.takeLast(3),
    R.set(aLens)
  )(o),
  R.over(cLens, R.dropLast(3))
)(o);
ram-bot
@ram-bot
Jun 03 2016 21:27
[10] does not have a method named "concat"
James Grayling
@jamesgrayling
Jun 03 2016 21:28
wow. nice work
Gabe Johnson
@gabejohnson
Jun 03 2016 21:30
@jamesbrown0: still learning. Those inner pipes smell funny
Brad Compton (he/him)
@Bradcomp
Jun 03 2016 21:31
R.lensProp
Brad Compton (he/him)
@Bradcomp
Jun 03 2016 21:31
Does the same thing as xLens there
James Grayling
@jamesgrayling
Jun 03 2016 21:33
I didn’t know you could edit messages!
Gabe Johnson
@gabejohnson
Jun 03 2016 21:33
yes but it won’t re-eval
@Bradcomp: thx
Brad Compton (he/him)
@Bradcomp
Jun 03 2016 21:45
Also, concat doesn't work with ram-bot
@ram-bot concat([4], [5])
ram-bot
@ram-bot
Jun 03 2016 21:45
[4] does not have a method named "concat"
Denis Stoyanov
@xgrommx
Jun 03 2016 22:03
@ram-bot
R.concat([4], [5])
ram-bot
@ram-bot
Jun 03 2016 22:03
[4] does not have a method named "concat"
Denis Stoyanov
@xgrommx
Jun 03 2016 22:03
R.concat
R.map