[Ramda] Handle Branching Logic with Ramda's Conditional Functions

发布时间:2017-3-2 4:00:36 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"[Ramda] Handle Branching Logic with Ramda's Conditional Functions ",主要涉及到[Ramda] Handle Branching Logic with Ramda's Conditional Functions 方面的内容,对于[Ramda] Handle Branching Logic with Ramda's Conditional Functions 感兴趣的同学可以参考一下。

[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);

上一篇:Vim安装jedi-vim提示的一个错误
下一篇:Maven常用参数及其说明【转:http://blog.csdn.net/wangjunjun2008/article/details/18982089】

相关文章

相关评论

本站评论功能暂时取消,后续此功能例行通知。

一、不得利用本站危害国家安全、泄露国家秘密,不得侵犯国家社会集体的和公民的合法权益,不得利用本站制作、复制和传播不法有害信息!

二、互相尊重,对自己的言论和行为负责。