zoukankan      html  css  js  c++  java
  • [Ramda] Handle Branching Logic with Ramda's Conditional Functions

    When you want to build your logic with small, composable functions you need a functional way to handle conditional logic. You could wrap ternary expressions and if/else statements in functions, handling all of the concerns around data mutation yourself, or you could leverage the conditional functions supplied by Ramda. In this lesson, we'll cover several of Ramda's conditional functions: ifElseunlesswhen and cond

    const products = [
      {name: 'Jeans', price:80, category: 'clothes'},
      {name: 'Cards', price: 5, category: 'games'},
      {name: 'iPhone', price: 649, category: 'electronics'},
      {name: 'Freakonomics', price: 30, category: 'books'}];
    
    
    /*
      LOGICS
    */
    const pLens = R.lensProp('price');
    const addDiscount = R.curry( (prec, amount) => {
      return amount - (amount * (prec/100))
    });
    
    /*
      EFFECTS
    */
    
    // Apply discount to all the products --> ifElse
    const applyDiscountForAllProduct = () => {
      const adjustPrice = R.over(pLens, addDiscount(50));
      return R.map(adjustPrice, products);
    }
    
    // Apply discount with condition to all predicates
    const applyDiscountWithCondition = () => {
      const prediction = R.propEq('category', 'clothes');
      const conditionTrue = R.over(pLens, addDiscount(50));
      const conditionFalse = R.over(pLens, addDiscount(10));
      
      const adjustPrice = R.ifElse(
        prediction,
        conditionTrue,
        conditionFalse
      );
      
      return R.map(adjustPrice, products);
    }
    
    // Apply disocunt when meet the prediciton --> when
    const applyDiscountOnlyToPart = () => {
      const prediction = R.propEq('category', 'clothes');
      const conditionTrue = R.over(pLens, addDiscount(50));
      const conditionFalse = R.identity; // return the original value
    
      /*const adjustPrice = R.ifElse(
        prediction,
        conditionTrue,
        conditionFalse
      );*/
      
      // or
      const adjustPrice = R.when(
        prediction,
        conditionTrue
      );
      
      return R.map(adjustPrice, products);
      
    }
    
    // Apply discount when prediction return false --> unless
    const applyDiscountOnlyToPart2 = () => {
      const prediction = R.propEq('category', 'clothes');
      const conditionTrue = R.over(pLens, addDiscount(50));
    
      const adjustPrice = R.unless(
        prediction,
        conditionTrue
      );
      
      return R.map(adjustPrice, products);
    }
    
    // Apply discount for multi conditions --> cond
    const applyDiscountForMultiConds = () => {
      const cond1 = [
        R.propEq('category', 'clothes'),
        R.over(pLens, addDiscount(50))
      ];
      const cond2 = [
        R.propEq('category', 'electronics'),
        R.over(pLens, addDiscount(10))
      ];
      const cond3 = [
        R.propEq('category', 'books'),
        R.over(pLens, addDiscount(70))
      ];
      const restCond = [
        R.T,
        R.identity
      ];
      
      const adjustPrice = R.cond([
        cond1,
        cond2,
        cond3,
        restCond
      ]);
      
      return R.map(adjustPrice, products);
    };
    
    
    const result = applyDiscountForMultiConds();
    console.clear();
    console.log(result);
  • 相关阅读:
    软件需求阅读笔记02
    软件需求阅读笔记01
    搜狗输入法
    冲刺周之后感想
    典型用户分析和场景
    四则运算
    学习总结
    2019年春阅读笔记5——对开源的认知
    2019年春阅读笔记4——分布式消息系统的现状、挑战与未来
    2019年春阅读笔记3——数据库集群方案
  • 原文地址:https://www.cnblogs.com/Answer1215/p/6102930.html
Copyright © 2011-2022 走看看