原文: https://codewithstyle.info/deep-dive-pipe-function-rxjs/

------------------------------------------------------------

Version 5 of RxJS introduced the concept of lettable (also known as pipeable) operators. Version 6 went one step further and deprecated the old way of calling operators (method chaining). You might have already used the pipe function. But do you really understand what it does?

Composing functions

RxJS is often called a functional-reactive programming library. It should not come as a surprise that you will find many functional programming inspirations in it. One of them is the pipe function. Take a look at the below piece of code:

const getElement = 
(id) => document.getElementById(id); const getValue =
(element) => element.value; function logElementValue(id) {
const el = getElement(id);
const value = getValue(el);
console.log(value);
}

The logElementValue function takes an id and logs to the console the value of the element with provided id. Can you see a pattern in this function’s implementation? Firstly, it calls getElement with id and stores the result in el. Next, the result is passed to getValue which produces a new result, el. Finally, el is passed to console.log. What this function does is simply taking the result of a function and passing it as an argument to another function. Is there a better, more concise way to implement this function? Let’s say we just have two functions (getElement and getValue). We will implement a generic function called compose that will pass the result of getElement to getValue.

const compose = (f, g) => x => g(f(x));

The definition is very simple but may take a moment to parse. We’ve defined a function that takes two functions f and g (that would be getElement and getValue in our case) and returns a new function. This new function will take an argument, pass it to f and then pass the result to g. That’s exactly what we need! Now I can rewrite logElementValue:

function logElementValue(id) {
const getValueFromId = compose(getElement, getValue);
const value = getValueFromId(id);
console.log(value);
}

How about more than two functions?

But, wait! Once we have the result of calling getValueFromId we immediately pass it to console.log. So it’s the same pattern here. We could write it like this:

function logElementValue(id) {
const getValueFromId = compose(getElement, getValue);
const logValue = compose(getValueFromId, console.log);
logValue(id);
}

But life would be much simpler if compose could take any number of functions. Can we do this? Sure:

const composeMany = (...args) => args.reduce(compose);

Another brain teaser! composeMany takes any number of functions. They are stored in args array. We reduce over args composing every function with the result of composing previous functions. Anyway, the results is a function that takes any number of functions and will pass the result of N-th function to (N+1)-th function. But what have we achieved by that?

function logElementValue(id) {  
const logValue = composeMany(getElement, getValue, console.log);
logValue(id);
}

Which can be simplified even more:

const logElementValue = composeMany(getElement, getValue, console.log);

Isn’t that cool? We have significantly simplified the code. It’s now very clear what logElementValue does. And by the way - composeMany is just a name a came up with. The official name is pipe!

const logElementValue = pipe(getElement, getValue, console.log);

Back to RxJS

Let’s take an example of pipe usage in RxJS.

number$.pipe(
map(n => n * n),
filter(n => n % 2 === 0)
);

We can also write it in a different way:

const { pipe } = rxjs;

const transformNumbers = pipe(
map(x => x * x),
filter(x => x % 2 === 0),
); transformNumbers(number$).subscribe(console.log);

And the result is exactly the same! As you can see, the pipe function in RxJS behaves in exactly the same way that the pipe function that we’ve defined in the first part of the article. It takes a number of functions and composes them by passing the result of a function as an argument to another function. You might say that this is different than the previous example because here we’re invoking map and filter and not simply passing them. Actually, both map and filter will return functions. We’re not composing map and filter themselves but rather the functions returned by invoking them. You can check out how RxJS implements pipe function here.

Pipeline operator

Our function is such a useful concept that it might be added as a separate operator to the JavaScript language! It would mean that the example from the previous article can be written in an even simpler way:

const logElementValue = getElement |> getValue |> console.log;

You can see the details of the proposal here.

Summary

I hope this article helped you understand what pipe function is all about. You should now feel more comfortable using it! The fact that RxJS migrated from the traditional, object-oriented approach of applying operators to the pipeline approach shows how strong the influence of functional programming is nowadays. I think that’s great! Let me know in comments if you prefer pipe function to traditional method chaining.

