import { chain, map, Apply } from "fp-ts/lib/IO";
import { log } from "fp-ts/lib/Console";
import { pipe } from "fp-ts/lib/function";
import * as D from "fp-ts/lib/Date";
import { sequenceT } from "fp-ts/lib/Apply";
import { v4 as uuidv4 } from "uuid";
export const Article = (title: string) =>
([id, now]: [string, Date]) => ({
id,
title,
createdAt: now
});
const createArticle = (title: string) =>
pipe(sequenceT(Apply)(uuidv4, D.create), map(Article(title)));
const program = pipe(
createArticle("hello"),
chain(log)
);
program();
在上面的例子中,由于Article
需要2个作为副作用的参数。问题是关于createArticle
,以及它是否可以写成一个无点函数。
您可以通过使用ap
将值应用于函数来反向执行管道操作。我必须创建一个函数来帮助TypeScript计算of
的类型。
import { chain, map, Apply, of, ap } from "fp-ts/lib/IO";
import { log } from "fp-ts/lib/Console";
import { flow, pipe } from "fp-ts/lib/function";
import * as D from "fp-ts/lib/Date";
import { sequenceT } from "fp-ts/lib/Apply";
import { v4 as uuidv4 } from "uuid";
const Article =
(title: string) =>
([id, now]: [string, Date]) => ({
id,
title,
createdAt: now,
});
const ofString = (str: string) => of(str);
const createArticle = flow(
ofString,
map(Article),
ap(sequenceT(Apply)(uuidv4, D.create))
);
const program = pipe(createArticle("hello"), chain(log));
program();