Package

purescript-barlow-lens

Repository
sigma-andex/purescript-barlow-lens
License
MIT-0
Uploaded by
sigma-andex
Published on
2021-04-07T08:35:09Z

Barlow lens increases your magnification and let's you see the stars ✨

Ehh, wat ?

Barlow lens is a lens for records that makes it easy to zoom deep into the record.

Installation

spago install barlow-lens

Usage

sky =
  { zodiac:
      { virgo:
          { alpha: "Spica"
          }
      }
  }

view (barlow (key :: _ "zodiac.virgo.alpha")) sky
-- "Spica"
over (barlow (key :: _ "zodiac.virgo.alpha")) toUpper sky
-- { zodiac: { virgo: { alpha: "SPICA" } } }
    
-- view (barlow (key :: _ "zodiac.virgo.alfa")) sky 
-- doesn't compile

Deep sky 🌌

Now you can also zoom into Maybes using ?...

sky =
  { zodiac:
      Just
        { virgo:
            Just
              { alpha: Just "Spica"
              }
        }
  }

preview (barlow (key :: _ "zodiac?.virgo?.alpha?")) sky

... and Eithers using < for Left and > for Right...

sky =
  { zodiac:
      Right
        { virgo:
            Just
              { alpha: Left "Spica"
              }
        }
  }

preview (barlow (key :: _ "zodiac>.virgo?.alpha<")) sky

... and Arrays (and other Traversables) using + ...

sky =
  { zodiac:
      [ { virgo:
            Just
              { star: "Spica"
              }
        }
      , { virgo:
            Just
              { star: "Serpentis"
              }
        }
      ]
  }

over (barlow (key :: _ "zodiac+.virgo?.star")) toUpper sky

... and Newtypes using !

newtype Alpha = Alpha { alpha :: String }
instance alphaNT :: Newtype Alpha { alpha :: String

sky =
  { zodiac:
      Just
        { virgo:
            Alpha { alpha: "Spica"
            }
        }
  }

preview (barlow (key :: _ "zodiac?.virgo!.alpha")) sky

Credits

This lib was heavily inspired by this incredible blog post.