[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 ...
随机推荐
- 解决 Visual Studio 2013、2015、2017 工具箱不显示ArcGIS 10.2 控件,及ArcGIS模板丢失问题
1.重装ArcObject SDK for .NET Framework方法 (1)问题描述: 环境:WIN10 64bit.Visual Studio 2013.ArcGIS10.1.ArcGIS ...
- JavaWeb学习笔记:Tomcat
Tomcat 开源的 Servlet 容器. 部署并启动 tomcat server. 解压 apache-tomcat-6.0.16.zip 到一个非中文文件夹下. 配置一个环境变量. java_h ...
- java / C++ B+树代码
C++ 代码 #include <> JAVA 代码 package org.test.me.struct; /** * author: shuly * create: 2018-09-1 ...
- 分享一些 WINDOWS 资源站点(备用)
分享一些 WINDOWS 资源站点(备用) Windows Embedded http://www.10down.net/windows-embedded.php Windows 7 SP1 Upd ...
- js23---工厂模式1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- 最值(min、max)与极值的理解
max(a,b)=−min(−a,−b) 如果 a≥b ⇒ max(a,b)=a,−a≤−b,⇒ 同理 min(a,b)=−max(−a,−b) 1. 最值 最小:不能更少,如果是整数关系的话,也即从 ...
- golang webservice[ json Martini webframe]
golang webservice[ json Martini webframe] https://github.com/brunoga/go-webservice-sample 自己修改了一下例子, ...
- 重排序列 & 拓扑排序
http://bookshadow.com/weblog/2016/10/30/leetcode-sequence-reconstruction/ 这道题目,检查重排的序列是否一致. 用了拓扑排序. ...
- 几种基于Java的SQL解析工具的比较与调用
1.sqlparser http://www.sqlparser.com/ 优点:支持的数据库最多,除了传统数据库外还支持hive和greenplum一类比较新的数据库,调用比较方便,功能不错 缺点: ...
- swiper轮播控件配置项
var mySwiper = new Swiper ('.swiper-container', { direction: 'horizontal', loop: true, auto ...