Coder Social home page Coder Social logo

sigma-andex / purescript-barlow-lens Goto Github PK

View Code? Open in Web Editor NEW
39.0 2.0 2.0 159 KB

Increase your magnification ๐Ÿ”ญ and zoom deep into a record.

License: MIT No Attribution

Dhall 1.62% PureScript 98.38%
lenses profunctors profunctor-optics

purescript-barlow-lens's Introduction

purescript-barlow-lens ๐Ÿ”ญ

Barlow lens increases your magnification and let's you see the stars โœจ

In other words, barlow lens makes creating complex lenses such as record lenses super simple.

Installation

Note: Version v1.0.0 requires Purescript > v0.15.10. If you are on an older version, use v0.9.0 instead or upgrade your Purescript version.

spago install barlow-lens

tl;dr

import Data.Lens 
import Data.Lens.Barlow
import Data.String (toUpper)

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

spica = view (barlow @"zodiac.virgo.alpha") sky
-- "Spica"
upped = over (barlow @"zodiac.virgo.alpha") toUpper sky
-- { zodiac: { virgo: { alpha: "SPICA" } } }
    
-- alfa = view (barlow @"zodiac.virgo.alfa") sky 
-- doesn't compile

or use the barlow helpers to make it even shorter:

import Data.Lens.Barlow
import Data.Lens.Barlow.Helpers
import Data.String (toUpper)

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

spica = view @"zodiac.virgo.alpha" sky
-- "Spica"
upped = over @"zodiac.virgo.alpha" toUpper sky
-- { zodiac: { virgo: { alpha: "SPICA" } } }

Features

Barlow supports lens creation for the following types:

Deep sky ๐ŸŒŒ

Maybe

Use ? to zoom into a Maybe.

import Data.Maybe (Maybe(..))
import Data.Lens.Barlow
import Data.Lens.Barlow.Helpers

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

spica = preview @"zodiac?.virgo?.alpha?" sky

Either

Use < for Left and > for Right to zoom into an Either.

import Data.Either (Either(..))
import Data.Maybe (Maybe(..))
import Data.Lens.Barlow
import Data.Lens.Barlow.Helpers
import Data.String (toUpper)

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

spica = preview @"zodiac>.virgo?.alpha<" sky

Array and other Traversables

Use + to zoom into Traversables like Array.

import Data.Maybe (Maybe(..))
import Data.Lens.Barlow
import Data.Lens.Barlow.Helpers
import Data.String (toUpper)

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

upped = over @"zodiac+.virgo?.star" toUpper sky

Newtype

Use ! to zoom into a Newtype.

import Data.Lens.Barlow
import Data.Lens.Barlow.Helpers
import Data.Maybe (Maybe(..))
import Data.Newtype (class Newtype)

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

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

spica = preview @"zodiac?.virgo!.alpha" sky

Data types

Barlow supports zooming into arbitrary sum and product types as long as there is a Generic instance.

Use %<NAME> to zoom into sum types, where <NAME> is the name of your data constructor. E.g. %Virgo for the data constructor Virgo.

Use %<INDEX> to zoom into product types, where <INDEX> is an integer between 1 and 9. Note that counting for product types and tuples usually starts with 1 and not 0. So the first element of a product is %1.

It is more readable if you separate your sum lens from your product lens with a . dot.

import Data.Lens.Barlow
import Data.Lens.Barlow.Helpers
import Data.Generic.Rep (class Generic)
import Data.Show.Generic (genericShow)
import Data.String.Common (toUpper)

data Zodiac
  = Carina { alpha :: String } 
  | Virgo { alpha :: String } { beta :: String } { gamma :: String } { delta :: String } 
  | CanisMaior String 

derive instance Generic Zodiac _

-- Optionally derive a show instance
instance Show Zodiac where
  show = genericShow

sky =
  { zodiac:
      Virgo { alpha : "Spica"} { beta: "ฮฒ Vir"} { gamma: "ฮณ Vir B"} { delta: "ฮด Vir"}
  }

upped = over @"zodiac.%Virgo.%4.delta" toUpper sky
-- { zodiac: Virgo { alpha : "Spica"} { beta: "ฮฒ Vir"} { gamma: "ฮณ Vir B"} { delta: "ฮ” VIR"} }

Credits

This lib was heavily inspired by this incredible blog post. Thanks also to @i-am-the-slime for pushing me to go further and for reviewing my PRs.

purescript-barlow-lens's People

Contributors

i-am-the-slime avatar sigma-andex avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

purescript-barlow-lens's Issues

How to used newest version with Helpers?

Hi

The current package set only included 0.7.2 it seems, which doesn't have the helpers yet.
I've tried overriding it in packages.dhall with

in  upstream
  with barlow-lens.version = "v0.8.0"

but when compiling it says

spago build
[info] Installing 1 dependencies.
[info] Searching for packages cache metadata..
[info] Recent packages cache metadata found, using it..
[info] Installing "barlow-lens"
[info] Installation complete.
Compiling Data.Lens.Barlow.Types
Compiling Data.Lens.Barlow.Parser
Compiling Data.Lens.Barlow.Generic
Error found:
at .spago/barlow-lens/v0.8.0/src/Data/Lens/Barlow/Construction.purs:48:3 - 48:4 (line 48, column 3 - line 48, column 4)

  Unable to parse module:
  Unexpected token '('


See https://github.com/purescript/documentation/blob/master/errors/ErrorParsingModule.md for more information,
or to contribute content related to this error.


[error] Failed to build

How should I fix this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.