簡體   English   中英

fp-ts 將數組拆分成多個分區

[英]fp-ts Split an array into multiple partitions

我是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) }

不,沒有任何現有的實用程序。 我會這樣定義我自己的:

import {flow} from 'fp-ts/function';

const P = { map: flow(A.map, R.map)) };

flow是從左到右function組成。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM