【问题标题】:Function composition in TypescriptTypescript 中的函数组合
【发布时间】:2021-04-22 20:20:58
【问题描述】:

我需要实现一个函数 (flow),将它的两个参数组成一个函数式组合。

要求:

  • flow 函数应将两个函数作为参数(称为fabfbc)并返回一个新函数,该函数将fab 应用于其输入参数,然后将fbc 应用于结果值。

提示:

  • 第一个函数称为fab,因为它将A 类型的值映射到B 类型。 fbc 也是如此。

问题是我不知道将什么作为参数传递给 fab 函数,我是 Typescript 新手,希望能提供任何帮助!

这是我目前的代码:

type Flow = <A, B, C>(
  fab: (a: A) => B, // Takes a function from A to B
  fbc: (b: B) => C // Takes a function from B to C
) => (a: A) => C; // Returns a function from A to C

export const flow: Flow = (fab, fbc) => {
  return fbc(fab());
};

【问题讨论】:

标签: javascript typescript function


【解决方案1】:
type Flow = <A, B, C>(
  fab: (a: A) => B, // Takes a function from A to B
  fbc: (b: B) => C // Takes a function from B to C
) => (a: A) => C; // Returns a function from A to C

export const flow: Flow = (fab, fbc) => (a) => fbc(fab(a));

【讨论】:

    猜你喜欢
    • 2020-06-15
    • 1970-01-01
    • 2019-12-18
    • 2021-12-15
    • 2013-01-17
    • 1970-01-01
    • 1970-01-01
    • 2021-05-05
    • 2022-01-22
    相关资源
    最近更新 更多