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. 详解Asp.net MVC DropDownLists

    Asp.net MVC中的DropDownLists貌似会让一开始从Asp.net Forms转过来的程序员造成不少迷惑.这篇文章讲述了为了使用DropDownLists,你需要在Asp.Net MV ...

  2. bzoj 1061 [Noi2008]志愿者招募(数学模型,MCMF)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1061 [题意] 雇人满足每天至少需要的人数. [思路一] Byvoid的题解 clic ...

  3. mybatis系列-06-输入映射

    通过parameterType指定输入参数的类型,类型可以是简单类型.hashmap.pojo的包装类型 6.1     传递pojo的包装对象 6.1.1     需求 完成用户信息的综合查询,需要 ...

  4. Designing Evolvable Web API with ASP.NET 随便读,随便记 “The Internet,the World Wide Web,and HTTP”

    1982年,诞生了 Internet; 1989年,诞生了World Wide Web . "World Wide Web"的构造为主要由 三部分构成: resources 资源 ...

  5. 机器学习中的数学(3)-模型组合(Model Combining)之Boosting与Gradient Boosting

    版权声明: 本文由LeftNotEasy发布于http://leftnoteasy.cnblogs.com, 本文可以被全部的转载或者部分使用,但请注明出处,如果有问题,请联系wheeleast@gm ...

  6. linux 切换c++版本

    删除gcc-4.6的软连接文件/usr/bin/gcc.(只是删除软连接) 命令:sudo rm /usr/bin/gcc 然后建一个软连接,指向gcc-4.4. 命令:sudo ln -s /usr ...

  7. node-mysql中的连接池代码学习

    node-mysql是一个node.js下的mysql驱动,前段时间在处理连接池的问题上遇到了连接不释放的疑难杂症,虽已解决,但仍需总结经验避免下次重蹈覆辙.下面是node-mysql中的连接池的部分 ...

  8. Linux优化之IO子系统监控与调优

    Linux优化之IO子系统 作为服务器主机来讲,最大的两个IO类型 : 1.磁盘IO 2.网络IO 这是我们调整最多的两个部分所在 磁盘IO是如何实现的 在内存调优中,一直在讲到为了加速性能,linu ...

  9. UI进阶 KVO

    KVO:(Key-Value-Observer)键值观察者,是观察者设计模式的一种具体实现 KVO触发机制:一个对象(观察者),监测另一对象(被观察者)的某属性是否发生变化,若被监测的属性发生的更改, ...

  10. Java学习笔记(六):面向对象、接口和抽象类

    类和对象 Java是一门面向对象的语言,下面我们来了解一下Java中的面向对象. 方法和重载 Java中的方法格式如下: 访问修饰符 返回值类型 方法名(参数){ 方法主体 } Java的方法支持重载 ...