Oli*_*ini 2 javascript ramda.js
If I want to define a function that filters a set matching on a given value, I can write:
const { filter, equals} = R
const data = [1, 2, 3, 4, 5]
const filterDataFor = x => filter(equals(x), data);
console.log(filterDataFor(2))Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script>Run Code Online (Sandbox Code Playgroud)
I thought that I might also be able to write this in the form of:
const { filter, equals, __ } = R
const data = [1, 2, 3, 4, 5]
const filterDataFor = filter(equals(__), data)
console.log(filterDataFor(2))Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script>Run Code Online (Sandbox Code Playgroud)
But evidently not; in the second example, filterDataFor is not a function. Could someone point out where I'm going wrong with this?
What you can do is to put a simple placeholder instead of equals(__) inside filter, then simply compose them together with equals:
const { filter, equals, __, compose } = R
const data = [1, 2, 3, 4, 5]
const filterDataFor = compose(filter(__, data), equals);
console.log(filterDataFor(2))Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script>Run Code Online (Sandbox Code Playgroud)
Since compose composes the functions from right to left, when you call filterDataFor(2) it will first evaluate equals(2) then the resulting function will be applied instead of the placeholder, essentially resulting in filter(equals(2), data).