// @flow import type { Monoid } from './Monoid' import type { Fold } from './Fold' import type { Either } from './Either' import * as either from './Either' export interface Getter { /** get the target of a [[Getter]] */ get(s: S): A } export function asFold(getter: Getter): Fold { return { foldMap(monoid: Monoid, f: (a: A) => M, s: S): M { return f(getter.get(s)) } } } /** join two Getter with the same target */ export function choice(getter1: Getter, getter2: Getter): Getter, A> { return { get(s: Either): A { return either.either(getter1.get, getter2.get, s) } } } export function composeGetter(ab: Getter, sa: Getter): Getter { return { get(s: S): B { return ab.get(sa.get(s)) } } }