[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);
复制代码

 

posted @   Zhentiw  阅读(516)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2015-11-25 [Redux] Store Methods: getState(), dispatch(), and subscribe()
2015-11-25 [Redux] Introduction
2014-11-25 [AngularJS] Best Practise - Minification and annotation
2014-11-25 [AngularJS] Best Practise - Module
2014-11-25 [AngularJS] Best Practise - Controller
点击右上角即可分享
微信分享提示