Exact version of the function using Ramda.js
I am trying to make the following pointfree function. I'm not sure how I can pass the argument to the inner function. I am using Ramda.js, but I think the concept is more general than this. Here is the code I have.
search = function(id) {
return find(propEq('id', id), items)
}
Here you will notice that the parameter id
is being passed to the inner function propEq
. This is the part I'm not sure about.
source to share
The question is more general than Ramda, but Ramda has several functions to make things like this easier, especially useWith
and converge
.
It can be written without dots with the useWith
following:
var search = useWith(find, propEq('id'), identity);
search(2, items); //=> {id: 2}
You can see it in action at the Ramda REPL .
source to share
With a little bit of mastering, you can get the free version, but the auto career gave problems, so I had to duplicate some functions by doing the functions manually. This is one liner:
search = compose(flip(find)(items), propEq('id'))
Using ES6 syntax for brevity:
var {compose} = R
var find = f => xs => R.find(f, xs)
var propEq = p => x => R.propEq(p, x)
var flip = f => a => b => f(b)(a)
// Example:
var items = [{id: 1}, {id: 2}, {id: 3}]
// point-full
var search = function(id) {
return find(propEq('id')(id))(items)
}
console.log(search(2))
// point-free
search = compose(flip(find)(items), propEq('id'))
console.log(search(2))
This is a point, but.
source to share
You just need to do it first. Then you can give it an ID first, and the items will output the function that precedes it.
Example:
const search = R.curry((id, items) => R.find(R.propEq('id', id), items));
const testSearch = R.pipe(
R.identity(() => [{id: '123'}, {id: 'asdf'}]),
search('123')
);
testSearch(); // { id: '123' }
source to share