Skip to content

Maybe(Monad)

此处阅读到的 Maybe 可能与其他函数式语言(如 Haskell)中的 Data Maybe 并不相同。PureEval 中的 Maybe 是一个 Data Maybe 与 Class Monad 的融合。

Maybe

Maybe 类型内包含一个不确定的值。

如果一个确定的值在 Maybe 中执行发生错误,该 Maybe 也会变为 Nothing。

undefinednull 值都将被认为 Nothing

  • Type

Maybe.of()

将一个值提升为 类型。

  • Type
  • Example
js
Maybe.of(1); //Just 1
Maybe.of(null); //Nothing
Maybe.of(1); //Just 1
Maybe.of(null); //Nothing

Maybe.is()

判断一个值是否为 类型。

  • Type
  • Example
js
Maybe.is(Just(1)); //true
Maybe.is(Nothing); //true
Maybe.is(Just(1)); //true
Maybe.is(Nothing); //true

Maybe.isNothing()

判断该 是否为

  • Type
  • Example
js
Just(1).isNothing(); //false
Just(null).isNothing(); //true
Just(1).isNothing(); //false
Just(null).isNothing(); //true

Maybe.map()

将 Maybe 中的值经过函数处理映射为新的值,并构造新的 Maybe 来保存。

  • Type
  • Example
js
Just(1).map(add(114513)); //Just 114514
Just(1).map(add(114513)); //Just 114514

Maybe.fold()

将函数应用到 中的

传入两个单元函数参数,分别代表 不为 的处理情况。

当匹配到 时,对应函数将会应用到 上,其函数值将作为返回值。

  • Type
  • Example
js
Just('The body next door').fold(() => 'Nothing', id); //"The body next door"
Just('The body next door').fold(() => 'Nothing', id); //"The body next door"

Maybe.chain()

在 Maybe 后链接一个返回 Maybe 类型的函数,可作为链式调用。

传入一个返回 Maybe 类型的函数,返回值为链接后的 Maybe 对象。

  • Type
  • Example
js
const getUser = (id) => {
	if (id === 1) {
		return Just({ id: 1, name: 'Alice' });
	} else {
		return Nothing;
	}
};

const getAddress = (user) => {
	if (user.name === 'Alice') {
		return Just({ street: '123 Main St', city: 'Anytown' });
	} else {
		return Nothing;
	}
};

const getCity = (address) => {
	if (address.street === '123 Main St') {
		return Just(address.city);
	} else {
		return Nothing;
	}
};

const city = Just(1)
	.chain(getUser)
	.chain(getAddress)
	.chain(getCity)
	.fold(
		() => 'Unknown',
		(city) => city
	);

console.log(city); // 'Anytown'
const getUser = (id) => {
	if (id === 1) {
		return Just({ id: 1, name: 'Alice' });
	} else {
		return Nothing;
	}
};

const getAddress = (user) => {
	if (user.name === 'Alice') {
		return Just({ street: '123 Main St', city: 'Anytown' });
	} else {
		return Nothing;
	}
};

const getCity = (address) => {
	if (address.street === '123 Main St') {
		return Just(address.city);
	} else {
		return Nothing;
	}
};

const city = Just(1)
	.chain(getUser)
	.chain(getAddress)
	.chain(getCity)
	.fold(
		() => 'Unknown',
		(city) => city
	);

console.log(city); // 'Anytown'

Just()

类型的构造函数。

实际同 Maybe.of

  • Type
  • Example
js
Just(1); //Just 1
Just(1); //Just 1

Nothing

类型的构造函数。

PureEval released under the GPL-3.0 License.