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

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

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

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

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

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

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

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

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

  6. [转]如何在 JS 代码中消灭 for 循环

    一,用好 filter,map,和其它 ES6 新增的高阶遍历函数 二,理解和熟练使用 reduce 三,用递归代替循环(可以break!) 四,使用高阶函数遍历数组时可能遇到的陷阱 五,死磕到底,T ...

  7. 翻译连载 | 附录 C:函数式编程函数库-《JavaScript轻量级函数式编程》 |《你不知道的JS》姊妹篇

    原文地址:Functional-Light-JS 原文作者:Kyle Simpson-<You-Dont-Know-JS>作者 关于译者:这是一个流淌着沪江血液的纯粹工程:认真,是 HTM ...

  8. 给 JavaScript 开发者讲讲函数式编程

    本文译自:Functional Programming for JavaScript People 和大多数人一样,我在几个月前听到了很多关于函数式编程的东西,不过并没有更深入的了解.于我而言,可能只 ...

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

随机推荐

  1. CSS3制作W3cplus的关注面板

    <!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...

  2. JS 数字格式千分位相互转换

    /** * 将数值四舍五入后格式化. * * @param num 数值(Number或者String) * @param cent 要保留的小数位(Number) * @param isThousa ...

  3. 原生js大总结四

    031.数组常用的一些方法   1.push: 在数组最后添加一个或者多个元素,返回添加后数组的长度   2.pop: 从数组最后取出一个元素,返回的是数组的最后一个元素(取出的元素)   3.uns ...

  4. Oracle学习总结(9)—— Oracle 常用的基本操作

    创建用户,相当于在sqlServer中创建一个数据库  create user 用户名 identified by 密码  修改用户密码  alter user 用户名 identified by 新 ...

  5. SQLite基础学习

    SQLite是一款轻量级数据库,集成于android中,以下从分享一下自己学习的. 在查阅资料时有一些好的说明就直接用了: 主要的curd语句 以下SQL语句获取5条记录,跳过前面3条记录 selec ...

  6. python3 分解质因数

    python3 分解质因数 #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Hiuhung Wan num = int(input(&quo ...

  7. 怎样用Adobe Acrobat 7 Pro把PDF文档拆分成多个啊?

    这个pdf文档里有多篇文章,我想把他们分开并分别保存在独立的pdf文档.怎么操作?我的电脑基础不太好,麻烦说得详细一些. Adobe Acrobat 7 Pro拆分PDF文档的方法: 1.点左边的“书 ...

  8. Log4net.confager配置官方文档

    http://logging.apache.org/log4net/release/config-examples.html

  9. spark源码解析之scala基本语法

    1. scala初识 spark由scala编写,要解析scala,首先要对scala有基本的了解. 1.1 class vs object A class is a blueprint for ob ...

  10. 利用Eclipse+openJTAG调试led.axf文件

    转自calvinlee1984 Subject:利用Eclipse+openJTAG调试led.axf文件 Date:     3-Mar-2011 By:         Calvinlee1984 ...