當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript monet.Maybe類代碼示例

本文整理匯總了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;未經允許,請勿轉載。