A Lens is a functional concept which solves a very common problem: how to easily update a complex immutable structure, for this purpose Lens acts as a zoom into a record.
addressStreetNameLens : Lens Address String
addressStreetNameLens =
let
get a =
a.streetName
set sn a =
{ a | streetName = sn }
in
Lens get set
placeAddressLens : Lens Place Address
placeAddressLens =
let
get p =
p.address
set a p =
{ p | address = a }
in
Lens get set
placeStreetName : Lens Place String
placeStreetName =
compose placeAddressLens addressStreetNameLensComposes Lens a b with Lens b c and returns Lens a c
Modifies given function (b -> b) to be (a -> a) using Lens a b
addressStreetNameLens = Lens Address String
fx streetName = String.reverse streeName
fx2 = Lens.modify addressStreetNameLens fx
fx2 {streetName="abcdef"} == {streetName="fedcba"}Modifies given function (b,d) -> (b,d) to be (a,c) -> (a,c) using Lens a b and Lens c d
Modifies given function (b,d,f) -> (b,d,f) to be (a,c,e) -> (a,c,e) using Lens a b and Lens c d and Lens e f
Modifies given function (b -> (b,c)) to be (a,c) -> (a,c) using Lens a b and merge function
Zips Lens a c with Lens b d to form Lens ( a, b ) ( c, d )
Tuple Lens a b with Lens a c and returns Lens a (b,c)
Tuple Lens a b with Lens a c with Lens a d and returns Lens a (b,c,d)
Casts Iso a b to Lens a b
In order to create Lens we need to supply 2 functions: set and get