本文整理汇总了TypeScript中src/monet.Maybe类的典型用法代码示例。如果您正苦于以下问题:TypeScript Maybe类的具体用法?TypeScript Maybe怎么用?TypeScript Maybe使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Maybe类的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的TypeScript代码示例。
示例1: log3b
function log3b(message: Maybe<string>): void {
if (message.isSome()) {
console.log(message.some())
} else {
console.warn('Nothing to log.')
}
}
开发者ID:cwmyers,项目名称:monet.js,代码行数:7,代码来源:maybe-spec.ts
示例2: log3
function log3(message: Maybe<string>): void {
if (message.isJust()) {
console.log(message.just())
} else {
console.warn('Nothing to log.')
}
}
开发者ID:cwmyers,项目名称:monet.js,代码行数:7,代码来源:maybe-spec.ts
示例3: log
function log(message: Maybe<string>): IO<void> {
return message.cata(() => IO(() => {
console.warn('Nothing to log.')
}), msg => IO(() => {
console.log(msg);
}));
}
开发者ID:cwmyers,项目名称:monet.js,代码行数:7,代码来源:maybe-spec.ts
示例4: NonEmptyList
import { NonEmptyList, NEL, List, Nil, Maybe } from 'src/monet';
const nel: NEL<string> = NonEmptyList('a', List('b'));
const nonempty: NonEmptyList<number> = NEL(0, List(12));
const nelBool: NEL<boolean> = nel.flatMap((str: string) => NEL.of(!str, List(Boolean(str))));
const nil = nelBool.filter(a => a).filter(a => !a);
const nel2 = NEL.fromList(nil).cata(() => NEL.unit(true), a => a);
const nel3 = NEL.fromList(Nil).cata(() => NEL.pure(true), a => a);
const taken: NonEmptyList<number> = nel2.takeLeft(nel3).takeRight(nonempty);
const tail: List<number> = taken.tail();
const xx: NEL<number> = taken.tails().map(e => e.head()).mapTails(t => t.extract()).cojoin().bind(t => t).reverse();
const yy: NEL<string> = xx.cobind(t => t.ap(NonEmptyList(String))).coflatMap(t => t.extract()).head();
const foldedL: Maybe<NEL<string>> = NonEmptyList.fromList(tail).map(t =>
t.tails().foldLeft(yy)((acc, n) => acc.concat(n.ap(NEL(String)))));
const foldedR: NonEmptyList<boolean> = foldedL.map(f => f.cojoin()).map(ff => ff.foldRight(nelBool)((v, acc) =>
acc.append(v.map(Boolean)))).orJust(nelBool);
console.log(true === foldedR.reduceLeft((acc, n) => acc && n));
开发者ID:cwmyers,项目名称:monet.js,代码行数:18,代码来源:nel-spec.ts
示例5:
const wrappedGreeting: Maybe<string> = wrapped.bind(v => Maybe.fromNull(v.payload));
开发者ID:cwmyers,项目名称:monet.js,代码行数:1,代码来源:maybe-spec.ts
示例6: getType
function getType(action) {
return Maybe.fromNull(action.type).filter(t => t !== 'MESSAGE');
}
开发者ID:cwmyers,项目名称:monet.js,代码行数:3,代码来源:maybe-spec.ts
示例7: getMessage
function getMessage(msg: IMessage): Maybe<IMessage> {
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!');
开发者ID:cwmyers,项目名称:monet.js,代码行数:29,代码来源:maybe-spec.ts
注:本文中的src/monet.Maybe类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论