arturopala/elm-monocle/2.1.0

NOTE: the latest version is 2.2.0

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 : Iso b c -> Iso a b -> 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 : Prism b c -> Iso a b -> 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 : Lens b c -> Iso a b -> 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 : Optional b c -> Iso a b -> 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

From a Prism

prismWithIso : Iso b c -> Prism a b -> 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 : Prism b c -> Prism a b -> 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 : Lens b c -> Prism a b -> 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 : Optional b c -> Prism a b -> 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

From a Lens

lensWithIso : Iso b c -> Lens a b -> 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 : Prism b c -> Lens a b -> 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 : Lens b c -> Lens a b -> 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 : Optional b c -> Lens a b -> 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

From an Optional

optionalWithIso : Iso b c -> Optional a b -> 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 : Prism b c -> Optional a b -> 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 : Lens b c -> Optional a b -> 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 : Optional b c -> Optional a b -> 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