All Projects β†’ sigma-andex β†’ purescript-barlow-lens

sigma-andex / purescript-barlow-lens

Licence: other
Increase your magnification πŸ”­ and zoom deep into a record.

Programming Languages

purescript
368 projects
Dhall
116 projects

Projects that are alternatives of or similar to purescript-barlow-lens

Elm Monocle
Functional abstractions to manipulate complex records in Elm - Iso, Prism, Lens, Optional, Traversal.
Stars: ✭ 137 (+328.13%)
Mutual labels:  lenses
lenticular.ts
(Yet another) implementation of functional lenses in JavaScript/TypeScript.
Stars: ✭ 29 (-9.37%)
Mutual labels:  lenses
concave
🧐 Lens-like state management (for React).
Stars: ✭ 13 (-59.37%)
Mutual labels:  lenses
Documentation
How does it all fit together?
Stars: ✭ 177 (+453.13%)
Mutual labels:  lenses
xml-lens
XML Optics library for Scala
Stars: ✭ 32 (+0%)
Mutual labels:  lenses
exercises-monocle
Scala Exercises for the Scala Optics library Monocle http://julien-truffaut.github.io/Monocle/
Stars: ✭ 12 (-62.5%)
Mutual labels:  lenses
Lens Regex Pcre
Text lenses using PCRE regexes
Stars: ✭ 116 (+262.5%)
Mutual labels:  lenses
optics.js
πŸ”­ Lenses, Prisms and Traversals in JavaScript!
Stars: ✭ 46 (+43.75%)
Mutual labels:  lenses
lentes
Functional references for Clojure and ClojureScript
Stars: ✭ 80 (+150%)
Mutual labels:  lenses
putting-lenses-to-work
A presentation for BayHac 2017 on how I uses lenses at work
Stars: ✭ 73 (+128.13%)
Mutual labels:  lenses
Python Lenses
A python lens library for manipulating deeply nested immutable structures
Stars: ✭ 179 (+459.38%)
Mutual labels:  lenses
Microlens
A lightweight (but compatible with β€˜lens’) lenses library
Stars: ✭ 254 (+693.75%)
Mutual labels:  lenses
program-imperatively-using-purescript
Program imperatively using PureScript, State monad and lenses
Stars: ✭ 23 (-28.12%)
Mutual labels:  lenses
Sauron
Yet another Scala lens macro
Stars: ✭ 166 (+418.75%)
Mutual labels:  lenses
lenses-jdbc
Lenses.io JDBC driver for Apache Kafka
Stars: ✭ 20 (-37.5%)
Mutual labels:  lenses
Typed
The TypeScript Standard Library
Stars: ✭ 124 (+287.5%)
Mutual labels:  lenses
futils
Utilities for generic functional programming
Stars: ✭ 21 (-34.37%)
Mutual labels:  lenses
lenses-go
Lenses.io CLI (command-line interface)
Stars: ✭ 34 (+6.25%)
Mutual labels:  lenses
tomland
🏝 Bidirectional TOML serialization
Stars: ✭ 103 (+221.88%)
Mutual labels:  profunctors
pathex
Fastest way to access data in Elixir
Stars: ✭ 242 (+656.25%)
Mutual labels:  lenses

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

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 (key :: _ "zodiac.virgo.alpha")) sky
-- "Spica"
upped = over (barlow (key :: _ "zodiac.virgo.alpha")) toUpper sky
-- { zodiac: { virgo: { alpha: "SPICA" } } }
    
-- alfa = view (barlow (key :: _ "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 (key :: _ "zodiac.virgo.alpha") sky
-- "Spica"
upped = over (key :: _ "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 (key :: _ "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 (key :: _ "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 (key :: _ "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 (key :: _ "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 (key :: _ "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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].