[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 ...
随机推荐
- hdu 3948(后缀数组+RMQ)
题意:求一个串中有多少不同的回文串. 分析:这一题的关键是如何去重,我表示我现在还没理解为什么这样去重,先放这里过两天再看!! //不同回文子串数目 #include <iostream> ...
- Object类介绍
一.Object类介绍
- webdriver(python)学习笔记七——多层框架定位与智能等待
多层框架或窗口定位: switch_to_frame() switch_to_window() 智能等待: implicitly_wait() 现在web应用中经常会遇到框架如(frame)或窗口(w ...
- LeetCode题解——Container With Most Water
题目: x轴上有一些点,每个点上有一条与x轴垂直的线(线的下端就是这个点,不超出x轴),给出每条线的高度,求这些线与x轴组成的最大面积. 解法: 贪心策略,维持两个指针,分别指向第一个和最后一个元素, ...
- php 正则表达
今天看书,看到代码里面出现了一段正则表达式匹配语句preg_match,感觉水很深的感觉,网上搜了一些资料,暂时没时间学习,但是觉得以后学的话有两个网址比较靠谱,如下: php正则表达式手册:php ...
- Javascript手记-执行环境和作用域
执行环境是javascript一个重要的概念,执行环境定义了变量有权访问其他数据决定了他们各自的行为,每个执行环境 都有一个与之关联的变量,环境中定义的所有变量和函数都保存在这个对象中,虽然我们编写的 ...
- 王家林的81门一站式云计算分布式大数据&移动互联网解决方案课程第14门课程:Android软硬整合设计与框架揭秘: HAL&Framework &Native Service &App&HTML5架构设计与实战开发
掌握Android从底层开发到框架整合技术到上层App开发及HTML5的全部技术: 一次彻底的Android架构.思想和实战技术的洗礼: 彻底掌握Andorid HAL.Android Runtime ...
- 关于OpenCV做图像处理内存释放的一些问题
转载:http://blog.sina.com.cn/s/blog_67a7426a0101czyr.html 工程运行,发现内存持续增长,到一定的时候就发生了内存泄漏. 内存泄露的定义 内存泄露是说 ...
- MLlib 中的聚类和分类
聚类和分类是机器学习中两个常用的算法,聚类将数据分开为不同的集合,分类对新数据进行类别预测,下面将就两类算法进行介绍. 1. 聚类和分类(1)什么是聚类 聚类( Clustering)指将数据对象分组 ...
- Hadoop port to Jxta P2P Framework
https://www.java.net/forum/topic/jxta/jxta-community-forum/hadoop-port-jxta-p2p-framework —————————— ...