Skip to content

Commit

Permalink
Convenient Lens.compose infix operator alias
Browse files Browse the repository at this point in the history
  • Loading branch information
Bastes committed Dec 10, 2017
1 parent 894ec47 commit b35a590
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,29 @@ A Optional is a weaker Lens and a weaker Prism.
## Common
Common lenses/prisms/optionals that most projects will use.

#### Convenient infix operator for composing lenses.
Allows to chain lens composition for deeply nested structures:
```elm
fromAtoB : Lens A B
fromAtoB = Lense .b (\a b -> { a | b = b })
fromBtoC : Lens B C
fromBtoC = Lense .c (\b c -> { b | c = c })
fromCtoD : Lens C D
fromCtoD = Lense .d (\c d -> { c | d = d })
fromDtoE : Lens D E
fromDtoE = Lense .e (\d e -> { d | e = e })
fromAtoE : Lens A E
fromAtoE = fromAtoB <|> fromBtoC <|> fromCtoD <|> fromDtoE

a : A
a = { b: { c: { d: { e: "Whatever we want to get" } } } }

fromAtoE.get a
=> "Whatever we want to get"

fromAtoE.set a "What we want to set"
=> { b: { c: { d: { e: "What we want to set" } } } }
```
#### Convenient infix operator for composing optionals.
```elm
.getOption (maybe => array 2) (Just <| Array.fromList [ 10, 11, 12, 13 ])
Expand Down
28 changes: 28 additions & 0 deletions src/Monocle/Common.elm
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Monocle.Common exposing (..)

{-| Common lenses/prisms/optionals that most projects will use.
@docs (<|>)
@docs (=>)
@docs maybe
@docs array
Expand All @@ -18,6 +19,33 @@ import Monocle.Lens as Lens exposing (Lens)
import Monocle.Optional as Optional exposing (Optional)


{-| Convenient Infix operator for composing lenses.
Allows to chain lens composition for deeply nested structures:
fromAtoB : Lens A B
fromAtoB = Lense .b (\a b -> { a | b = b })
fromBtoC : Lens B C
fromBtoC = Lense .c (\b c -> { b | c = c })
fromCtoD : Lens C D
fromCtoD = Lense .d (\c d -> { c | d = d })
fromDtoE : Lens D E
fromDtoE = Lense .e (\d e -> { d | e = e })
fromAtoE : Lens A E
fromAtoE = fromAtoB <|> fromBtoC <|> fromCtoD <|> fromDtoE
a : A
a = { b: { c: { d: { e: "Whatever we want to get" } } } }
fromAtoE.get a
=> "Whatever we want to get"
fromAtoE.set a "What we want to set"
=> { b: { c: { d: { e: "What we want to set" } } } }
-}
(<|>) : Lens a b -> Lens b c -> Lens a c
(<|>) =
Lens.compose


{-| Convenient infix operator for composing optionals.
.getOption (maybe => array 2) (Just <| Array.fromList [ 10, 11, 12, 13 ])
Expand Down

0 comments on commit b35a590

Please sign in to comment.