Search code examples
javascriptfunctional-programmingramda.jspointfree

How to write with 2+ parameters function in point-free style


I'm trying to implement this pointful function

const getItem = (items, id) => items.find(item => item.id === id);

in point-free style using ramda.js.

When i use something like this:

const getItem = find(propEq('id'));

the first parameter items will be passed to find function, but we will lose 2nd id parameter.

The question is, how to implement getItem function in point-free style?


Solution

  • If you are free to change the order of arguments of the function, useWith is a simple solution:

    const getItemById = R.useWith(
      R.find,
      [R.propEq('id')]
    );
    
    console.log(
      getItemById(
        'b',
        [{ id: 'a' }, { id: 'b' }]
      )
    );
    <script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.24.0/ramda.min.js"></script>