Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/Astrocoders/lenses-ppx
GADT lenses
https://github.com/Astrocoders/lenses-ppx
ocaml ppx reasonml
Last synced: 2 months ago
JSON representation
GADT lenses
- Host: GitHub
- URL: https://github.com/Astrocoders/lenses-ppx
- Owner: Astrocoders
- License: mit
- Created: 2018-12-25T19:53:33.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2021-10-16T17:05:25.000Z (over 3 years ago)
- Last Synced: 2024-08-03T01:29:23.292Z (6 months ago)
- Topics: ocaml, ppx, reasonml
- Language: Reason
- Homepage:
- Size: 227 KB
- Stars: 105
- Watchers: 8
- Forks: 10
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-list - lenses-ppx
README
# What are GADTs?
[GADTs: A primer](https://sketch.sh/s/yH0MJiujNSiofDWOU85loX/)# 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 versionFor 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
```rescript
module StateLenses = %lenses(
type state = {
email: string,
age: int,
}
)
```Out
```rescript
module StateLenses = {
type state = {
email: string,
age: int,
}
type rec field<_> =
| Email: field
| Age: field
let get:
type value. (state, field) => value =
(state, field) =>
switch field {
| Email => state.email
| Age => state.age
}
let set:
type value. (state, field, value) => state =
(state, field, value) =>
switch field {
| Email => {...state, email: value}
| Age => {...state, age: value}
}
}
```
Using
```rescript
open StateLenseslet state = {email: "[email protected]", age: 969}
Js.log(state->get(Email))
Js.log(state->get(Age))
```Alternatively you can also use it like
```rescript
@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
- https://github.com/scoville/re-optic/blob/master/docs/lenses-ppx.md which is more strict and follows more closely Optics standards