Git Product home page Git Product logo

lenses-ppx's Introduction

What are GADTs?

GADTs: A primer

Why

Differently from normal lenses/optics the following approach allows for composing "lenses" into lists/arrays which is them useful for things like https://github.com/rescriptbr/reschema

Install

Install the last stable version

For ReScript

npm install --save-dev lenses-ppx@latest
or
yarn add lenses-ppx@latest -D

For BuckleScript < 6

npm install --save-dev [email protected]
or
yarn add [email protected] -D

Build

npm run build

Watch

npm run watch

In

module StateLenses = %lenses(
  type state = {
    email: string,
    age: int,
  }
)

Out

module StateLenses = {
  type state = {
    email: string,
    age: int,
  }
  type rec field<_> =
    | Email: field<string>
    | Age: field<int>
  let get:
    type value. (state, field<value>) => value =
    (state, field) =>
      switch field {
      | Email => state.email
      | Age => state.age
      }
  let set:
    type value. (state, field<value>, value) => state =
    (state, field, value) =>
      switch field {
      | Email => {...state, email: value}
      | Age => {...state, age: value}
      }
}

Using

open StateLenses

let state = {email: "[email protected]", age: 969}

Js.log(state->get(Email))
Js.log(state->get(Age))

Alternatively you can also use it like

@lenses @decco
type bartux = {
  color: string,
  top: int,
}

let bartux = {color: "red", top: 10}

Js.log(bartux->bartux_get(Color))
Js.log(bartux->bartux_set(Top, 20))
Js.log(bartux_encode(bartux))

Alternatives

lenses-ppx's People

Contributors

actions-user avatar anmonteiro avatar baransu avatar elnygren avatar fakenickels avatar romanschejbal avatar

Stargazers

 avatar

Watchers

 avatar

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.