[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 ...
随机推荐
- Qt虽然自己的源代码里不使用Exception,但也提供了一个QException及其子类QUnhandledException
http://doc.qt.io/qt-5/exceptionsafety.htmlhttp://doc.qt.io/qt-5/qexception.htmlhttp://doc.qt.io/qt-5 ...
- R语言-上海二手房数据分析
案例:通过分析上海的二手房的数据,分析出性价比(地段,价格,未来的升值空间)来判断哪个区位的二手房性价比最高 1.载入包 library(ggplot2) library(Hmisc) library ...
- HDU 4010 Query on The Trees (动态树)(Link-Cut-Tree)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4010 题意; 先给你一棵树,有 \(4\) 种操作: 1.如果 \(x\) 和 \(y\) 不在同一 ...
- 【Codeforces Round #451 (Div. 2) A】Rounding
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 模拟 [代码] /* 1.Shoud it use long long ? 2.Have you ever test several ...
- maven仓库快速镜像
国内连接maven官方的仓库更新依赖库,网速一般很慢,收集一些国内快速的maven仓库镜像以备用. ====================国内OSChina提供的镜像,非常不错=========== ...
- 3lession-python编程规范
今天继续学习python,内容主要就是python编程过程中的一些规范,包括注释.换行等 1.注释 所有的注释都是以#开头,注释可以单独占有一行,也可以放到语句的末尾.因为python没有多行注释,所 ...
- android.mk-编译文件学习(转载)
工作了那么久,都是使用大神们写的脚本机械的编译,对于android.mk根本没去了解过.今天趁着这个机会,在网上搜索了下.虽然依然不是很名白,留做记录,以后真用到了,再深入研究 转载自 http:// ...
- 1.Dubbo教程
转自:https://blog.csdn.net/hellozpc/article/details/78575773 2. 什么是dubbo 2.1. 简介 DUBBO是一个分布式服务框架,致力于提供 ...
- 基于EmguCV的摄像机标定及矫正
标签: EmguCV摄像头标定C# 2015-05-03 14:55 501人阅读 评论(6) 收藏 举报 分类: C# 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] ...
- jmeter--参数化的四种方法
本文转自:http://www.cnblogs.com/imyalost/p/6229355.html 参数化是自动化测试脚本的一种常用技巧.简单来说,参数化的一般用法就是将脚本中的某些输入使用参数来 ...