惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
Y
Y Combinator Blog
F
Fortinet All Blogs
H
Hackread – Cybersecurity News, Data Breaches, AI and More
N
Netflix TechBlog - Medium
T
Tailwind CSS Blog
aimingoo的专栏
aimingoo的专栏
博客园 - Franky
T
The Blog of Author Tim Ferriss
D
DataBreaches.Net
量子位
博客园 - 三生石上(FineUI控件)
I
InfoQ
Engineering at Meta
Engineering at Meta
WordPress大学
WordPress大学
阮一峰的网络日志
阮一峰的网络日志
爱范儿
爱范儿
D
Docker
美团技术团队
雷峰网
雷峰网
U
Unit 42
Stack Overflow Blog
Stack Overflow Blog
Recent Announcements
Recent Announcements
人人都是产品经理
人人都是产品经理

szhshp 的第三边境研究所

假设, AI 把我代替的那一刻真的到来 | szhshp 的第三边境研究所 小傻瓜都能懂的 AstrBot QQ 机器人集成 MCP 功能实战指南 | szhshp 的第三边境研究所 《智人之上: 从石器时代到 AI 时代的信息网络简史》阅读笔记 | szhshp 的第三边境研究所 iPadOS 26 无法设置空间场景图片壁纸的解决方法 | szhshp 的第三边境研究所 《一路云海》(终) | szhshp 的第三边境研究所 《一路云海》(四): 如何不按套路旅行 | szhshp 的第三边境研究所 《一路云海》(三): In Ya Mellow Tone | szhshp 的第三边境研究所 《一路云海》(二): 关西世博参观纪实 | szhshp 的第三边境研究所 《一路云海》(一): 新的征程 | szhshp 的第三边境研究所 2025 大阪世博会 [ 3 天前-先到先得 ] 阶段 场馆预约必中独家攻略 | szhshp 的第三边境研究所 Hackathon 随想 | szhshp 的第三边境研究所 一杯双皮奶 | szhshp 的第三边境研究所 Armbian + CasaOS + NAS 配置指南 | szhshp 的第三边境研究所 Docker 构建镜像报错: error getting credentials - err: exit status 1, out: `` | szhshp 的第三边境研究所 Disqus RIP! 论过高的维护成本如何治疗固执的坏习惯 | szhshp 的第三边境研究所 炸弹猫桌游变体规则 | szhshp 的第三边境研究所 《小岛经济学》阅读笔记 | szhshp 的第三边境研究所 《金钱心理学》阅读笔记 | szhshp 的第三边境研究所 为知笔记 RIP: 迁移剩余的笔记 | szhshp 的第三边境研究所 2025 博客第十年展望 - 再见我的过去 | szhshp 的第三边境研究所 我在独立游戏里面致敬的作品 | szhshp 的第三边境研究所 《How to make thing faster》阅读笔记 | szhshp 的第三边境研究所 《The Art of Clean Code》阅读笔记 | szhshp 的第三边境研究所 《Clean Architecture: A Craftsman Guide to Software Structure and Design》阅读笔记 | szhshp 的第三边境研究所 《How AI Works》阅读笔记 | szhshp 的第三边境研究所 游戏策划废案 - Project Uranus | szhshp 的第三边境研究所 游戏策划废案 - Project X | szhshp 的第三边境研究所 人生第一款独立游戏开发复盘 | szhshp 的第三边境研究所 Trap of Life | szhshp 的第三边境研究所 如果我用手搓了个暗物质雏形 | szhshp 的第三边境研究所
《The Joy of Javascript》- 2 - Functor/Monad | szhshp 的...
2022-06-20 · via szhshp 的第三边境研究所

Meta

目录

相关文章

一本书里面内容较多, 因此分成了多篇 Post, 可以从此处看到相关文章:

Functor & Monad | 函子和单子

实现 FP 需要保证一些函数的输入和输出规范化. 以方便 compose 进行链式调用.

例如: arr.map(x=>x+1).map(x=>x+2) 就是因为 .map() 方法的输入输出的规范化以使得其可以链式调用, 多个 map 方法可以进行 compose: const testMethod = compose(map(capitalize), map(getAscii));

Functor

Functor 的特性

  1. A functor is anything (such as an object) that can be mapped over or that implements the map interface properly
  2. 函数式编程不直接操作值, 而是由函子完成
  3. 行为类似于一个容器, 容器会包裹不同的输入, 之后会返回一个统一的结构 (A Functor is a container which can be mapped upon by a Unary function.)

原生的 Array 类型就是一个 Functor

[1, 2, 3, 4].map(add).map(add)  

Functor 的设计原则

  1. identity: 如果给 map 传递一个返回自己的方法, 那么得到的结果和自身相等
    • F.map(x=>x) === F
  2. composition: 多个方法可以进行链式调用, 比如下方的 f(g(x)) 可以写成 f.map(g).map(x)
    • F.map((x) => f(g(x))) === F.map(g).map(f)

