arturopala / elm-monocle / Monocle.Compose

Pipeline-friendly composition helpers

Using these allow to compose an "outer" optic with an "inner" other optic.

Optics in functional programming languages that support typeclasses can be expressed as functions that compose through the composition operator (just like any other functions) ; in Elm (plus typclasses), it would look like this:

lensAtoB >> lensBtoC >> lensCtoD == lensAtoD

lensAtoB >> optionalBtoC >> prismCtoD == optionalAtoC

But Elm doesn't support typeclasses, so we're stuck with defining composition functions that look similar like this:

import Monocle.Compose as Compose
lensAtoB
  |> Compose.lensWithLens lensBtoC
  |> Compose.lensWithLens lensCtoD
  == lensAtoD
lensAtoB
  |> Compose.lensWithOptional optionalBtoC
  |> Compose.optionalWithPrism prismCtoD
  == optionalAtoC

This is arguably more "discoverable" and maybe more readable, if more verbose.

From an Iso

isoWithIso : Monocle.Iso.Iso b c -> Monocle.Iso.Iso a b -> Monocle.Iso.Iso a c

pipeline-friendly composition between two Iso

ab : Iso A B
ab = ..

bc : Iso B C
bc = ..

ac : Iso A C
ac =
  ab
    |> ComposeIso.isoWithIso bc

isoWithPrism : Monocle.Prism.Prism b c -> Monocle.Iso.Iso a b -> Monocle.Prism.Prism a c

pipeline-friendly composition between an outer Iso and an inner Prism (the result is a Prism)

ab : Iso A B
ab = ..

bc : Prism B C
bc = ..

ac : Prism A C
ac =
  ab
    |> ComposeIso.isoWithPrism bc

isoWithLens : Monocle.Lens.Lens b c -> Monocle.Iso.Iso a b -> Monocle.Lens.Lens a c

pipeline-friendly composition between an outer Iso and an inner Lens (the result is a Lens)

ab : Iso A B
ab = ..

bc : Lens B C
bc = ..

ac : Lens A C
ac =
  ab
    |> ComposeIso.isoWithLens bc

isoWithOptional : Monocle.Optional.Optional b c -> Monocle.Iso.Iso a b -> Monocle.Optional.Optional a c

pipeline-friendly composition between an outer Iso and an inner Optional (the result is an Optional)

ab : Iso A B
ab = ..

bc : Optional B C
bc = ..

ac : Optional A C
ac =
  ab
    |> ComposeIso.isoWithOptional bc

isoWithTraversal : Monocle.Traversal.Traversal b c -> Monocle.Iso.Iso a b -> (c -> c) -> a -> a

pipeline-friendly composition between an outer Iso and an inner Traversal (the result is a Traversal)

ab : Iso A B
ab = ..

bc : Traversal B C
bc = ..

ac : Traversal A C
ac =
  ab
    |> ComposeIso.isoWithTraversal bc

From a Prism

prismWithIso : Monocle.Iso.Iso b c -> Monocle.Prism.Prism a b -> Monocle.Prism.Prism a c

pipeline-friendly composition between an outer Prism and an inner Iso (the result is a Prism)

ab : Prism A B
ab = ..

bc : Iso B C
bc = ..

ac : Prism A C
ac =
  ab
    |> ComposePrism.prismWithIso bc

prismWithPrism : Monocle.Prism.Prism b c -> Monocle.Prism.Prism a b -> Monocle.Prism.Prism a c

pipeline-friendly composition between two Prisms

ab : Prism A B
ab = ..

bc : Prism B C
bc = ..

ac : Prism A C
ac =
  ab
    |> ComposePrism.prismWithPrism bc

prismWithLens : Monocle.Lens.Lens b c -> Monocle.Prism.Prism a b -> Monocle.Optional.Optional a c

pipeline-friendly composition between an outer Prism and an inner Lens (the result is an Optional)

ab : Prism A B
ab = ..

bc : Lens B C
bc = ..

ac : Optional A C
ac =
  ab
    |> ComposePrism.prismWithLens bc

prismWithOptional : Monocle.Optional.Optional b c -> Monocle.Prism.Prism a b -> Monocle.Optional.Optional a c

pipeline-friendly composition between an outer Prism and an inner Optional (the result is an Optional)

ab : Prism A B
ab = ..

bc : Optional B C
bc = ..

ac : Optional A C
ac =
  ab
    |> ComposePrism.prismWithOptional bc

prismWithTraversal : Monocle.Traversal.Traversal b c -> Monocle.Prism.Prism a b -> (c -> c) -> a -> a

pipeline-friendly composition between an outer Prism and an inner Traversal (the result is a Traversal)

ab : Prism A B
ab = ..

bc : Traversal B C
bc = ..

ac : Traversal A C
ac =
  ab
    |> ComposePrism.prismWithTraversal bc

From a Lens

lensWithIso : Monocle.Iso.Iso b c -> Monocle.Lens.Lens a b -> Monocle.Lens.Lens a c

pipeline-friendly composition between an outer Lens and an inner Iso (the result is a Lens)

ab : Lens A B
ab = ..

bc : Iso B C
bc = ..

ac : Lens A C
ac =
  ab
    |> Compose.lensWithIso bc

lensWithPrism : Monocle.Prism.Prism b c -> Monocle.Lens.Lens a b -> Monocle.Optional.Optional a c

