[Ramada] Build a Functional Pipeline with Ramda.js
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的更多相关文章
- 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 ...
- [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 ...
- vue-cli脚手架build目录中的karma.conf.js配置文件
本文系统讲解vue-cli脚手架build目录中的karma.conf.js配置文件 这个配置文件是命令 npm run unit 的入口配置文件,主要用于单元测试 这条命令的内容如下 "c ...
- vue-pdf的3.3.1版本build后多生成168个js文件
当同事使用vue-pdf来浏览pdf之后,就发现build之后一堆散乱的js文件,真可怕! 果然google之后是它的原因.参考:Vue-pdf create 168 excess bundles i ...
- 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/ ...
- [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 ...
- rails 里js 在production 只合并不压缩等问题,以及assets pipeline 加载js 在指定页面上
因为刚学rails,试着做了一个小系统操作微信公共帐号, 之后部署的时候遇见了一个问题,整套系统在互联网端访问,非常的慢,而在手机端访问,10s后才会有响应, 打开chrome的调试工具,发现appl ...
- [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 ...
- vue-cli脚手架build目录中的webpack.prod.conf.js配置文件
// 下面是引入nodejs的路径模块 var path = require('path') // 下面是utils工具配置文件,主要用来处理css类文件的loader var utils = req ...
随机推荐
- Spring Bean基本管理--bean注入方式汇总
依赖注入方式:Spring支持两种依赖注入方式,分别是属性注入和构造函数注入.还有工厂方法注入方式. 依赖注入还分为:注入依赖对象可以采用手工装配或自动装配,在实际应用开发中建议使用手工装配,因为自动 ...
- strcpy函数和strncpy函数的区别
strcpy函数和strncpy函数的原型介绍在我的另一篇文章中介绍了,见strcpy,strncpy,strlen等函数原型 strcpy:字串复制 原型:char *strcpy(char *de ...
- XTUOJ 1252 Defense Tower 贪心
题目链接:http://202.197.224.59/OnlineJudge2/index.php/Problem/read/id/1252 思路:考虑每条边对玩家的伤害 假设连接的节点是u,v,破坏 ...
- Java每日一则-002
Java中包的层级关系 java中的包在逻辑上是没有套嵌的,也就是说: java.lang 和 java.lang.awt 是两个平行的包,地位相等,互不相关.只不过一个名字叫java.lang另一个 ...
- Spark视频 王家林 Spark公开课大讲坛第二期: Spark的Shark和SparkSQL
王家林 Spark公开课大讲坛第一期:Spark把云计算大数据速度提高100倍以上 http://edu.51cto.com/lesson/id-30816.html Spark实战高手之路 系列书籍 ...
- RabbitMQ (三) 发布/订阅 -摘自网络
这篇博客中,我们会做一些改变,就是把一个消息发给多个消费者,这种模式称之为发布/订阅(类似观察者模式). 为了验证这种模式,我们准备构建一个简单的日志系统.这个系统包含两类程序,一类程序发动日志,另一 ...
- 探索ORACLE之ASM概念
一. ASM(自动存储管理)的来由: ASM是Oracle 10g R2中为了简化Oracle数据库的管理而推出来的一项新功能,这是Oracle自己提供的卷管理器,主要用于替代操作系统所提供的 ...
- 【转】Maven实战(九)---模块聚合和继承
原博文出自于:http://blog.csdn.net/liutengteng130/article/details/47001831 感谢! 类之间有聚合和继承关系,Maven也具备这样的设计原 ...
- Delimiter must not be alphanumeric or backslash 问题及解决
Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash in 正则表达 ...
- JDBC学习笔记(4)——PreparedStatement的使用
PreparedStatement public interface PreparedStatement extends Statement;可以看到PreparedStatement是Stateme ...