These are chat archives for ramda/ramda

25th
Oct 2018
Rishabh26121991
@Rishabh26121991
Oct 25 2018 04:59
how can i do that ?
Riku Tiira
@rikutiira
Oct 25 2018 07:48
R.pipe(R.filter(R.propSatisfies(Boolean, ‘household_id’), R.length)
that assumes household_id cannot be 0 though, otherwise you can do R.propSatisfies(R.complement(R.isNil)) for example
Andreas Herd
@mendrik
Oct 25 2018 09:07
onSelect={(e) => change(R.uniq(R.append(R.path(dataPath, e), values)))} how can I remove the (e) => bit without using R.__
Riku Tiira
@rikutiira
Oct 25 2018 09:12

if you want to do it 100% point-free style:

R.pipe(R.path(dataPath), R.flip(R.append)(values), R.uniq, change) // v1
R.pipe(R.path(dataPath), R.of, R.concat(values), R.uniq, change) // v2

In case of v1, I’d rather use R.__ than R.flip though

Andreas Herd
@mendrik
Oct 25 2018 09:16
well I use typescript which doesn't work with that
but kiitos, I think v2 will do nicely
Rishabh26121991
@Rishabh26121991
Oct 25 2018 16:17
How to use ramda in client side js application.
Brad Compton (he/him)
@Bradcomp
Oct 25 2018 16:18
@Rishabh26121991 Do you mean without some sort of build step?
Rishabh26121991
@Rishabh26121991
Oct 25 2018 16:19
I mean I have simple html file and on submit form button I am calling JS file and I want to use ramda in that js file .
Brad Compton (he/him)
@Bradcomp
Oct 25 2018 16:20
You can use a CDN: <script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>
Or else download a copy and host it locally.
Rishabh26121991
@Rishabh26121991
Oct 25 2018 16:21
This will go in html file and how do i use ramda in js file from this html ?
Brad Compton (he/him)
@Bradcomp
Oct 25 2018 16:22
It will go in above your script in the HTML, then you can reference Ramda functions with R.
i.e. R.map(R.inc, [1,2,3]) // => [2,3,4]
Rishabh26121991
@Rishabh26121991
Oct 25 2018 16:25
it is giving error
file://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js net::ERR_FILE_NOT_FOUND
Brad Compton (he/him)
@Bradcomp
Oct 25 2018 16:26
remove file from the beginning
swap it with https
Rishabh26121991
@Rishabh26121991
Oct 25 2018 16:29

In the html, I am using this
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.17.1/ramda.min.js"></script>

in the console log it is giving error:
file://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js net::ERR_FILE_NOT_FOUND

I dont know from where file is coming.
Brad Compton (he/him)
@Bradcomp
Oct 25 2018 16:30
try src="https://..."
Rishabh26121991
@Rishabh26121991
Oct 25 2018 16:31
Great. It works
Thanks a lot.
Brad Compton (he/him)
@Bradcomp
Oct 25 2018 16:31
:+1:
Rishabh26121991
@Rishabh26121991
Oct 25 2018 17:44

var censusMembers = Object.freeze([
{
id: 1,
name: 'Bob'
}, {
id: 2,
name: 'Sue'
}, {
id: 3,
name: 'Mary',
household_id: 2
}, {
id: 4,
name: 'Elizabeth',
household_id: 6
}, {
id: 5,
name: 'Tom'
}, {
id: 6,
name: 'Jill'
}, {
id: 7,
name: 'John',
household_id: 6
}
]);

In this array, A dependent can be determined by the presence of a household_id. The household_id is a reference to the ID of the employee that that member is a depended of (ex in the censusMembers list 'Mary' is a dependent of 'Sue')

How to build a function using ramda that returns a new array with dependents mapped onto member. for example:
output should be
"""[{
"id": 1,
"name": "Bob",
"dependents": []
}, {
"id": 2,
"name": "Sue",
"dependents": [{
"id": 3,
"name": "Mary",
"household_id": 2
}]
}, {
"id": 5,
"name": "Tom",
"dependents": []
}, {
"id": 6,
"name": "Jill",
"dependents": [{
"id": 4,
"name": "Elizabeth",
"household_id": 6
}, {
"id": 7,
"name": "John",
"household_id": 6
}]"""

Brad Compton (he/him)
@Bradcomp
Oct 25 2018 17:47
@Rishabh26121991 You can format code by encasing it in three backtics ``` code ```
Brad Compton (he/him)
@Bradcomp
Oct 25 2018 17:55
Can people with a household_id also have dependents?
Rishabh26121991
@Rishabh26121991
Oct 25 2018 18:01
No they can not be because they are dependent
Brad Compton (he/him)
@Bradcomp
Oct 25 2018 18:03
I would look at using groupBy so separate everything out by household_id, then map over the top level members to pull in their dependents
Rishabh26121991
@Rishabh26121991
Oct 25 2018 18:21
I didnt get.
Brad Compton (he/him)
@Bradcomp
Oct 25 2018 18:29
groupBy(propOr(0, 'household_id'), censusMembers) will give you an object where the keys are the household IDs and the values are a list of the objects with that houselhold ID, and all the ones without a household ID will be under 0
So then you can map over all the ones under 0, associating their dependents using the ID as a key to the object you created in step one
Rishabh26121991
@Rishabh26121991
Oct 25 2018 18:51

After using this R.groupBy(R.propOr(0,'household_id'));

I am getting this

{"0":[{"id":1,"name":"Bob"},{"id":2,"name":"Sue"},{"id":5,"name":"Tom"},{"id":6,"name":"Jill"}],"2":[{"id":3,"name":"Mary","household_id":2}],"6":[{"id":4,"name":"Elizabeth","household_id":6},{"id":7,"name":"John","household_id":6}]}

But how to append dependents key in {1,2,and 5 } ids

Brad Compton (he/him)
@Bradcomp
Oct 25 2018 18:53
What have you tried?
Rishabh26121991
@Rishabh26121991
Oct 25 2018 18:55
R.reduce((a, c) => ('household_id' in c) + a, 0, censusMembers)
Brad Compton (he/him)
@Bradcomp
Oct 25 2018 19:59

So we have an object:

households {
[household_id]: [Member]
}

And we know that households[0] is our top level.

So given that, we can get the dependents for a particular id by

houleholds[id] || [];

So then we can do that for all the top levels

map(/ get and attach households/, households[0]);

so then we need to define that function.
  1. get the dependents
  2. merge with the existing member
const getAndAttach = (member) => merge(member, getDependents(member.id));

const getDependents = id => ({dependents: propOr([], id, households)});
All together:
const households = groupBy(propOr(0, 'household_id'), members);

const result = map(getAndAttach, households[0]);
Ideally we would pull in the households array as a parameter, but this works for now
Rishabh26121991
@Rishabh26121991
Oct 25 2018 20:27
@Bradcomp
Great, it works Thank you so much.