[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 ...
随机推荐
- Windows 共享无线上网 无法启动ICS服务解决方法(WIN7 ICS服务启动后停止)
Windows 共享无线上网 无法启动ICS服务解决方法(WIN7 ICS服务启动后停止) ICS 即Internet Connection Sharing,internet连接共享,可以使局域网上其 ...
- sqlite3-查看数据库
在做android开发的时候,有时候我们需要查看系统下的数据库,这时候我们可以使用下面的方法 1.数据库存放位置 data/data/package/databases/abc.db 2.导出数据库 ...
- touch、touchevent-事件总结
1.废话不多说,直接上代码,如下 package com.example.mygestrue; import android.support.v7.app.ActionBarActivity; imp ...
- java解压多目录Zip文件(解决中文乱码问题)--转载
原文地址:http://zhangyongbo.iteye.com/blog/1749439 import java.io.BufferedOutputStream; import java.io.F ...
- Android 6.0 最简单的权限获取方法 RxPermition EasyPermition
Android 6.0 要单独的获取权限 这里提供两种很简单的方法 EasyPermition RxPermition EasyPermition https://github.com/googles ...
- 洛谷 P2755 洗牌问题
P2755 洗牌问题 题目描述 给你2N张牌,编号为1,2,3..n,n+1,..2n.这也是最初的牌的顺序. 一次洗牌是把序列变为n+1,1,n+2,2,n+3,3,n+4,4..2n,n.可以证 ...
- GPUImage ==> 一个基于GPU图像和视频处理的开源iOS框架
Logo 项目介绍: GPUImage是Brad Larson在github托管的开源项目. GPUImage是一个基于GPU图像和视频处理的开源iOS框架,提供各种各样的图像处理滤镜,并且支持照相机 ...
- CentOS7安装docker 18.06
原文:CentOS7安装docker 18.06 一.CentOS Docker 安装 参考docker 官方网站:https://docs.docker.com/install/linux/dock ...
- sea.js五分钟上手
SeaJS是一个遵循CommonJS规范的JavaScript模块加载框架.本文给大家分享sea.js知识总结,感兴趣的朋友一起学习吧http://reactjs.cn/http://reactjs. ...
- (转)Windows2008优化IIS7.5支持10万个同时请求的配置方法
通过对IIS7的配置进行优化,调整IIS7应用池的队列长度,请求数限制,TCPIP连接数等方面,从而使WEB服务器的性能得以提升,保证WEB访问的访问流畅. 在运行中cmd后,输入:C:\Window ...