【发布时间】:2023-02-01 10:44:43
【问题描述】:
我是 fp-ts 的新手,我想知道是否有一个实用程序/模式可以根据另一个索引/键数组将一个数组拆分为多个分区,这样常规函数例如map() 和 foldMap() 将对分区(原始数组的子数组)进行操作:
const x = ["a", "b", "c", "d", "e", "f"];
const p = [0, 0, 1, 2, 1, 0];
const partition = (partion: number[]) => (x: any[]) => {
return x.reduce((result, nextValue, index) => {
if (!(partion[index] in result)) result[partion[index]] = [];
result[partion[index]].push(nextValue);
return result;
}, {});
};
const xPartitioned = partition(p)(x);
const shout = (x: string) => x.toUpperCase() + `!`;
// Works as intended: { "0": ["A!", "B", "F!"], "1": ["C!", "E!"], "2": ["D!"] }
const res1 = R.map(A.map(shout))(xPartitioned);
// Would like to be able to do something like:
const res2 = P.map(shout)(xPartitioned)
是否有任何现有的实用程序,或者我应该写我自己的别名,例如:
const P = { map: (callbackfn) => (partitioned) => R.map(A.map(callbackfn))(partitioned) }
【问题讨论】:
标签: typescript fp-ts