arturopala/elm-monocle/1.7.0

NOTE: this package is not compatible with Elm 0.19.1

Monocle.Common

Common lenses/prisms/optionals that most projects will use.

(<|>) : Lens a b -> Lens b c -> Lens a c

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" } } } }
(=>) : Optional a b -> Optional b c -> Optional a c

Convenient infix operator for composing optionals.

.getOption (maybe => array 2) (Just <| Array.fromList [ 10, 11, 12, 13 ])
=> 12
(=|>) : Optional a b -> Lens b c -> Optional a c

Convenient infix operator for composing optional with lens.

.getOption (maybe =|> id) (Just { id = 12 })
=> 12
maybe : Optional (Maybe a) a

Step into a Maybe value.

maybe.set 5 Nothing
> Just 5
array : Int -> Optional (Array a) a

Step into an Array at the given index.

.getOption (array 2) (Array.fromList [ 10, 11, 12, 13 ])
> Just 12


.getOption (array 8) (Array.fromList [ 10, 11, 12, 13 ])
> Nothing
list : Int -> Optional (List a) a

Step into an List at the given index. (shortcut to avoid converting a List to an Array ; if it is too slow, consider using array and an Array instead of a List in your data)

.getOption (list 2) [ 10, 11, 12, 13 ]
> Just 12


.getOption (list 8) [ 10, 11, 12, 13 ]
> Nothing
listToArray : Iso (List a) (Array a)

Iso that converts a list to an array.

.get listToArray [1, 2, 3, 4] == Array.fromList [1, 2, 3, 4]
> True


.reverseGet listToArray (Array.fromList [9, 8, 7, 6]) == [ 9, 8, 7, 6 ]
> True
dict : comparable -> Optional (Dict comparable v) v

Step into a Dict with the given key.

.getOption (dict "Tom") (Dict.fromList [ ( "Tom", "Cat" ) ])
> Just "Cat"


.getOption (dict "Jerry") (Dict.fromList [ ( "Tom", "Cat" ) ])
> Nothing
result : Optional (Result e a) a

Step into the success value of a Result.

result.getOption (Ok 5)
> Just 5


result.getOption (Err "500")
> Nothing
id : Lens { a | id : b } b

Step into a record with an id key.

id.get { id = 1000, name = ... }
> Just 1000

Since records with an id field are incredible common, this is included for convenience. It also serves as a simple recipe for creating record lenses.

first : Lens ( a, b ) a

Step into the first element of a pair.

first.get ( 'a', 'b' )
> Just 'a'
second : Lens ( a, b ) b

Step into the second element of a pair.

second.get ( 'a', 'b' )
> Just 'b'