pipeline-friendly composition between an outer Lens and an inner Prism (the result is an Optional)

ab : Lens A B
ab = ..

bc : Prism B C
bc = ..

ac : Optional A C
ac =
  ab
    |> Compose.lensWithPrism bc

lensWithLens : Monocle.Lens.Lens b c -> Monocle.Lens.Lens a b -> Monocle.Lens.Lens a c

pipeline-friendly composition between two Lenses

ab : Lens A B
ab = ..

bc : Lens B C
bc = ..

ac : Lens A C
ac =
  ab
    |> Compose.lensWithLens bc

lensWithOptional : Monocle.Optional.Optional b c -> Monocle.Lens.Lens a b -> Monocle.Optional.Optional a c

pipeline-friendly composition between an outer Lens and an inner Optional (the result is an Optional)

ab : Lens A B
ab = ..

bc : Optional B C
bc = ..

ac : Optional A C
ac =
  ab
    |> Compose.lensWithOptional bc

lensWithTraversal : Monocle.Traversal.Traversal b c -> Monocle.Lens.Lens a b -> (c -> c) -> a -> a

pipeline-friendly composition between an outer Lens and an inner Traversal (the result is an Traversal)

ab : Lens A B
ab = ..

bc : Traversal B C
bc = ..

ac : Traversal A C
ac =
  ab
    |> Compose.lensWithTraversal bc

From an Optional

optionalWithIso : Monocle.Iso.Iso b c -> Monocle.Optional.Optional a b -> Monocle.Optional.Optional a c

pipeline-friendly composition between an outer Optional and an inner Iso (the result is an Optional)

ab : Optional A B
ab = ..

bc : Iso B C
bc = ..

ac : Optional A C
ac =
  ab
    |> ComposeOptional.optionalWithIso bc

optionalWithPrism : Monocle.Prism.Prism b c -> Monocle.Optional.Optional a b -> Monocle.Optional.Optional a c

pipeline-friendly composition between an outer Optional and an inner Prism (the result is an Optional)

ab : Optional A B
ab = ..

bc : Prism B C
bc = ..

ac : Optional A C
ac =
  ab
    |> ComposeOptional.withPrism bc

optionalWithLens : Monocle.Lens.Lens b c -> Monocle.Optional.Optional a b -> Monocle.Optional.Optional a c

pipeline-friendly composition between an outer Optional and an inner Lens (the result is an Optional)

ab : Optional A B
ab = ..

bc : Lens B C
bc = ..

ac : Optional A C
ac =
  ab
    |> ComposeOptional.optionalWithLens bc

optionalWithOptional : Monocle.Optional.Optional b c -> Monocle.Optional.Optional a b -> Monocle.Optional.Optional a c

pipeline-friendly composition between two Optionals

ab : Optional A B
ab = ..

bc : Optional B C
bc = ..

ac : Optional A C
ac =
  ab
    |> ComposeOptional.optionalWithOptional bc

optionalWithTraversal : Monocle.Traversal.Traversal b c -> Monocle.Optional.Optional a b -> (c -> c) -> a -> a

pipeline-friendly composition between an outer Optional and an inner Traversal (the result is a Optional)

ab : Optional A B
ab = ..

bc : Traversal B C
bc = ..

ac : Optional A C
ac =
  ab
    |> ComposeOptional.optionalWithTraversal bc

From a Traversal

traversalWithIso : Monocle.Iso.Iso b c -> Monocle.Traversal.Traversal a b -> (c -> c) -> a -> a

pipeline-friendly composition between an outer Traversal and an inner Iso (the result is a Traversal)

ab : Traversal A B
ab = ..

bc : Iso B C
bc = ..

ac : Traversal A C
ac =
  ab
    |> ComposeTraversal.traversalWithIso bc

traversalWithPrism : Monocle.Prism.Prism b c -> Monocle.Traversal.Traversal a b -> (c -> c) -> a -> a

pipeline-friendly composition between an outer Traversal and an inner Prism (the result is a Traversal)

ab : Traversal A B
ab = ..

bc : Prism B C
bc = ..

ac : Traversal A C
ac =
  ab
    |> ComposeTraversal.traversalWithPrism bc

traversalWithLens : Monocle.Lens.Lens b c -> Monocle.Traversal.Traversal a b -> (c -> c) -> a -> a

pipeline-friendly composition between an outer Traversal and an inner Lens (the result is a Traversal)

ab : Traversal A B
ab = ..

bc : Lens B C
bc = ..

ac : Traversal A C
ac =
  ab
    |> ComposeTraversal.traversalWithLens bc

traversalWithOptional : Monocle.Optional.Optional b c -> Monocle.Traversal.Traversal a b -> (c -> c) -> a -> a

pipeline-friendly composition between an outer Traversal and an inner Optional (the result is a Traversal)

ab : Traversal A B
ab = ..

bc : Optional B C
bc = ..

ac : Traversal A C
ac =
  ab
    |> ComposeTraversal.traversalWithOptional bc

traversalWithTraversal : Monocle.Traversal.Traversal b c -> Monocle.Traversal.Traversal a b -> (c -> c) -> a -> a

pipeline-friendly composition between two Traversals (the result is a Traversal)

ab : Traversal A B
ab = ..

bc : Traversal B C
bc = ..

ac : Traversal A C
ac =
  ab
    |> ComposeTraversal.traversalWithTraversal bc