原文: 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. Dynamic Property Wizard 添加类提示 ATL类只能添加到MFC EXE 和MFC规则DLL项目或完全支持ATL的项目 错误提示解决方式

    在重新打开OPM项目添加ATL动态属性向导Dynamic Property Wizard提示错误 ATL类只能添加到MFC EXE 和MFC规则DLL项目或完全支持ATL的项目. 有效的解决方式,右键 ...

  2. python基础篇(五)

    PYTHON基础篇(五) 算法初识 什么是算法 二分查找算法 ♣一:算法初识 A:什么是算法 根据人们长时间接触以来,发现计算机在计算某些一些简单的数据的时候会表现的比较笨拙,而这些数据的计算会消耗大 ...

  3. leetcode 55 Jump Game 三种方法,回溯、动态规划、贪心

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  4. 前端HTML学习心得

    学习最好的效果就是理论加实践--Hanks!!!(给大家打鸡血的哈哈哈) 前面的学习我教大家怎么搭建简单的前端开发环境,现在我教大家怎么使用工具学习(从入门到放弃哈哈,不不不,这是以前的我,现在我下了 ...

  5. win10 双网卡设置内网和外网同时访问

    当前环境是内网使用固定ip 用有线连接 外网自动获取使用wifi模块连接wifi cmd窗口下运行route print -4 打印路由信息 首先删除 所有0.0.0.0的路由,也就是默认设置 rou ...

  6. Windows 下升级 node & npm 到最新版本

    查询 Node 的安装目录where node 升级 Node:在官网下载最新的安装包,直接安装即可.https://nodejs.org/ 升级 npmnpm install -g npm 使用 n ...

  7. Java中遍历Map对象的4种方法

    java中的所有map都实现了Map接口,以下方法适用于任何map实现(HashMap, TreeMap, LinkedHashMap, Hashtable, 等等). HashMap<Inte ...

  8. 20 IO流(十七)——Commons工具包,FileUtils(二)——copy方法

    copy方法 package com.sxt.copy; import java.io.File; import java.io.IOException; import java.net.URL; i ...

  9. 05- if-else语句、循环for

    if-else 语句 if是条件语句.if语句的语法是 if 条件{ #注意大括号和条件之间不能换行 执行代码 } if语句还包括else if 和 else 部分 package main impo ...

  10. Linux 中的 ~/. 表示的意思

    在Linux中, ~ 表示用户的目录, 如用户名是Gavin, 那么~/表示  /home/Gavin 所以~/. 表示 用户目录下的隐藏文件. 扩展: 若以用户身份登录 ~ 表示 /home  目录 ...