monad 是函数式编程中的一个基本概念,它提供了一种以结构化方式处理计算和数据转换的方法。单子有多种类型,每种类型都旨在解决特定问题并处理不同类型的数据和效果。
单子是一种抽象,允许对包装值进行链接操作。它由三个主要属性定义:
maybe monad 用于处理可选值。它表示可能失败或返回 null 或未定义的计算。
class maybe { constructor(value) { this.value = value; } static of(value) { return new maybe(value); } isnothing() { return this.value === null || this.value === undefined; } map(fn) { return this.isnothing() ? this : maybe.of(fn(this.value)); } flatmap(fn) { return this.isnothing() ? this : fn(this.value); } } // usage const maybevalue = maybe.of('hello') .map(str => str.touppercase()) .flatmap(str => maybe.of(`${str} world`)); console.log(maybevalue); // maybe { value: 'hello world' }
either monad 用于处理可以返回成功值(右)或错误值(左)的计算。
class either { constructor(value, isright = true) { this.value = value; this.isright = isright; } static right(value) { return new either(value, true); } static left(value) { return new either(value, false); } map(fn) { return this.isright ? either.right(fn(this.value)) : this; } flatmap(fn) { return this.isright ? fn(this.value) : this; } } // usage const rightvalue = either.right(5) .map(x => x + 1) .flatmap(x => either.right(x * 2)); console.log(rightvalue); // either { value: 12, isright: true } const leftvalue = either.left('error') .map(x => x + 1) .flatmap(x => either.right(x * 2)); console.log(leftvalue); // either { value: 'error', isright: false }
promise monad 用于处理异步计算。
const fetchdata = url => { return new promise((resolve, reject) => { settimeout(() => { resolve(`data from ${url}`); }, 1000); }); }; // usage fetchdata('https://api.example.com') .then(data => { console.log(data); // 'data from https://api.example.com' return fetchdata('https://api.example.com/2'); }) .then(data => { console.log(data); // 'data from https://api.example.com/2' }) .catch(error => { console.error(error); });
list monad 用于处理生成值列表的计算。
class list { constructor(values) { this.values = values; } static of(values) { return new list(values); } map(fn) { return list.of(this.values.map(fn)); } flatmap(fn) { return list.of(this.values.flatmap(value => fn(value).values)); } } // usage const list = list.of([1, 2, 3]) .map(x => x + 1) .flatmap(x => list.of([x, x * 2])); console.log(list); // list { values: [ 2, 4, 3, 6, 4, 8 ] }
reader monad 用于处理依赖于某些共享环境或配置的计算。
class reader { constructor(fn) { this.fn = fn; } static of(value) { return new reader(() => value); } map(fn) { return new reader(env => fn(this.fn(env))); } flatmap(fn) { return new reader(env => fn(this.fn(env)).fn(env)); } run(env) { return this.fn(env); } } // usage const config = { baseurl: 'https://api.example.com' }; const fetchuser = new reader(env => `${env.baseurl}/user`); const fetchposts = new reader(env => `${env.baseurl}/posts`); const fetchuserandposts = fetchuser.flatmap(userurl => fetchposts.map(postsurl => ({ userurl, postsurl })) ); console.log(fetchuserandposts.run(config)); // { userurl: 'https://api.example.com/user', postsurl: 'https://api.example.com/posts' }
writer monad 用于处理生成值以及日志或附加数据的计算。
class writer { constructor(value, log) { this.value = value; this.log = log; } static of(value) { return new writer(value, ''); } map(fn) { const result = fn(this.value); return new writer(result.value, this.log + result.log); } flatmap(fn) { const result = fn(this.value); return new writer(result.value, this.log + result.log); } tell(log) { return new writer(this.value, this.log + log); } } // usage const writer = writer.of(3) .map(value => new writer(value + 1, 'incrementedn')) .flatmap(value => new writer(value * 2, 'doubledn')); console.log(writer); // writer { value: 8, log: 'incrementedndoubledn' }
state monad 用于处理维护状态的计算。
class State { constructor(runState) { this.runState = runState; } static of(value) { return new State(state => [value, state]); } map(fn) { return new State(state => { const [value, newState] = this.runState(state); return [fn(value), newState]; }); } flatMap(fn) { return new State(state => { const [value, newState] = this.runState(state); return fn(value).runState(newState); }); } run(initialState) { return this.runState(initialState); } } // Usage const increment = new State(state => [state + 1, state + 1]); const result = increment .flatMap(() => increment) .flatMap(() => increment) .run(0); console.log(result); // [3, 3]
monad 提供了一种结构化且可预测的方式来处理函数式编程中的计算和数据转换。每种类型的 monad 都有特定的用途,从使用 maybe monad 处理可选值到使用 promise monad 管理异步操作。