We'll learn how to take advantage of Ramda's automatic function currying and data-last argument order to combine a series of pure functions into a left-to-right composition, or pipeline, with Ramda's pipe function.

A simple example will take 'teams' array and output the best score team's name. We use 'R.sort', 'R.head' and 'R.prop' to get job done:

const teams = [
{name: 'Lions', score: 5},
{name: 'Tigers', score: 4},
{name: 'Bears', score: 6},
{name: 'Monkeys', score: 2},
]; const getTopName = function(teams){
const sorted = R.sort( (a,b) => b.score > a.score, teams);
const bestTeam = R.head(sorted);
const name = R.prop('name', bestTeam);
return name;
} const result = getTopName(teams)
console.log(result)

One thing in Ramda which is really cool that, for example, 'R.sort' takes two arguements, if you don't passin the second arguement which is 'teams', it will then return a function, so that it enable you currying function and take second arguement as param.

const teams = [
{name: 'Lions', score: 5},
{name: 'Tigers', score: 4},
{name: 'Bears', score: 6},
{name: 'Monkeys', score: 2},
]; const getBestTeam = R.sort( (a,b) => b.score > a.score);
const getTeamName = R.prop('name');
const getTopName = function(teams){
const sorted = getBestTeam(teams);
const bestTeam = R.head(sorted);
const name = getTeamName(bestTeam);
return name;
} const result = getTopName(teams)
console.log(result)

We will still get the same result.

Use 'R.pipe' to chain function together

In functional programming or lodash (_.chain), we get used to write chain methods, in Ramda, we can use R.pipe():

const teams = [
{name: 'Lions', score: 5},
{name: 'Tigers', score: 4},
{name: 'Bears', score: 6},
{name: 'Monkeys', score: 2},
]; const getBestTeam = R.sort( (a,b) => b.score > a.score);
const getTeamName = R.prop('name');
const getTopName = R.pipe(
getBestTeam,
R.head,
getTeamName
); /*
const getTopName = function(teams){
const sorted = getBestTeam(teams);
const bestTeam = R.head(sorted);
const name = getTeamName(bestTeam);
return name;
}*/ const result = getTopName(teams)
console.log(result)

[Ramada] Build a Functional Pipeline with Ramda.js的更多相关文章

  1. Build an ETL Pipeline With Kafka Connect via JDBC Connectors

    This article is an in-depth tutorial for using Kafka to move data from PostgreSQL to Hadoop HDFS via ...

  2. [Javascript] Deep merge in Javascript with Ramda.js mergeDeepWith

    Javascript's Object.assign is shadow merge, loadsh's _.merge is deep merge, but has probem for array ...

  3. vue-cli脚手架build目录中的karma.conf.js配置文件

    本文系统讲解vue-cli脚手架build目录中的karma.conf.js配置文件 这个配置文件是命令 npm run unit 的入口配置文件,主要用于单元测试 这条命令的内容如下 "c ...

  4. vue-pdf的3.3.1版本build后多生成168个js文件

    当同事使用vue-pdf来浏览pdf之后,就发现build之后一堆散乱的js文件,真可怕! 果然google之后是它的原因.参考:Vue-pdf create 168 excess bundles i ...

  5. How to build a sortable table in native js?

    How to build a sortable table in native/vanilla js? H5 DnD https://developer.mozilla.org/zh-CN/docs/ ...

  6. [Functional Programming 101] Crocks.js -- when to use map and when to use chain?

    As a beginner of Crocks.js, it was a problem for we to figure out when to use .map() and when to use ...

  7. rails 里js 在production 只合并不压缩等问题,以及assets pipeline 加载js 在指定页面上

    因为刚学rails,试着做了一个小系统操作微信公共帐号, 之后部署的时候遇见了一个问题,整套系统在互联网端访问,非常的慢,而在手机端访问,10s后才会有响应, 打开chrome的调试工具,发现appl ...

  8. [Nuxt] Build a Navigation Component in Vue.js and Use in a Nuxt Layout

    You can isolate parts of templates you want to re-use into components, but you can also reuse those ...

  9. vue-cli脚手架build目录中的webpack.prod.conf.js配置文件

    // 下面是引入nodejs的路径模块 var path = require('path') // 下面是utils工具配置文件,主要用来处理css类文件的loader var utils = req ...

随机推荐

  1. Eclipse “Invalid Project Description” when creating new project from existing source

    1) File>Import>General>Existing Project into Workspace2) File>Import>Android>Exist ...

  2. 编程式事务、XML配置事务、注解实现事务

    Spring2.0框架的事务处理有两大类: 1 编码式事务 , 这个不说. 2 声明式事务 , 就说这个. 声明式事务又有三种实现方法: 1 (第一种) 最早的方法,用TransactionProxy ...

  3. 数据仓库之ETL漫谈

    ETL,Extraction-Transformation-Loading的缩写,中文名称为数据抽取.转换和加载. 大多数据仓库的数据架构可以概括为: 数据源-->ODS(操作型数据存储)--& ...

  4. The Bookcase

    题意: 有n本宽w高h的书,向三层书架上放,每层不能为空,求占用的整体的最小面积(总高度*三层中最宽的) 分析: 不太好想,dp[i][j]表示第一层宽度为i第二层为j放的最小高度 dp[i][j]= ...

  5. .net,mvc使用bootstrap做一个标准后台

    今天准备搭一个公用后台,使用bootstrap,方便今后开发,顺便mark一下 后期列表页将使用kendo-ui,增强后台的效果 下面是代码... login页面 @{ Layout = null; ...

  6. linux 系统维护命令

    cat /home/username/.bash_history :查看用户的历史操作记录 (centos , mac 支持) last -x : 使用root登陆使用last -x可查看用户登陆历史 ...

  7. svn IP地址变更后如何变更

    通过grep ip地址,发现svn中url地址信息是记录在.svn文件夹entries文件中的,第一种方案应该是遍历目录下的entries文件,将ip替换为新的ip即可. 可以发现这个用sed命令即可 ...

  8. 2016 CocosPods安装教程

    CocoaPods简介 CocoaPods是一个管理Swift和Objective-C的Cocoa项目的依赖工具.它现在有超过一万八千多个库,可以优雅地帮助你扩展你的项目.简单的说,就是替你管理Swi ...

  9. 启动程序的同时传参给接收程序(XE8+WIN764)

    相关资料: http://blog.csdn.net/yanjiaye520/article/details/7590252 注意事项: 1.ParamStr(0)是实例自己. 2.传的参数是以空格分 ...

  10. HDU 5826 physics (积分推导)

    physics 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5826 Description There are n balls on a smoo ...