Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- type Composer = <A, B, C> (
- f: (x: B) => C,
- g: (x: A) => B,
- ) => (x: A) => C
- const compose: Composer = (f, g) => x => f(g(x))
- type Option<A> = Some<A> | None
- interface Some<A> {
- _tag: 'Some',
- value: A
- }
- interface None {
- _tag: 'None'
- }
- const some = <A>(x: A): Option<A> => ({ _tag: 'Some', value: x })
- const none: Option<never> = {_tag: 'None'}
- const isNone = <A>(x: Option<A>): x is None => x._tag === 'None'
- type Inc = (x: number) => number
- const inc: Inc = x => x + 1
- type Div2 = (x: number) => Option<number>
- const div2: Div2 = x => x === 0 ? none : some(2/x)
- const composed = compose(
- (x: Option<number>) => isNone(x) ? none : some(inc(x.value)),
- div2
- )
- console.log(composed(4))
- console.log(composed(0))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement