本文整理汇总了TypeScript中src/monet.Maybe.chain方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Maybe.chain方法的具体用法?TypeScript Maybe.chain怎么用?TypeScript Maybe.chain使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类src/monet.Maybe
的用法示例。
在下文中一共展示了Maybe.chain方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: Just
if (msg && msg.hasOwnProperty('type')) {
return Just(msg);
}
return None<IMessage>();
}
const wrapped = getMessage({type: 'MESSAGE', payload: 'Hello World'});
console.assert(wrapped.isSome() === wrapped.isJust());
console.assert(wrapped.isNone() === wrapped.isNothing());
console.assert(!(wrapped.isNone() === wrapped.isJust()));
console.assert(wrapped.equals(Some({type: 'MESSAGE', payload: 'Hello World'})))
const wrappedGreeting: Maybe<string> = wrapped.bind(v => Maybe.fromNull(v.payload));
const wrappedType = wrapped.flatMap(getType);
const unpacked = wrappedGreeting.chain(g => wrappedType.map(t => `Type: ${t} - Body: ${g}`));
log(unpacked).run();
log2(getMessage(null).flatMap(getType)).performUnsafeIO();
log3(Some('Message!'));
log3b(getMessage({type: 'x', payload: null}).chain(getType));
const name: string = None<string>().orSome('NAME');
const surname: string = Nothing<string>().orJust('SURNAME');
const message: string = Maybe.Just(0).filter(Boolean).map(String).orElse(unpacked).orJust('Hi!');
const messageCopy: string = Nothing().ap(unpacked.map(m => () => m)).orSome('Hi!');
console.log(name, surname, message, messageCopy);