https://github.com/purescript/purescript-record
Functions for working with records and polymorphic labels
https://github.com/purescript/purescript-record
Last synced: 8 days ago
JSON representation
Functions for working with records and polymorphic labels
- Host: GitHub
- URL: https://github.com/purescript/purescript-record
- Owner: purescript
- License: bsd-3-clause
- Created: 2017-07-16T00:53:17.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2024-01-06T04:13:13.000Z (about 2 years ago)
- Last Synced: 2026-01-29T04:47:32.267Z (23 days ago)
- Language: PureScript
- Homepage:
- Size: 64.5 KB
- Stars: 68
- Watchers: 8
- Forks: 32
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# purescript-record
[](https://github.com/purescript/purescript-record/releases)
[](https://github.com/purescript/purescript-record/actions?query=workflow%3ACI+branch%3Amaster)
[](https://pursuit.purescript.org/packages/purescript-record)
Functions for working with records and polymorphic labels
## Installation
```
spago install record
```
## Examples
Given some Symbol ("type level String") Proxy and a constrained or concrete record type, you can use this library to generically modify records.
```purs
x_ = Proxy :: Proxy "x"
-- we can get a value out of a field
gotX :: Int
gotX = Record.get x_ { x: 1 }
-- we can insert a value into a record that does not have a field at that label yet
insertedX :: { x :: Int }
insertedX = Record.insert x_ 1 {}
-- we can delete a field from a record at a specific label
deletedX :: {}
deletedX = Record.delete x_ { x: 1 }
-- we can set a new value for a field
setX1 :: { x :: Int }
setX1 = Record.set x_ 1 { x: 0 }
-- we can also modify the type of the field by replacing the contents
setX2 :: { x :: Unit }
setX2 = Record.set x_ unit { x: 0 }
-- we can modify the field value with a function
modifyX :: { x :: Int }
modifyX = Record.modify x_ (\value -> value + 1) { x: 0 }
-- we can also merge two records
mergedXY :: { x :: Int , y :: Int }
mergedXY = Record.merge { x: 1 } { y: 1 }
```
See the [tests](./test/Main.purs) for more examples.
If you need to combine multiple operations and avoid intermediate values, you might consider using [Record.Builder](https://pursuit.purescript.org/packages/purescript-record/docs/Record.Builder).
You can also find an explanation and example of how to use this library [in this tutorial](https://purescript-simple-json.readthedocs.io/en/latest/inferred-record-types.html) of the Simple-JSON docs.
## Documentation
Module documentation is [published on Pursuit](http://pursuit.purescript.org/packages/purescript-record).