如果你想“正确”地做到这一点,那么你需要将所有非确定性(非纯)函数调用包装在 IO 或 IOEither 中(取决于它们是否可以失败)。
所以首先让我们定义哪些函数调用是“纯”的,哪些不是。我发现最容易想到的就是这样 - 如果函数 ALWAYS 为相同的输入提供相同的输出并且不会导致任何可观察到的副作用,那么它就是纯粹的。
“相同的输出”并不意味着参照平等,它意味着结构/行为平等。所以如果你的函数返回另一个函数,这个返回的函数可能不是同一个函数对象,但它的行为必须相同(原始函数被认为是纯函数)。
所以在这些方面,以下是正确的:
cherio.load
是纯的
$
是纯的
.get
不纯
.find
不纯
.attr
不纯
.map
是纯的
.filter
是纯的
现在让我们为所有非纯函数调用创建包装器:
const getIO = selection => IO.of(selection.get())
const findIO = (...args) => selection => IO.of(selection.find(...args))
const attrIO = (...args) => element => IO.of(element.attr(...args))
需要注意的一点是,在这里我们将非纯函数(.attr
或attrIO
包装版本)应用于元素数组。attrIO
如果我们只是在数组上进行映射,我们会返回Array<IO<result>>
,但它不是超级有用,我们需要它IO<Array<result>>
。为了实现这一点,我们需要traverse
而不是map
https://gcanti.github.io/fp-ts/modules/Traversable.ts.html。
所以如果你有一个数组rows
并且你想应用attrIO
它,你可以这样做:
import { array } from 'fp-ts/lib/Array';
import { io } from 'fp-ts/lib/IO';
const rows: Array<...> = ...;
// normal map
const mapped: Array<IO<...>> = rows.map(attrIO('data-test'));
// same result as above `mapped`, but in fp-ts way instead of native array map
const mappedFpTs: Array<IO<...>> = array.map(rows, attrIO('data-test'));
// now applying traverse instead of map to "flip" the `IO` with `Array` in the type signature
const result: IO<Array<...>> = array.traverse(io)(rows, attrIO('data-test'));
然后将所有东西组装在一起:
import { array } from 'fp-ts/lib/Array';
import { io } from 'fp-ts/lib/IO';
import { flow } from 'fp-ts/lib/function';
const getIO = selection => IO.of(selection.get())
const findIO = (...args) => selection => IO.of(selection.find(...args))
const attrIO = (...args) => element => IO.of(element.attr(...args))
const getTests = (text: string) => {
const $ = cheerio.load(text);
return pipe(
$('table tr'),
getIO,
IO.chain(rows => array.traverse(io)(rows, flow($, findIO('a')))),
IO.chain(links => array.traverse(io)(links, flow(
attrIO('data-test'),
IO.map(a => a ? a : null)
))),
IO.map(links => links.filter(v => v != null))
);
}
现在getTests
为您返回tests
原始代码中变量中相同元素的 IO。
免责声明:我没有通过编译器运行代码,它可能有一些拼写错误或错误。您可能还需要付出一些努力来使其成为强类型。
编辑:
如果您想保留有关错误的信息(在这种情况下,data-test
其中一个元素缺少属性a
),您有多种选择。当前getTests
返回一个IO<string[]>
. 要在此处放置错误信息,您可以执行以下操作:
IO<Either<Error, string>[]>
- 一个 IO,它返回一个数组,其中每个元素都是错误或值。要使用它,您仍然需要稍后进行过滤以消除错误。这是最灵活的解决方案,因为您不会丢失任何信息,但感觉也有点没用,因为Either<Error, string>
在这种情况下与string | null
.
import * as Either from 'fp-ts/lib/Either';
const attrIO = (...args) => element: IO<Either<Error, string>> => IO.of(Either.fromNullable(new Error("not found"))(element.attr(...args) ? element.attr(...args): null));
const getTests = (text: string): IO<Either<Error, string>[]> => {
const $ = cheerio.load(text);
return pipe(
$('table tr'),
getIO,
IO.chain(rows => array.traverse(io)(rows, flow($, findIO('a')))),
IO.chain(links => array.traverse(io)(links, attrIO('data-test')))
);
}
IOEither<Error, string[]>
- 返回错误或值数组的 IO。这里最常见的做法是在获得第一个缺失属性时返回 Error,如果所有值都没有错误,则返回一个值数组。同样,如果有任何错误,此解决方案会丢失有关正确值的信息,并且会丢失有关除第一个错误之外的所有错误的信息。
import * as Either from 'fp-ts/lib/Either';
import * as IOEither from 'fp-ts/lib/IOEither';
const { ioEither } = IOEither;
const attrIOEither = (...args) => element: IOEither<Error, string> => IOEither.fromEither(Either.fromNullable(new Error("not found"))(element.attr(...args) ? element.attr(...args): null));
const getTests = (text: string): IOEither<Error, string[]> => {
const $ = cheerio.load(text);
return pipe(
$('table tr'),
getIO,
IO.chain(rows => array.traverse(io)(rows, flow($, findIO('a')))),
IOEither.rightIO, // "lift" IO to IOEither context
IOEither.chain(links => array.traverse(ioEither)(links, attrIOEither('data-test')))
);
}
IOEither<Error[], string[]>
- 一个返回错误数组或值数组的 IO。如果有任何错误,这个会聚合错误,如果没有错误,则会聚合值。如果有任何错误,此解决方案会丢失有关正确值的信息。
这种方法在实践中比上述方法更罕见,实施起来也更棘手。一个常见的用例是验证检查,为此有一个单子转换器https://gcanti.github.io/fp-ts/modules/ValidationT.ts.html。我对它没有太多经验,所以不能在这个话题上多说。
IO<{ errors: Error[], values: string[] }>
- 返回包含错误和值的对象的 IO。此解决方案也不会丢失任何信息,但实施起来稍微复杂一些。
这样做的规范方法是为结果对象定义一个 monoid 实例,{ errors: Error[], values: string[] }
然后使用以下方法聚合结果foldMap
:
import { Monoid } from 'fp-ts/lib/Monoid';
type Result = { errors: Error[], values: string[] };
const resultMonoid: Monoid<Result> = {
empty: {
errors: [],
values: []
},
concat(a, b) {
return {
errors: [].concat(a.errors, b.errors),
values: [].concat(a.values, b.values)
};
}
};
const attrIO = (...args) => element: IO<Result> => {
const value = element.attr(...args);
if (value) {
return {
errors: [],
values: [value]
};
} else {
return {
errors: [new Error('not found')],
values: []
};
};
const getTests = (text: string): IO<Result> => {
const $ = cheerio.load(text);
return pipe(
$('table tr'),
getIO,
IO.chain(rows => array.traverse(io)(rows, flow($, findIO('a')))),
IO.chain(links => array.traverse(io)(links, attrIO('data-test'))),
IO.map(results => array.foldMap(resultMonoid)(results, x => x))
);
}