arturopala / elm-monocle / Monocle.Common

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

maybe : Monocle.Optional.Optional (Maybe a) a

Step into a Maybe value.

maybe.set 5 Nothing
    > Just 5

array : Basics.Int -> Monocle.Optional.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 : Basics.Int -> Monocle.Optional.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 : Monocle.Iso.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 -> Monocle.Optional.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 : Monocle.Optional.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 : Monocle.Lens.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 : Monocle.Lens.Lens ( a, b ) a

Step into the first element of a pair.

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

second : Monocle.Lens.Lens ( a, b ) b

Step into the second element of a pair.

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