Functor 实例

一般会实现以下属性和方法: 1. _value : 通过这个属性从容器中取得实际的值 2. of() : 给方法提供初始值 3. map() : map 方法接收一个 fn, fn 去得到一个新的值

class Container {
  static of(value) {
    return new Container(value)
  }
  constructor(value) {
    this._value = value
  }
  map(fn) {
    return Container.of(fn(this._value))
  }
}

let c = Container.of(5)
  .map((x) => x * x)
  .map((x) => x + 2)

console.log(c) // Container { _value: 27 }

More Functor Example

Functor with Mixin
const Functor = {
  map(f = identity) {
    /* 注意当前 Functor 需要用到 Container 的 get 方法 (因为 val 设置成了 private), 所有扩展了这个 Functor 的类都需要拥有这个 get 方法 */
    return this.constructor.of(f(this.get()));
  },
};

const double = (x) => x * 2;
const triple = (x) => x * 3;

class Container {
  #val;  /* 注意这个地方的 val 是 private, 因此必须使用 get 方法才能够得到对应的值. */
  constructor(value) {
    this.#val = value;
  }
  static of(value) {
    return new Container(value);
  }

  get() {
    return this.#val;
  }
}

Object.assign(Container.prototype, Functor);

console.log(Container.of(2).get()); /* 2 */
console.log(Container.of(2).map(double).get()); /* 4 */
console.log(Container.of(2).map(double).map(triple).get()); /* 12 */
Maybe Functor
class Maybe {
  static of(value) {
    return new Maybe(value);
  }
  constructor(value) {
    this._value = value;
  }
  map(fn) {
    /* 在这个地方进行了一些特殊的处理 */
    return this.isNothing() ? Maybe.of(null) : Maybe.of(fn(this._value));
  }
  isNothing() {
    return this._value === null || this._value === undefined;
  }
}

let obj = new Maybe(5)
  .map((x) => null)  /* 此处对应的值已经转化成了 null */
  .map((x) => x * x);

console.log(obj);

Monad

Monad 的特性

  • Monad 也是类似函子的概念, 区别在于有时候函子的返回值不是我们需要的类型
    • 使用 compose 要求输入和输出一致比如 number -> number
    • 但是有一些操作会返回更多的类型比如 number -> number[]
      • 这个时候需要一个 flatMap 方法进行拍平
    • 又或者可能会出现返回嵌套类型比如 number -> { res: number }
      • 这个时候可能需要对 Object 进行额外的 mapping
  • Monad 就是通过一个额外的方法来将返回不一致的情况进行处理, 使其方便后期 compose 复合操作

Monad 的设计原则

注意 chain 方法根据不同的实现可能会有不同的名称

  • Left identity: M.of(a).chain(f) === f(a)
  • Right identity: m.chain(M.of) === m
  • Associativity: m.chain(f).chain(g) === m.chain(x => f(x).chain(g))

Monad 实例

/* Number -> Number[], 如果连续调用将会得到多层嵌套数组 */
var f = (x) => [x ** 2];
var g = (x) => [x * 3];

/* Monad Container */
var Container = class {
  of(value) {
    return new Container(value);
  }

  constructor(value) {
    this.value = value;
  }

  map(f) {
    return new Container(f(this.value));
  }

  chain(f) {
    /* 此处对于 Functor 的 map 进行了一些额外操作 */
    /* 这个方法根据需求不同会有不同实现, 比如对一些 side effect 的处理. */
    return new Container(f(this.value).flat()[0]);
  }
};

var test = new Container(2);

// test.of(2).map(f).map(g); // 报错, Functor 的 map 方法无法处理数组

test.of(2).chain(f).chain(g); // Container { value: 12 } 

Interoperation with Class | 给 Class 扩展 Functor 和 Monad

const Functor = {
  map(f = identity) {
    return this.constructor.of(f(this.get()));
  },
};


/* 将 Functor 扩展到 Monad 中 */
const Monad = Object.assign({}, Functor, {
  flatMap(f) {
    return this.map(f).get();
  },
});

/* 如此一来 SomeClass 就同时拥有了 map 和 flatMap, SomeClass 就成为了一个 Monad */
Object.assign(SomeClass.prototype, Functor, Monad);

Summary

  • JavaScript offers behavior delegation via implicit links and mixins for building objects in a compositional manner.
  • Behavior delegation is the natural way to model objects in JavaScript. It uses the implicit delegation mechanism present in JavaScript’s lookup process and the prototype chain.
  • Object concatenation offers a simple approach based on structural object composition, which allows you to build objects by attaching (embedding) behavior from other independent objects.
  • You can use mixins to extend objects (or classes) dynamically and favor structural composition over inheritance.
  • Mixins address the issue of multiple inheritance through a mechanism known as mixin linearization.
  • JavaScript offers a shortcut for Object.assign by using the spread operator, although Object.assign and the spread operator are not interchangeable.