These are chat archives for ramda/ramda

12th
Jan 2018
isidorosp
@isidorosp
Jan 12 2018 13:16
Hi everyone. I've got an array of objects and I'm trying to keep/pick only certain keys from each object (e.g. the 'id' key). I know I can R.pick(['id'], ...) from a specific array element, but how do I do this over the whole array?
Henrik Raitasola
@henrikra
Jan 12 2018 13:17
R.map(R.pick(['id']))
isidorosp
@isidorosp
Jan 12 2018 13:18
that easy :/
Henrik Raitasola
@henrikra
Jan 12 2018 13:19
did you want it to be more difficult? :D
isidorosp
@isidorosp
Jan 12 2018 13:19
was tryign with foreaches and whatnot
and it wasn't happening :D
ok. and just append my arary to the end of that right? so basically R.map(R.pick(['id']))(myArray)
Henrik Raitasola
@henrikra
Jan 12 2018 13:20
yeah that is one way you can do that :+1:
forEach is not usually very functional programming
isidorosp
@isidorosp
Jan 12 2018 13:22
indeed
Are
@are1000
Jan 12 2018 13:22
JavaScript is not usually very functional programming language ¯_(ツ)_/¯
Henrik Raitasola
@henrikra
Jan 12 2018 13:22
true xD
but you can try your best
isidorosp
@isidorosp
Jan 12 2018 13:24
hahah
Part of my problem is I keep thinking not functional programming :( Now I want to take my resulting data and basically "flatten it" (i.e. an array of objects with 1 key, i just want an array with those values)
sorry meant instead, not "i.e."
Henrik Raitasola
@henrikra
Jan 12 2018 13:31
when you want help it is easier if you show data that goes in and data that should come out :D
isidorosp
@isidorosp
Jan 12 2018 13:33
sure
[ { id: "BCH-USD" }, { id: "LTC-EUR" }, ] should become'[BCH-USD', 'LTC-EUR']
Are
@are1000
Jan 12 2018 13:35
Yep, thats exactly what @henrikra said earlier: R.map(R.pick(['id']))
Henrik Raitasola
@henrikra
Jan 12 2018 13:35
not exactly
because pick returns object
isidorosp
@isidorosp
Jan 12 2018 13:35
yeah before my data was different
Are
@are1000
Jan 12 2018 13:35
Oh, right, its prop
R.map(R.prop('id'))
Henrik Raitasola
@henrikra
Jan 12 2018 13:36
yeah
isidorosp
@isidorosp
Jan 12 2018 13:36
this is after i already slimmed the data from within the array of bojects
Henrik Raitasola
@henrikra
Jan 12 2018 13:36
but you dont need pick then at all
just use prop like @are1000 said
isidorosp
@isidorosp
Jan 12 2018 13:36
R.map(R.prop('id'),R.map(R.pick(['id']))(test1))
Are
@are1000
Jan 12 2018 13:36
Yep, just do R.map(R.prop('id'))(test1)
isidorosp
@isidorosp
Jan 12 2018 13:37
basically this is what i ended upw ith
Henrik Raitasola
@henrikra
Jan 12 2018 13:37
no need for pick in your situation xD
isidorosp
@isidorosp
Jan 12 2018 13:37
oh
interesting
Are
@are1000
Jan 12 2018 13:38
When you want to somehow transform the array M a -> M b first think about how would you transform only one element a -> b
In your case you want { id: 'BCH-USD', some: 'other', data: 42 } to become 'BCH-USD'
isidorosp
@isidorosp
Jan 12 2018 13:39
i'm a bit confused about how prop is working there. how does it know that by 'id' i'm talking about what the key is named (and not for example the value)
Are
@are1000
Jan 12 2018 13:39
The function R.prop works like this:
isidorosp
@isidorosp
Jan 12 2018 13:39
in pick at least the docs specify
Are
@are1000
Jan 12 2018 13:39
const prop = name => object => object[name]
(ofc it is more complex, but it generally works like this)
so when you do R.prop('id') it actually means object => object['id']
so when you finally pass an object into it R.prop('id')({ id: 42 }) it just extracts the field and returns 42
isidorosp
@isidorosp
Jan 12 2018 13:41
I see. Thanks!
Got any good links/resources for kind of "thinking in Ramda". Maybe I should go back to those and pour over the docs again
isidorosp
@isidorosp
Jan 12 2018 13:47
thanks
:)
Brad Compton (he/him)
@Bradcomp
Jan 12 2018 14:33
Just an FYI: map(prop(p)) is the same as pluck(p)
isidorosp
@isidorosp
Jan 12 2018 14:59
oh nice
Anirudh Mangalvedhekar
@amangalvedhekar
Jan 12 2018 15:29
does anyone have an idea why R.intersectionWith was removed from latest version
Sten-H
@Sten-H
Jan 12 2018 16:33
If I have two indexes of an array that I want to swap the values of, is there a fancy way of doing that? My solutions have been very imperative and non fancy :)
Brad Compton (he/him)
@Bradcomp
Jan 12 2018 17:06
@amangalvedhekar It was replaced
R.innerJoin
Brad Compton (he/him)
@Bradcomp
Jan 12 2018 17:06
That should do what you want it to
intersectionWith was problematic, because it would pick from the shorter of the two lists, and thus if the lists were of different types you could have undefined behavior
Sorry if that's a crappy explanation
Anirudh Mangalvedhekar
@amangalvedhekar
Jan 12 2018 17:27
@Bradcomp thanks for the information.. yeah we had bugs due to R.intersectionWith … I will try to use R.innerJoin
Brian Lee
@brian-dlee
Jan 12 2018 17:39
Does anyone have any solutions for correcting compose Property of ramda not found when using ramda with flow?
Screen Shot 2018-01-12 at 9.40.10 AM.png
functionalStoic
@functionalStoic
Jan 12 2018 21:02
@brian-dlee What does your import of Ramda look like?
Brian Lee
@brian-dlee
Jan 12 2018 21:03
It looks like import * as R from 'ramda'
It turns out I have a few methods that are not coming through. Both R.over and R.lens are having the same issues as well.
isidorosp
@isidorosp
Jan 12 2018 21:20
Hey guys. Back again :/ I'm trying to convert this array of arrays (that's also kind of flipped) to an array of objects
I had some luck using default JS reduce in getting the first object of output correct, but I'm not able to replicate it with Ramda. Any ideas?
functionalStoic
@functionalStoic
Jan 12 2018 21:24
@brian-dlee Hmm. Sorry not of any help. I’m using flow in a project as well and I hadn’t noticed the issue butI’m getting the error on a few as well. Although compose doesn’t get highlighted
isValidWallSize_js_—_designer__Workspace_.png
Brian Lee
@brian-dlee
Jan 12 2018 21:27
Okay. That looks pretty similar to my issues. I was messing with type definitions and copy/pasting definitions from other version in flow/flow-typed for ramda and I was able to get the error for compose to go away like you are seeing. It may just be that you have a slightly different version of the definitions installed in your project. Thanks for your attention. I appreciate it.
Brian Lee
@brian-dlee
Jan 12 2018 21:28
@isidorosp I'm too new to Ramda to solve that one for you in a relatively clean way. I'd love to see a nice answer through
functionalStoic
@functionalStoic
Jan 12 2018 21:29
@brian-dlee I’ve actually been doing a lot more importing like
import compose from ‘ramda/es/compose’ like they’ve recommendd in the repot but I’ve not figured out how to get any flow typing with them imported that way
isidorosp
@isidorosp
Jan 12 2018 21:29
I mean, i could do it via iterating over the array in 2 loops, but that sounds super inelegant and not the point of ramda :)
I think it's probably some combinatino of map and reduce but i'm having trouble
Brian Lee
@brian-dlee
Jan 12 2018 21:31
@JasonSooter Good to know. I haven't seen that import method. I tend to have issues with the generic naming of Ramda functions when importing by name. That way looks like it could minimize bundle size when tree shaking, though.
functionalStoic
@functionalStoic
Jan 12 2018 21:34
Yeah tree shaking is one of the main benefits of that way. But your import method is suppose to support tree shaking as well. I also like how just having the word can be really clear and simple.
@brian-dlee But if you use a bunch of methods in the same file you can end up with a long list of imports occasionally
Mike Lambert
@lax4mike
Jan 12 2018 21:41
@isidorosp you can check out R.transpose
R.transpose essentially rotates a 2d array
functionalStoic
@functionalStoic
Jan 12 2018 21:45
@lax4mike Nice editor indeed!
Alec
@alavkx
Jan 12 2018 21:45
@isidorosp
this is my best go at it
https://goo.gl/NpypDw
isidorosp
@isidorosp
Jan 12 2018 21:46
both of those are really interesting/helpful. Ultimately lax's is what I need because that's how this charting library I'm using is structuring it's data (a bit odd, yeah)
Mike Lambert
@lax4mike
Jan 12 2018 21:47
the first transpose separates the header from the data, then for each data point, pair it with the header, transpose, and run fromPairs on it to change it into an object
Alec
@alavkx
Jan 12 2018 21:47
Ahh I see now what you were going for :))
isidorosp
@isidorosp
Jan 12 2018 21:48
yeah that's the part im confused with lax...
Mike Lambert
@lax4mike
Jan 12 2018 21:48
which part?
isidorosp
@isidorosp
Jan 12 2018 21:48
where you'd doing compose (header ... data)
*youre
Mike Lambert
@lax4mike
Jan 12 2018 21:48
that's just fancy es6 destructuring
([header, ...data]) => { ... } that function is given an array, and the [header, ...data] just says, put the first item in header and put the rest in data
so header is [ "Date", "Open", "High", "Low", "Volume" ]
and data is an array of all the data, one of which looks like [ "Jan 11, 2018", "14968.20", "15018.80", "13105.90", "16,534,100,000" ]
Alec
@alavkx
Jan 12 2018 21:51
If you understand what's happening in my example (prop(0), drop(1)), it is equivalent
in a way
isidorosp
@isidorosp
Jan 12 2018 21:53
I think i see, got some reading to do
really nifty
Mike Lambert
@lax4mike
Jan 12 2018 21:53
i find is easier to follow if you inspect the output at each step
isidorosp
@isidorosp
Jan 12 2018 21:53
yeah
Mike Lambert
@lax4mike
Jan 12 2018 21:53
you can either comment out everything up until a point
or use R.tap(console.log) inside your composition
R.compose(
  R.fromPairs,
  R.transpose,
  R.tap(console.log),
  R.pair(header)
)
isidorosp
@isidorosp
Jan 12 2018 21:56
what's tap do?
Mike Lambert
@lax4mike
Jan 12 2018 21:56
it's for running side effects
it takes an argument in, runs the function you provide (console.log in our case) and returns the original argument unchanged
isidorosp
@isidorosp
Jan 12 2018 21:57
so basically it works from the bottom up
(everythign that's in compose that is)
Mike Lambert
@lax4mike
Jan 12 2018 21:57
yup
you can use R.pipe if you want to work the other way
isidorosp
@isidorosp
Jan 12 2018 21:58
wher'es R.pair come from?
I don't see it in the docs
Mike Lambert
@lax4mike
Jan 12 2018 21:58
it's there
isidorosp
@isidorosp
Jan 12 2018 21:58
oh
Mike Lambert
@lax4mike
Jan 12 2018 21:58
it's just (a, b) => [a, b]
isidorosp
@isidorosp
Jan 12 2018 21:58
iw as in wrong version for soem reason
Mike Lambert
@lax4mike
Jan 12 2018 22:01
when you're working with objects, you usually use R.fromPairs
isidorosp
@isidorosp
Jan 12 2018 22:02
ok im getting it now
Mike Lambert
@lax4mike
Jan 12 2018 22:02
so your goal is normally to make pairs, like [ ["Open", "Jan 10"], ... ]
isidorosp
@isidorosp
Jan 12 2018 22:02
the combination of what you did w/ the header and transpose is really what makes this work
Mike Lambert
@lax4mike
Jan 12 2018 22:02
yeah
isidorosp
@isidorosp
Jan 12 2018 22:03
from pairs is basically just "translating" the values from the array into the obj right?
Mike Lambert
@lax4mike
Jan 12 2018 22:03
yeah, a nested array into an object
isidorosp
@isidorosp
Jan 12 2018 22:04
when you separate header and data
how is it that only the "headerless" data is being passed into the compose?
Mike Lambert
@lax4mike
Jan 12 2018 22:04
i pass it explicitly, R.map( ...compose chain...)(data)
isidorosp
@isidorosp
Jan 12 2018 22:05
ah so data isn't all data
but what's left after you've done [header, ...data]?
Mike Lambert
@lax4mike
Jan 12 2018 22:05
right, it's missing the header
isidorosp
@isidorosp
Jan 12 2018 22:05
kk
Mike Lambert
@lax4mike
Jan 12 2018 22:05
es6 destructuring
isidorosp
@isidorosp
Jan 12 2018 22:06
and the transpose at the end
ah ok. it's actually the first thing that happens
which is how you get the header
is like doing a shift
Mike Lambert
@lax4mike
Jan 12 2018 22:07
yeah, it's kind of rotating the 2d array
put an R.tap(console.log) right above the R.transpose to see what it's doing
Alec
@alavkx
Jan 12 2018 22:09
Another thing that I like to do in the REPL.. Highlight everything you don't understand and delete it, look at the output, and re-add the functions one at a time
Similar to tap, works better for me for some reason
Mike Lambert
@lax4mike
Jan 12 2018 22:09
yeah, or comment it out
Alec
@alavkx
Jan 12 2018 22:09
^:thumbsup:
isidorosp
@isidorosp
Jan 12 2018 22:12
thanks again :)
super helpful
Mike Lambert
@lax4mike
Jan 12 2018 22:13
:thumbsup: