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: ifElseunlesswhen 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的更多相关文章

  1. hive中使用case、if:一个region统计业务(hive条件函数case、if、COALESCE语法介绍:CONDITIONAL FUNCTIONS IN HIVE)

    前言:Hive ql自己设计总结 1,遇到复杂的查询情况,就分步处理.将一个复杂的逻辑,分成几个简单子步骤处理. 2,但能合在一起的,尽量和在一起的.比如同级别的多个concat函数合并一个selec ...

  2. [Ramda] Handle Errors in Ramda Pipelines with tryCatch

    Handling your logic with composable functions makes your code declarative, leading to code that's ea ...

  3. [Ramda] Sort, SortBy, SortWith in Ramda

    The difference between sort, sortBy, sortWith is that: 1. sort: take function as args. 2. sortBy: ta ...

  4. [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 ...

  5. [Ramda] Rewrite if..else with Ramda ifElse

    From: const onSeachClick = (searchTerm) => { if(searchTerm !== '') { searchForMovies(searchTerm) ...

  6. [Ramda] Getter and Setter in Ramda & lens

    Getter on Object: 1. prop: R.prop(}); //=> 100 R.prop('x', {}); //=> undefined 2. props: R.pro ...

  7. 前端笔记之React(六)ES6的Set和Map&immutable和Ramda和lodash&redux-thunk

    一.ES6的Set.Map数据结构 Map.Set都是ES6新的数据结构,都是新的内置构造函数,也就是说typeof的结果,多了两个: Set 是不能重复的数组 Map 是可以任何东西当做键的对象 E ...

  8. 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 ...

  9. 50分钟学会Laravel 50个小技巧

    50分钟学会Laravel 50个小技巧 时间 2015-12-09 17:13:45  Yuansir-web菜鸟 原文  http://www.yuansir-web.com/2015/12/09 ...

随机推荐

  1. 洛谷 P2240 数的计数数据加强版

    P2240 数的计数数据加强版 题目背景 无 题目描述 我们要求找出具有下列性质数的个数(包含输入的自然数n): 先输入一个自然数n(n<=1500001),然后对此自然数按照如下方法进行处理: ...

  2. 怎样将OpenStack部署到Hadoop

    随着信息时代的快速发展,大数据技术和私有云环境都非常实用;只是,假设将两者结合在一起.企业会获得巨大的利润.虽然结合两者会让环境变得更复杂.企业仍然能够看到将 OpenStack 私有云和 Apach ...

  3. fromCharCode vs chr

    fromCharCode vs chr echo off set "fn=%*" set php=d:/www/php5/php.exe cls echo. %php% %fn% ...

  4. 一个很详细的web.xml讲解(转)

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC "- ...

  5. python缩进报错

    1.这个错误应该已经好久了:也应该熟悉了才对 IndentationError: unexpected indent python 缩进错误:意外缩进Python 但今天的问题有点特殊,我想在程序中间 ...

  6. vue 星星评分组件

    显示评分和打分组件,可现实半颗星星效果 效果图: 参数名 类型 说明 score Number 分数 ,默认0,保留一位小数 disabled Boolean 是否只读,默认false,鼠标点击可以打 ...

  7. Validation failed for query for method public abstract boxfish.bean.Student boxfish.service.StudentServiceBean.find(java.lang.String)!

    转自:https://blog.csdn.net/lzx925060109/article/details/40323741 1. Exception in thread "main&quo ...

  8. Js里面的arguments

    了解这个对象之前先来认识一下javascript的一些功能: 其实Javascript并没有重载函数的功能,但是Arguments对象能够模拟重载.Javascrip中国每个函数都会有一个Argume ...

  9. QSYS系统简介

    QSYS是SoPC Builder的新一代产品. QSYS系统集成工具自动生成互联逻辑,连接IP和子系统 QSYS的设计理念是提高设计抽象级,从而使机器自动生成底层代码. Altera的Avalon总 ...

  10. C# 数据通信

    json asmxwcfwebRequestwebClient 串口 socket