typescript fp-ts如何使用函数来咖喱选项.fold

wfypjpf4  于 2023-02-05  发布在  TypeScript
关注(0)|答案(1)|浏览(130)

我想用这个形状创建一个函数来折叠一个选项

(option<A>) => ( fun: (<A>) => <B> ) => <B>

目前的代码看起来是这样的,但是我感觉有更惯用的方法来实现相同的效果

const stepIntrD = fu.pipe(
    lectureInteractionStats.children,
    option.fromNullable,
    option.chain(readonlyArray.findFirst<InteractionStatsData>((lis) => lis.id == step.id)),
    (v) => (fun: (_: InteractionStatsData) => boolean) => option.fold(() => false, fun)(v)
);
        
const wazVisited = stepIntrD(wasVisited);
brccelvz

brccelvz1#

下面是编写代码的其他一些更惯用的方法:

const stepIntrD = (fun: (_: InteractionStatsData) => boolean): boolean => fu.pipe(
    lectureInteractionStats.children,
    option.fromNullable,
    option.chain(readonlyArray.findFirst<InteractionStatsData>((lis) => lis.id == step.id)),
    option.fold(() => false, fun)
);

// This version evaluates interactionStatsData once, which is closer to your original code,
// whereas the version above evaluates that code every time stepIntrD is run
const interactionStatsData = fu.pipe(
    lectureInteractionStats.children,
    option.fromNullable,
    option.chain(readonlyArray.findFirst<InteractionStatsData>((lis) => lis.id == step.id))
);
const stepIntrD = (fun: (_: InteractionStatsData) => boolean): boolean => fu.pipe(
    interactionStatsData,
    option.fold(() => false, fun)
);

相关问题