[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: ifElse
, unless
, when
and cond
const products = [
{name: 'Jeans', price:, category: 'clothes'},
{name: 'Cards', price: , category: 'games'},
{name: 'iPhone', price: , category: 'electronics'},
{name: 'Freakonomics', price: , category: 'books'}]; /*
LOGICS
*/
const pLens = R.lensProp('price');
const addDiscount = R.curry( (prec, amount) => {
return amount - (amount * (prec/))
}); /*
EFFECTS
*/ // Apply discount to all the products --> ifElse
const applyDiscountForAllProduct = () => {
const adjustPrice = R.over(pLens, addDiscount());
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());
const conditionFalse = R.over(pLens, addDiscount()); 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());
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()); 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())
];
const cond2 = [
R.propEq('category', 'electronics'),
R.over(pLens, addDiscount())
];
const cond3 = [
R.propEq('category', 'books'),
R.over(pLens, addDiscount())
];
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);
[Ramda] Handle Branching Logic with Ramda's Conditional Functions的更多相关文章
- hive中使用case、if:一个region统计业务(hive条件函数case、if、COALESCE语法介绍:CONDITIONAL FUNCTIONS IN HIVE)
前言:Hive ql自己设计总结 1,遇到复杂的查询情况,就分步处理.将一个复杂的逻辑,分成几个简单子步骤处理. 2,但能合在一起的,尽量和在一起的.比如同级别的多个concat函数合并一个selec ...
- [Ramda] Handle Errors in Ramda Pipelines with tryCatch
Handling your logic with composable functions makes your code declarative, leading to code that's ea ...
- [Ramda] Sort, SortBy, SortWith in Ramda
The difference between sort, sortBy, sortWith is that: 1. sort: take function as args. 2. sortBy: ta ...
- [Ramda] Change Object Properties with Ramda Lenses
In this lesson we'll learn the basics of using lenses in Ramda and see how they enable you to focus ...
- [Ramda] Rewrite if..else with Ramda ifElse
From: const onSeachClick = (searchTerm) => { if(searchTerm !== '') { searchForMovies(searchTerm) ...
- [Ramda] Getter and Setter in Ramda & lens
Getter on Object: 1. prop: R.prop(}); //=> 100 R.prop('x', {}); //=> undefined 2. props: R.pro ...
- 前端笔记之React(六)ES6的Set和Map&immutable和Ramda和lodash&redux-thunk
一.ES6的Set.Map数据结构 Map.Set都是ES6新的数据结构,都是新的内置构造函数,也就是说typeof的结果,多了两个: Set 是不能重复的数组 Map 是可以任何东西当做键的对象 E ...
- Methods and Systems for Enhancing Hardware Transactions Using Hardware Transactions in Software Slow-Path
Hybrid transaction memory systems and accompanying methods. A transaction to be executed is received ...
- 50分钟学会Laravel 50个小技巧
50分钟学会Laravel 50个小技巧 时间 2015-12-09 17:13:45 Yuansir-web菜鸟 原文 http://www.yuansir-web.com/2015/12/09 ...
随机推荐
- 【iOS与EV3混合机器人编程系列之中的一个】iOS要干嘛?EV3能够更酷!
乐高Mindstorm EV3智能机器人(下面简称EV3)自从在2013年的CES(Consumer Electronics Show美国消费电子展)上展出之后,就吸引了全球广大机器人爱好者的眼球!E ...
- [ReasonML] Workshops code
/* list of strings */ let _ = ["example-1", "example-2", "example-3"]; ...
- IIS6下AD域设置
简介:IIS6下AD域设置 IIS6下AD域设置 http://files.cnblogs.com/files/KingUp/AD%E5%9F%9F%E8%AE%BE%E7%BD%AE.rar
- 2. APIS官网剖析(博主推荐)
不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ 2. APIS .APIS Apache Kafka引入一个新的java客户端(在o ...
- 基于jQuery的楼层案例
~(function() { var flag = true; //点击切换效果 $(".oDR7_asideItem:not(:first)").click(function() ...
- Vue的学习--遇到的一些问题和解决方法
包括: 1.Missing space before function parentheses 2.如何给.vue文件的页面添加css 3.如何给.vue文件页面里的元素添加监听器 4.如何为每一个页 ...
- 设计模式六大原则(二):里氏替换原则(Liskov Substitution Principle)
里氏替换原则(LSP)由来: 最早是在 妖久八八 年, 由麻神理工学院得一个女士所提出来的. 定义: 1:如果对每一个类型为 T1的对象 o1,都有类型为 T2 的对象o2,使得以 T1定义的所有程序 ...
- phalcon之视图缓存
phalcon官方站点上的视图缓存用法根本就是不通的 现提供一种行的通的方法例如以下: public function testAction() { if( $this->view->ge ...
- LINUX设备驱动程序笔记(三)字符设备驱动程序
<一>.主设备号和次设备号 对字符设备的訪问时通过文件系统内的设备名称进行的.那些设备名称简单称之为文件系统树的节点,它们通常位于/dev文件夹. 字符设备驱动程 ...
- Delphi部份函数,命令,属性中文说明
Abort 函数 引起放弃的意外处理 Abs 函数 绝对值函数 AddExitProc 函数 将一过程添加到运行时库的结束过程表中 Addr 函数 返回指定对象的地址 AdjustLineBreaks ...