I'm new to fp-ts
, and I was wondering whether there's a utility/pattern for splitting an array into multiple partitions based on another array of indices/keys, such that the regular functions eg map()
and foldMap()
would operate on the partitions (sub-arrays of the original array):
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)
Is there any existing utility for this, or should I write my own aliases, eg:
const P = { map: (callbackfn) => (partitioned) => R.map(A.map(callbackfn))(partitioned) }
No, there isn't any existing utility for this. I would define my own like this:
import {flow} from 'fp-ts/function';
const P = { map: flow(A.map, R.map)) };
flow
is left-to-right function composition.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.