[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 ...
随机推荐
- UVA - 10167 - Birthday Cake (简单枚举)
思路:简单枚举 AC代码: #include <cstdio> #include <cstring> #include <iostream> #include &l ...
- hdu 1384 Intervals (差分约束)
/* 给你 n 个区间 [Ai, Bi],要求从每一个区间中至少选出 Ci 个数出来组成一个序列 问:满足上面条件的序列的最短长度是多少? 则对于 不等式 f(b)-f(a)>=c,建立 一条 ...
- [BZOJ1672][Usaco2005 Dec]Cleaning Shifts 清理牛棚 线段树优化DP
链接 题意:给你一些区间,每个区间都有一个花费,求覆盖区间 \([S,T]\) 的最小花费 题解 先将区间排序 设 \(f[i]\) 表示决策到第 \(i\) 个区间,覆盖满 \(S\dots R[i ...
- 1.java soap api操作和发送soap消息
转自:https://blog.csdn.net/lbinzhang/article/details/84721359 1. /** * soap请求 * * @return * @throws Ex ...
- 74.sscanf数据扫描
"%[0-9A-Za-z] 读取一个集合,遇到不是数组或者大小写字母跳出 %*[^0-9A-Za-z]读取所有的非数字字母的字符,忽略 示例: ]= "123sadsadasd ...
- int android.support.v7.widget.RecyclerView$ViewHolder.mItemViewType' on a null.....
Android.support.v7.widget.RecyclerView$ViewHolder.mItemViewType' on a null..空指针问题,费劲心思才找到报空指针的原因: 代码 ...
- 数组-reduce方法
转自: https://github.com/Advanced-Frontend/Daily-Interview-Question/issues/139 实现 convert 方法,把原始 list ...
- 七、Docker+nginx
原文:七.Docker+nginx docker run -p 80:80 --name nginx-v1.0.0 -v /usr/nginx/www:/www -v /home/docker/ngi ...
- 常用处理字符串的SQL函数
汇总函数:Count.Sum.AVG.MAX.min; 数学函数: ABS:绝对值.floor:给出比给定值小的最大整数. round(m,n):m为给定的值,n为小数点后保留的位数. power(m ...
- POJ——T2271 Guardian of Decency
http://poj.org/problem?id=2771 Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 5932 A ...