[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)
时间: 2024-10-17 21:10:24

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

[Transducer + Ramda] Write highly performance / functional code by using transducer-js and ramda.js libs

Tansducer-js lib A high performance Transducers implementation for JavaScript. Transducers are composable algorithmic transformations. They are independent from the context of their input and output sources and specify only the essence of the transfo

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 JDBC connections. Read this eGuide to discover the fundamental differences between iPaaS and dPaaS and how the innovative approach of dPaaS gets to t

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

// 下面是引入nodejs的路径模块 var path = require('path') // 下面是utils工具配置文件,主要用来处理css类文件的loader var utils = require('./utils') // 下面引入webpack,来使用webpack内置插件 var webpack = require('webpack') // 下面是config目录下的index.js配置文件,主要用来定义了生产和开发环境的相关基础配置 var config = require

[Cycle.js] Hello World in Cycle.js

Now you should have a good idea what Cycle.run does, and what the DOM Driver is. In this lesson, we will not build a toy version of Cycle.js anymore. Instead, we will learn how to use Cycle.js to solve problems. We will start by making a simple Hello

在 Chrome 开发者工具中调试 node.js

命令行工具 devtool ,它可以在 Chrome 的开发者工具中运行 Node.js 程序. 下面的记录显示了在一个 HTTP 服务器中设置断点的情况. 该工具基于 Electron 将 Node.js 和 Chromium 的功能融合在了一起.它的目的在于为调试.分析和开发 Node.js 应用程序提供一个简单的界面. 你可以使用 npm 来安装它: npm install -g devtool 在某种程度上,我们可以用它来作为 node shell 命令的替代品.例如,我们可以这样打开一

Vue.js——webpack

Vue.js——60分钟webpack项目模板快速入门 browserify是一个 CommonJS风格的模块管理和打包工具,上一篇我们简单地介绍了Vue.js官方基于browserify构筑的一套开发模板.webpack提供了和browserify类似的功能,在前端资源管理这方面,它提供了更加出色的功能.官方基于webpack提供了两种项目模板,分别是vue-webpack-simple模板和vue-webpack模板,今天我们将介绍官方提供的这两种项目模板,并用vue-webpack-sim

翻译 - 【Dojo Tutorials】Part 5 - Build FlickrView for production

原文:Part 5 - Build FlickrView for production 在前面的几篇文章中,我们通过编写HTML, CSS和JavaScript实现了这个FlickrView移动应用.本片文章将专注于为部署更新代码,利用Dojo的构建系统让生产环境应用保持紧凑,并回顾了整个Dojo Mobile驱动的应用. Dojo Mobile与构建 为Dojo Mobile应用创建一个构建版本是很重的,因为我们想要我们的应用尽可能的小.让我们看逐步了解一下如何创建我们Dojo Mobile应

18--Assets Pipeline初探

? 还有上节课其他页面的form也是字体红色,如下: 有一个问题,一个大一点的网站可能一个页面需要加载的coffee文件和scss文件会很多,那么加载页面的时候页面就会不断地向服务器发送请求去加载这些前端文件,如果这些文件很多就会影响页面呈现的速度.而rails也考虑到这点,帮我们做了很多前端优化的工作,就是利用Assets Pipeline进行前端优化: Rails通过Assets Pipeline把所有js和css文件全部压缩打包成一个文件,(打包就是多个js和css文件打包为一个文件,这样

用Backbone.js创建一个联系人管理系统(四)

原文: Build a Contacts Manager Using Backbone.js: Part 4 这一系列教程的第四部分,教我们如何完成对已经存在的Contacts进行编辑和保存. 本教程是基于这一系列的前三个教程. 有不清楚的请先阅读前三部分. 开始 在Contact原模版里添加一个edit按钮. <button class="edit">Edit</button> 在原Contact显示模版下添加新的编辑模版 <script id=&quo