Reputation: 779
How can I change this code:
map(user => assoc('isAdult', isAdult(user), user)
to function pointfree (using Ramda). I was thinking how to do it with useWith, but can't find working implementation.
Any ideas?
import { pipe, prop, gte, __, map, assoc, sortWith, descend } from 'ramda'
const isAdult = pipe(
prop('age'),
gte(__, 18)
)
const func = pipe(
map(user => assoc('isAdult', isAdult(user), user)),
sortWith([
descend(prop('isAdult')),
descend(prop('age')),
])
)
export default func
EDIT (more info):
I want to create a function that returns array of objects extended by the isAdult
key. If the user is more than 18 years old, we set the flag to true
.
In addition, the function should return users sorted by the flag isAdult
and then sorted by the key age
.
Sample data:
const data1 = [
{
name: 'Cassidy David',
email: '[email protected]',
id: 'FC92BF1E-A6FD-E5C1-88AB-183BD1BC59C5',
position: 1,
age: 53,
created_at: '2017-04-07'
},
{
name: 'Gwendolyn Edwards',
email: '[email protected]',
id: '00000001-ED9D-3A88-0D3C-07A148FD43ED',
position: 2,
age: 10,
created_at: '2016-05-21'
},
Upvotes: 2
Views: 361
Reputation: 50787
I think it's debatable whether trying to make this point-free is useful.
This is quite readable as is:
map(user => assoc('isAdult', isAdult(user), user)),
Point-free is a great technique that can sometimes enhance readability. But if it doesn't, I wouldn't bother with it.
That said, if you do want to try point-free, chain
is probably most helpful. Note how chain works for functions: chain(f, g)(x) //=> f(g(x), x)
.
So you can write your relevant part as
map(chain(assoc('isAdult'), isAdult))
You can see this in action on the Ramda REPL.
Upvotes: 3
Reputation: 47279
Alright I got a solution, although it is pretty debatable whether this is an improvement in clarity over what you have already:
const newFields = map(pipe(isAdult, objOf('isAdult')))(data1)
zipWith(merge, data1, newFields)
So I'm doing one extra pass where I generate a list with objects that just have the new key and merge them together, and then I combine the lists.
I could not get a solution where you do one pass without avoiding an arrow function like the one you did.
EDIT: Scott Sauyet's solution addresses the problem quite well actually
Upvotes: 3