Giter Site home page Giter Site logo

pbrant / purescript-derive-lenses Goto Github PK

View Code? Open in Web Editor NEW

This project forked from paf31/purescript-derive-lenses

0.0 1.0 0.0 12 KB

A little utility to derive lenses and prisms for data types in PureScript

License: MIT License

Haskell 97.59% PureScript 2.41%

purescript-derive-lenses's Introduction

purescript-derive-lenses

A little utility to derive lenses and prisms for data types in PureScript

Usage

Provide a PureScript module on standard input, and a new module will be returned on standard output

How it Works

This tool generates the following types of optics:

  • Prisms for data constructors which have zero or one argument
  • Lenses for object fields appearing in data constructors

The generated lenses are compatible with the purescript-profunctor-lenses library.

Example

Input file:

module Data.Tree where

type Foo = {foo :: Int}

data Tree a
  = Leaf
  | Branch { l :: Tree a, value :: a, r :: Tree a }

Output file:

module Data.Tree.Lenses where

import Prelude (Unit, unit, const)
import Data.Lens (Lens, PrismP, lens, prism)
import Data.Either (Either(..))
import Data.Tree


foo :: forall a b r. Lens { "foo" :: a | r } { "foo" :: b | r } a b
foo = lens _."foo" (_ { "foo" = _ })

l :: forall a b r. Lens { "l" :: a | r } { "l" :: b | r } a b
l = lens _."l" (_ { "l" = _ })

value :: forall a b r. Lens { "value" :: a | r } { "value" :: b | r } a b
value = lens _."value" (_ { "value" = _ })

r :: forall a b r. Lens { "r" :: a | r } { "r" :: b | r } a b
r = lens _."r" (_ { "r" = _ })

_Leaf :: forall a. PrismP (Tree a) Unit
_Leaf = prism (const Leaf) unwrap
  where
  unwrap Leaf = Right unit
  unwrap y = Left y

_Branch :: forall a. PrismP (Tree a) { r :: Tree a
                                     , value :: a
                                     , l :: Tree a
                                     }
_Branch = prism Branch unwrap
  where
  unwrap (Branch x) = Right x
  unwrap y = Left y

These optics can now be composed in the usual ways:

leftRight :: forall a. PrismP (Tree a) (Tree a)
leftRight = r <<< _Branch <<< l <<< _Branch

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.