[Ramda] Refactor to a Point Free Function with Ramda's useWith Function

Naming things is hard and arguments in generic utility functions are no exception. Making functions "tacit" or "point free" removes the need for the extra parameter names and can make your code cleaner and more succinct. In this lesson, we'll create a normal, "pointed" function and then use ramda's useWith function to refactor our way to point-free bliss.

const R = require('ramda');

const { find, useWith, identity, propEq } = R;

const countries = [
    {flag: 'GB', cc: 'GB'},
    {flag: 'US', cc: 'US'},
    {flag: 'CA', cc: 'CA'},
    {flag: 'FR', cc: 'FR'}
];

// Normally when you see there are multi args, you can think to use 'R.useWith'
// to make it point free style
//const getCountry = (cc, list) => find(propEq('cc'), list);

/*
* Refactor the getCountry function to make it as point-free function
* */
const getCountry = useWith(
    find,
    [
        propEq('cc'),
        identity
    ]
);

const result = getCountry('US', countries);

console.log(result);
原文地址:https://www.cnblogs.com/Answer1215/p/6492127.html