[Ramda] Rewrite if..else with Ramda ifElse
From:
const onSeachClick = (searchTerm) => {
if(searchTerm !== '') {
searchForMovies(searchTerm)
} else {
console.log('a search term should be provided')
}
}
To:
// Utils const inNotEmpty = R.compose(
R.not,
R.isEmpty
); const onSearchClick = () => {
R.ifElse(
isNotEmpty, // logic to check
searchForMovices, // do it if true
log('a search term should be provided') // do it if false
)
}
Example2:
/*
Example2:
*/
function processSearchResponse(response) {
// check total_results prop from response,
// it shuold greater than 0
const searchHasResult = R.compose(
R.lt(),
R.prop('total_results')
);
// get results props from response,
// then createMoviesElements called
const createElementFromResults = R.compose(
createMovicesElements,
R.prop('results')
);
//always return empty
const createArrayWithNotFound = R.always([
createMoviceNotFoundElement({})
]); const elements = R.ifElse(
searchHasResult,
createElementFromResults,
createArrayWithNotFound
)(response);
}
[Ramda] Rewrite if..else with Ramda ifElse的更多相关文章
- [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 hand ...
- [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] 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 ...
- [转]如何在 JS 代码中消灭 for 循环
一,用好 filter,map,和其它 ES6 新增的高阶遍历函数 二,理解和熟练使用 reduce 三,用递归代替循环(可以break!) 四,使用高阶函数遍历数组时可能遇到的陷阱 五,死磕到底,T ...
- 翻译连载 | 附录 C:函数式编程函数库-《JavaScript轻量级函数式编程》 |《你不知道的JS》姊妹篇
原文地址:Functional-Light-JS 原文作者:Kyle Simpson-<You-Dont-Know-JS>作者 关于译者:这是一个流淌着沪江血液的纯粹工程:认真,是 HTM ...
- 给 JavaScript 开发者讲讲函数式编程
本文译自:Functional Programming for JavaScript People 和大多数人一样,我在几个月前听到了很多关于函数式编程的东西,不过并没有更深入的了解.于我而言,可能只 ...
- [Ramda] Filter, Reject and Partition
We'll learn how to get a subset of an array by specifying items to include with filter, or items to ...
随机推荐
- gplaycli—— 用于从 GooglePlayStore 中下载和管理 Apk 文件的命令行工具
gplaycli-- 用于从 GooglePlayStore 中下载和管理 Apk 文件的命令行工具 这个 GooglePlay市场 中 https://play.google.com/store/a ...
- eclipse-12.04无图标,无法固定到侧边栏
今天下载了adt-bundle,但是eclipse打开以后,侧边栏显示的是一个问好,而且没办法固定到侧边栏,导致每次打开eclipse都要进入到相应目录,很麻烦.我们可以通过如下方法设置eclipse ...
- eclipse- log 打印跟输出到文件
1.在eclipse中打印log,经常使用的就是log.e(string,string) 代码中如下 @Override public boolean onTouchEvent(MotionEvent ...
- Event Serializers官网剖析(博主推荐)
不多说,直接上干货! Flume Sources官网剖析(博主推荐) Flume Channels官网剖析(博主推荐) Flume Channel Selectors官网剖析(博主推荐) Flume ...
- JavaScript---call()使用的一些疑问
疑问:在使用.call()时,调用对象到底是否可以直接拥有了被调用者的方法和属性? 这里输出结果为:ReferenceError: o is not defined function Person(n ...
- PatentTips - Method and Apparatus to Support Virtualization with Code Patches
BACKGROUND As recognized in Revision 2.0 of the Intel® Virtualization Technology Specification for t ...
- python输出杨辉三角
使用python列表,展示杨辉三角 # !/usr/bin/env python # -*- coding:utf-8 -*- # Author:Hiuhung Wan yanghui = [] fo ...
- 驱动学习3-make
在向内核中添加驱动的时候要完成3项工作 (1)在Kconfig中添加新代码对应项目的编译条件(下面Makefile文件中需要用到它定义的的宏变量) (2)将驱动源码添加到对应的目录中 (3)在目录Ma ...
- HDU 2587 - 很O_O的汉诺塔
看题传送门 吐槽题目 叫什么很O_O的汉诺塔我还@.@呢. 本来是想过一段时间在来写题解的,不过有人找我要. 本来排名是第8的.然后搞了半天,弄到了第五.不过代码最短~ 截止目前就9个ID过,小小的成 ...
- stm32四种输入
1. 上拉输入(GPIO_Mode_IPU) 上拉输入就是信号进入芯片后加了一个上拉电阻,再经过施密特触发器转换成0.1信号,读取此时的引脚电平为高电平: 2. ...