Advertisement
STANAANDREY

compose ts

Feb 16th, 2025
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. type Incrementer = (x: number) => number;
  2. const increment: Incrementer = x => x + 1;
  3. console.assert(increment(2) == 3)
  4.  
  5.  
  6. type Tostr = (x: number) => string;
  7. const tostr: Tostr = (x: number) => `"${x}"`
  8. console.assert(tostr(2) == `"2"`)
  9.  
  10. type Composer = <A, B, C> (
  11.     f: (x: B) => C,
  12.     g: (x: A) => B,
  13. ) => (x: A) => C
  14. const compose: Composer = (f, g) => x => f(g(x))
  15.  
  16. type IncrNTostr = (x: number) => string
  17. const incrNTostr: IncrNTostr = compose(tostr, increment)
  18.  
  19. console.assert(incrNTostr(5) == `"6"`)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement