Hyperlinkv0.8.0-beta.28

Function

Function.composeconsteffect/Function.ts:486
<B, C>(bc: (b: B) => C): <A>(self: (a: A) => B) => (a: A) => C
<A, B, C>(self: (a: A) => B, bc: (b: B) => C): (a: A) => C

Composes two functions, ab and bc into a single function that takes in an argument a of type A and returns a result of type C. The result is obtained by first applying the ab function to a and then applying the bc function to the result of ab.

When to use

Use to compose exactly two unary functions into a reusable unary function.

Example (Composing two functions)

import { Function } from "effect"
import * as assert from "node:assert"

const increment = (n: number) => n + 1
const square = (n: number) => n * n

assert.strictEqual(Function.compose(increment, square)(2), 9)
combinatorsflowpipe
export const compose: {
  <B, C>(bc: (b: B) => C): <A>(self: (a: A) => B) => (a: A) => C
  <A, B, C>(self: (a: A) => B, bc: (b: B) => C): (a: A) => C
} = dual(2, <A, B, C>(ab: (a: A) => B, bc: (b: B) => C): (a: A) => C => (a) => bc(ab(a)))