[Ramda] Handle Branching Logic with Ramda's Conditional Functions
with 39 Functions handle Logic Conditional Ramda
2023-09-14 08:59:19 时间
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: ifElse
, unless
, when
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);
相关文章
- ORA-01555: snapshot too old: rollback segment number string with name “string” too small ORACLE 报错 故障修复 远程处理
- ORA-23463: flavor incompatible with object “string”.”string” ORACLE 报错 故障修复 远程处理
- ORA-31105: User does not own lock with supplied token “string” ORACLE 报错 故障修复 远程处理
- ORA-31153: Cannot create schema URL with reserved prefix “http://xmlns.oracle.com/xdb/schemas/” ORACLE 报错 故障修复 远程处理
- ORA-41647: negation not allowed in rule conditions with “any” semantics ORACLE 报错 故障修复 远程处理
- ORA-42030: cannot online redefine table “string”.”string” with commit SCN-based MV log ORACLE 报错 故障修复 远程处理
- ORA-42037: cannot online redefine table “string”.”string” with IOT overflow segment ORACLE 报错 故障修复 远程处理
- ORA-42039: cannot online redefine table “string”.”string” with FGA or RLS enabled ORACLE 报错 故障修复 远程处理
- ORA-42040: cannot online redefine table “string”.”string” with column of binary XML type ORACLE 报错 故障修复 远程处理
- ORA-01555: snapshot too old: rollback segment number string with name “string” too small ORACLE 报错 故障修复 远程处理
- ORA-12082: “string”.”string” cannot be index organized ORACLE 报错 故障修复 远程处理
- ORA-15701: All “SQL Tuning Set(s)” with name like “string” and owner like “string” are empty ORACLE 报错 故障修复 远程处理
- ORA-16179: incremental changes to “string” not allowed with SPFILE ORACLE 报错 故障修复 远程处理
- 图文详解mysql中with…as用法
- postgresql—-数据库表约束—-NOT NULL,DEFAULT,CHECK详解数据库
- mysql insert into … select from on duplicate update详解数据库
- UIImageView — 选择图片详解手机开发
- iOS开发笔记 – Swift快速入门(视频分享)详解手机开发
- 微信小程序开发—小程序对接Django—6详解手机开发
- SDP(0):Streaming-Data-Processor – Data Processing with Akka-Stream详解编程语言
- log4j日志的配置–Debug详解编程语言
- Java DES 加解密(”DES/CBC/PKCS5Padding”)详解编程语言
- disabled=”disabled” readonly=”readonly” type=”hidden”提交表单的区别详解编程语言
- Create a nice looking chart with CL_GUI_CHART_ENGINE – Part 1 – Chart Designer详解编程语言
- Create a nice looking chart with CL_GUI_CHART_ENGINE – Part 3 – Chart Data and render详解编程语言
- Nvidia、Google、Intel……同样是做AI芯片,走的路又有什么不同?| 深度