【转】Deep dive into pipe function in RxJS的更多相关文章

  1. [RxJS] Chain RxJS Operators Together with a Custom `pipe` Function using Array.reduce

    Instead of writing complex operators, it's usually best to write simple, single-purpose operators th ...

  2. X64 Deep Dive

    zhuan http://www.codemachine.com/article_x64deepdive.html X64 Deep Dive This tutorial discusses some ...

  3. 《Docker Deep Dive》Note - Docker 引擎

    <Docker Deep Dive>Note Docker 引擎 1. 概览 graph TB A(Docker client) --- B(daemon) subgraph Docker ...

  4. Deep Dive into Neo4j 3.5 Full Text Search

    In this blog we will go over the Full Text Search capabilities available in the latest major release ...

  5. Deep Dive into Spark SQL’s Catalyst Optimizer(中英双语)

    文章标题 Deep Dive into Spark SQL’s Catalyst Optimizer 作者介绍 Michael Armbrust, Yin Huai, Cheng Liang, Rey ...

  6. 《Docker Deep Dive》Note - 纵观 Docker

    <Docker Deep Dive>Note 由于GFW的隔离,国内拉取镜像会报TLS handshake timeout的错误:需要配置 registry-mirrors 为国内源解决这 ...

  7. 重磅解读:K8s Cluster Autoscaler模块及对应华为云插件Deep Dive

    摘要:本文将解密K8s Cluster Autoscaler模块的架构和代码的Deep Dive,及K8s Cluster Autoscaler 华为云插件. 背景信息 基于业务团队(Cloud BU ...

  8. vue3 deep dive

    vue3 deep dive vue core vnode vue core render / mount / patch refs https://www.vuemastery.com/course ...

  9. A Deep Dive Into Draggable and DragTarget in Flutter

    https://medium.com/flutter-community/a-deep-dive-into-draggable-and-dragtarget-in-flutter-487919f6f1 ...

随机推荐

  1. WARNING:Your password has expired --linux 用户密码过期

    今天在ssh 提示  WARNING:Your password has expired 设置用户到期时间 chage -M 36000 用户名 chage -l 用户名 #查看用户信息

  2. linux grep的用法

    linux grep的用法<pre>[root@iZ23uewresmZ ~]# cat /home/ceshis.txtb124230 b034325 a081016 m7187998 ...

  3. linux-pdb命令行下python断点调试工具

    一般地,我们可以使用如下的方式进入调试(比如我们要调试的源文件为hello.py):  1. 在命令行启动目标程序,加上-m参数.  python -m pdb hello.py 这样程序会自动停在第 ...

  4. cetos7 Mysql5.7安装

    centos7 MySQL安装 一.检查是否已经存在mysql,若存在卸载,避免安装时产生一些错误 rpm -qa | grep -i mysql 若存在,使用rpm -e packname –nod ...

  5. 分布式全文搜索引擎(ElasticSearch)

    1. ElasticSearch介绍(简称ES) ES即为了解决原生Lucene使用的不足,优化Lucene的调用方式,并实现了高可用的分布式集群的搜索方案. 首先,ES的索引库管理支持依然是基于Ap ...

  6. Python-11-生成器

    一.定义 可以理解为一种数据类型,这种数据类型自动实现了迭代器协议(其他数据类型需要调用__iter__方法),所以生成器就是一种迭代器. 二.生成器的两种形式 1. 生成器函数 使用yield代替r ...

  7. Scrapy框架——介绍、安装、命令行创建,启动、项目目录结构介绍、Spiders文件夹详解(包括去重规则)、Selectors解析页面、Items、pipelines(自定义pipeline)、下载中间件(Downloader Middleware)、爬虫中间件、信号

    一 介绍 Scrapy一个开源和协作的框架,其最初是为了页面抓取 (更确切来说, 网络抓取 )所设计的,使用它可以以快速.简单.可扩展的方式从网站中提取所需的数据.但目前Scrapy的用途十分广泛,可 ...

  8. Android--Google Map API V2使用

    一.获取API Key 1.先获取SHA-1 fingerprint 数字证书是有两种,一种是debug,还有release.前者只能用于测试:后者才可以用于实际产品. debug:在命令行中输入命令 ...

  9. 结合consul raft库理解raft

    一 入口 github.com/hashicorp/consul/agent/consul/server.go func (s *Server) setupRaft() error { 状态机,用于c ...

  10. Spark 系列(六)—— 累加器与广播变量

    一.简介 在 Spark 中,提供了两种类型的共享变量:累加器 (accumulator) 与广播变量 (broadcast variable): 累加器:用来对信息进行聚合,主要用于累计计数等场景: ...