zoukankan      html  css  js  c++  java
  • [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);
  • 相关阅读:
    基础语法 -实验楼
    JavaSE案例-Bank
    初识Java
    Java学习大纲-0412更新
    增量法
    蛮力法
    Host‘116.77.33.xx’is not allowed to connect to this MySQL server
    Maven坐标
    HotSpot虚拟机对象创建
    程序计数器为什么是线程私有的?
  • 原文地址:https://www.cnblogs.com/Answer1215/p/6492127.html
Copyright © 2011-2022 走看看