Announcing `purescript-barlow-lens`: Lens for nested records

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 :slightly_smiling_face:
Check it out here: purescript-barlow-lens.
Happy for any feedback!

11 Likes

That’s pretty wild! Nice job!

We need type-applications :laughing:

4 Likes

Thx!
Yeah really looking forward for type-applications.

1 Like