If you are like me and love lenses, you probably have noted that it is a bit cumbersome to create lenses for nested records. E.g. to extract the alpha
value in the following record you have to do:
sky = { zodiac: { virgo: { alpha: "Spica" } } }
zodiacL = prop (Proxy :: Proxy "zodiac")
virgoL = prop (Proxy :: Proxy "virgo")
alphaL = prop (Proxy :: Proxy "alpha")
alpha = view (zodiacL <<< virgoL <<< alphaL) sky
-- Spica
With barlow-lens
this is reduced to:
sky = { zodiac: { virgo: { alpha: "Spica" } } }
alpha = view (barlow (key :: _ "zodiac.virgo.alpha")) sky
barlow-lens
also supports Maybe
using ?
, Either
using <
and >
and Array
using +
. So you can do e.g.:
sky =
{ zodiac:
[ { virgo: Just { star: Left "Spica" } }
, { virgo: Just { star: Right "Serpentis" } }
]
}
sky' = over (barlow (key :: _ "zodiac+.virgo?.star>")) toUpper sky
-- { zodiac: [ { virgo: Just { star: Left "Spica" }}, { virgo: Just { star: Right "SERPENTIS" }}]}
And of course this is all type-safe
Check it out here: purescript-barlow-lens.
Happy for any feedback!