This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { curry } from 'ramda' | |
| function maybeMap<A, B> (f: (val: A) => B, m: Maybe<A>): Maybe<B> { | |
| switch (m.type) { | |
| case MaybeType.Nothing: | |
| return Nothing() | |
| case MaybeType.Just: | |
| return Just(f(m.value)) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const safeHead = <T> (list: ReadonlyArray<T>): Maybe<T> => | |
| list.length === 0 | |
| ? Nothing() | |
| : Just(list[0]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { compose, toUpper } from 'ramda' | |
| function unsafeHead<T> (list: ReadonlyArray<T>): T { | |
| return list[0] | |
| } | |
| type UpperCaseHead = (list: ReadonlyArray<string>) => string | |
| const upperCaseHead: UpperCaseHead = compose( | |
| toUpper, | |
| unsafeHead, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const greet = (maybeName: Maybe<string>): string => { | |
| switch (maybeName.type) { | |
| case MaybeType.Nothing: | |
| return 'Pleased to meet you!' | |
| case MaybeType.Just: | |
| return `Good to see you again ${maybeName.value}` | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| enum MaybeType { | |
| Just = 'maybe-type__just', | |
| Nothing = 'maybe-type__nothing', | |
| } | |
| interface Just<T> { | |
| type: typeof MaybeType.Just | |
| value: T | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { curry } from 'ramda' | |
| enum MaybeType { | |
| Just = 'maybe-type__just', | |
| Nothing = 'maybe-type__nothing', | |
| } | |
| interface Just<T> extends Readonly<{ | |
| type: typeof MaybeType.Just | |
| value: T |
NewerOlder