Skip to content

Instantly share code, notes, and snippets.

@freddi301
Created May 31, 2018 09:22
Show Gist options
  • Select an option

  • Save freddi301/29bb1aba3b47be88514f5210dddfdade to your computer and use it in GitHub Desktop.

Select an option

Save freddi301/29bb1aba3b47be88514f5210dddfdade to your computer and use it in GitHub Desktop.
Transducers in TypeScript
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