Skip to content

Instantly share code, notes, and snippets.

@galanggg
Created June 7, 2024 05:50
Show Gist options
  • Save galanggg/49a1adbee3b08490c7079781f7657bb1 to your computer and use it in GitHub Desktop.
Save galanggg/49a1adbee3b08490c7079781f7657bb1 to your computer and use it in GitHub Desktop.

Revisions

  1. galanggg created this gist Jun 7, 2024.
    27 changes: 27 additions & 0 deletions function-composition.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    //define type for increment function

    type Increment = (x: number) => number
    const increment: Increment = x => x + 1

    //define type for toString function

    type Tostring = (x: number) => string
    const toString: Tostring = x => `"${x}"`

    //define type for increment then to string function
    type IncrementThenToString = (x: number) => string
    const increment_then_tostring: IncrementThenToString = x => toString(increment(x))

    console.log(increment_then_tostring(6))

    //make Compose Function

    type Compose = <A,B,C> {
    f: (x: B) => C,
    g: (x: A) => B
    => (x: A) => C
    }
    const compose: Compose = (f,g) => x => f(g(x))
    const increment_then_tostring2: IncrementThenToString = compose(tostring, increment)

    console.log(increment_then_tostring2(6))