diff --git a/src/monocle.ts b/src/monocle.ts deleted file mode 100644 index 65cc848a1..000000000 --- a/src/monocle.ts +++ /dev/null @@ -1,250 +0,0 @@ -import { HKT } from './HKT' -import { Monoid, monoidArray, monoidAll, monoidAny } from './Monoid' -import { Applicative } from './Applicative' -import { Foldable, foldMap } from './Foldable' -import { Traversable } from './Traversable' -import * as option from './Option' -import { Some, Option } from './Option' -import { identity, constant, Predicate } from './function' -import * as id from './Identity' -import * as con from './Const' - -/* - Laws: - 1. get . reverseGet = identity - 2. reversetGet . get = identity -*/ -export class Iso { - constructor( - public get: (s: S) => A, - public reverseGet: (a: A) => S - ) { } - - /** compose a Iso with a Iso */ - compose(ab: Iso): Iso { - return new Iso( - s => ab.get(this.get(s)), - b => this.reverseGet(ab.reverseGet(b)) - ) - } - - modify(f: (a: A) => A, s: S): S { - return this.reverseGet(f(this.get(s))) - } - - /** view a ISO as a Lens */ - asLens(): Lens { - return new Lens( - this.get, - this.reverseGet - ) - } - - /** view a ISO as a Prism */ - asPrism(): Prism { - return new Prism( - s => new Some(this.get(s)), - this.reverseGet - ) - } -} - -/* - Laws: - 1. get(set(a, s)) = a - 2. set(get(s), s) = s - 3. set(a, set(a, s)) = set(a, s) -*/ -export class Lens { - constructor( - public get: (s: S) => A, - public set: (a: A, s: S) => S - ) { } - - modify(f: (a: A) => A, s: S): S { - return this.set(f(this.get(s)), s) - } - - /** compose a Lens with a Lens */ - compose(ab: Lens): Lens { - return new Lens( - s => ab.get(this.get(s)), - (b, s) => this.set(ab.set(b, this.get(s)), s) - ) - } - - /** view a Lens as a Option */ - asOptional(): Optional { - return new Optional( - s => new Some(this.get(s)), - this.set - ) - } -} - -/* - Laws: - 1. getOption(s).fold(identity, reverseGet) = s - 2. getOption(reverseGet(a)) = Some(a) -*/ -export class Prism { - constructor( - public getOption: (s: S) => Option, - public reverseGet: (a: A) => S - ) { } - - modify(f: (a: A) => A, s: S): S { - return this.modifyOption(f, s) - .fold(constant(s), identity) - } - - modifyOption(f: (a: A) => A, s: S): Option { - return this.getOption(s) - .map(a => this.reverseGet(f(a))) - } - - /** compose a Prism with a Prism */ - compose(ab: Prism): Prism { - return new Prism( - s => this.getOption(s).chain(a => ab.getOption(a)), - b => this.reverseGet(ab.reverseGet(b)) - ) - } - - /** view a Prism as a Optional */ - asOptional(): Optional { - return new Optional( - this.getOption, - this.reverseGet - ) - } -} - -/* - Laws: - 1. getOption(s).fold(identity, set(_, s)) = s - 2. getOption(set(a, s)) = getOption(s).map(_ => a) - 3. set(a, set(a, s)) = set(a, s) -*/ -export class Optional { - constructor( - public getOption: (s: S) => Option, - public set: (a: A, s: S) => S - ){} - - modify(f: (a: A) => A, s: S): S { - return this.modifyOption(f, s) - .fold(constant(s), identity) - } - - modifyOption(f: (a: A) => A, s: S): Option { - return this.getOption(s) - .map(a => this.set(f(a), s)) - } - - /** compose a Optional with a Optional */ - compose(ab: Optional): Optional { - return new Optional( - s => this.getOption(s).chain(a => ab.getOption(a)), - (b, s) => this.modify(a => ab.set(b, a), s) - ) - } - - /** view a Options as a Traversal */ - asTraversal(): Traversal { - return new Traversal( - (applicative: Applicative, f: (a: A) => HKT, s: S): HKT => - this.getOption(s).fold( - () => applicative.of(s), - a => applicative.map(a => this.set(a, s), f(a)) - ) - ) - } -} - -export class Traversal { - constructor( - // Van Laarhoven representation - public modifyF: (applicative: Applicative, f: (a: A) => HKT, s: S) => HKT - ){} - - modify(f: (a: A) => A, s: S): S { - return (this.modifyF(id, a => id.of(f(a)), s) as id.Identity).extract() - } - - set(a: A, s: S): S { - return this.modify(constant(a), s) - } - - /** compose a Traversal with a Traversal */ - compose(ab: Traversal): Traversal { - return new Traversal( - (applicative: Applicative, f: (a: B) => HKT, s: S): HKT => - this.modifyF(applicative, a => ab.modifyF(applicative, f, a), s) - ) - } - - /** view a Traversal as a Fold */ - asFold(): Fold { - return new Fold( - (monoid: Monoid, f: (a: A) => M, s: S): M => - (this.modifyF(con.getApplicative(monoid), a => new con.Const(f(a)), s) as con.Const).fold(identity) - ) - } -} - -/** create a Traversal from a Traversable */ -export function fromTraversable(traversable: Traversable): Traversal, A> { - return new Traversal, A>( - (applicative: Applicative, f: (a: A) => HKT, s: HKT): HKT> => - traversable.traverse(applicative, f, s) - ) -} - -export class Fold { - constructor( - public foldMap: (monoid: Monoid, f: (a: A) => M, s: S) => M - ){} - - /** compose a Fold with a Fold */ - compose(ab: Fold): Fold { - return new Fold( - (monoid: Monoid, f: (b: B) => M, s: S): M => - this.foldMap(monoid, a => ab.foldMap(monoid, f, a), s) - ) - } - - /** get all the targets of a Fold */ - getAll(s: S): Array { - return this.foldMap(monoidArray, a => [a], s) - } - - /** find the first target of a Fold matching the predicate */ - find(p: Predicate, s: S): Option { - return this.foldMap(option.monoidFirst, a => p(a) ? option.of(a) : option.none, s) - } - - /** get the first target of a Fold */ - headOption(s: S): Option { - return this.find(() => true, s) - } - - /** check if at least one target satisfies the predicate */ - exist(p: Predicate, s: S): boolean { - return this.foldMap(monoidAny, p, s) - } - - /** check if all targets satisfy the predicate */ - all(p: Predicate, s: S): boolean { - return this.foldMap(monoidAll, p, s) - } - -} - -/** create a Fold from a Foldable */ -export function fromFoldable(fold: Foldable): Fold, A> { - return new Fold( - (monoid: Monoid, f: (a: A) => M, s: HKT): M => - foldMap(fold, monoid, f, s) - ) -}