Created
May 31, 2018 09:22
-
-
Save freddi301/29bb1aba3b47be88514f5210dddfdade to your computer and use it in GitHub Desktop.
Transducers in TypeScript
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
| type Reducer<Memo, Item> = (memo: Memo, item: Item) => Memo | |
| type Transducer<Memo, Item, ToItem> = (reducing: Reducer<Memo, ToItem>) => Reducer<Memo, Item> | |
| type Mapper<A, B> = (a: A) => B; | |
| const mapping = <Memo, Item, ToItem>(mapper: Mapper<Item, ToItem>): Transducer<Memo, Item, ToItem> => | |
| reducing => (memo, item) => | |
| reducing(memo, mapper(item)) | |
| type Predicate<T> = (item: T) => boolean; | |
| const filtering = <Memo, Item>(predicate: Predicate<Item>): Transducer<Memo, Item, Item> => | |
| reducing => (memo, item) => | |
| predicate(item) ? reducing(memo, item) : memo | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment