【问题标题】:fp-ts Split an array into multiple partitionsfp-ts 将数组拆分成多个分区
【发布时间】: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


    【解决方案1】:

    不,没有任何现有的实用程序。我会这样定义我自己的:

    import {flow} from 'fp-ts/function';
    
    const P = { map: flow(A.map, R.map)) };
    

    flow 是从左到右的函数组合。

    【讨论】:

      猜你喜欢
      • 2021-08-07
      • 2021-08-14
      • 1970-01-01
      • 2016-11-16
      • 2017-02-04
      • 2021-12-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多