Skip to content

Commit 233c011

Browse files
authored
Merge pull request #50 from justinwoo/examples
Add examples to README and a link to read an explanation
2 parents 8f54cdb + ad3bf24 commit 233c011

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,48 @@ Functions for working with records and polymorphic labels
1111
bower install purescript-record
1212
```
1313

14+
## Examples
15+
16+
Given some Symbol ("type level String") Proxy (`SProxy`) and a constrained or concrete record type, you can use this library to generically modify records.
17+
18+
```purs
19+
x_ = SProxy :: SProxy "x"
20+
21+
-- we can get a value out of a field
22+
gotX :: Int
23+
gotX = Record.get x_ { x: 1 }
24+
25+
-- we can insert a value into a record that does not have a field at that label yet
26+
insertedX :: { x :: Int }
27+
insertedX = Record.insert x_ 1 {}
28+
29+
-- we can delete a field from a record at a specific label
30+
deletedX :: {}
31+
deletedX = Record.delete x_ { x: 1 }
32+
33+
-- we can set a new value for a field
34+
setX1 :: { x :: Int }
35+
setX1 = Record.set x_ 1 { x: 0 }
36+
37+
-- we can also modify the type of the field by replacing the contents
38+
setX2 :: { x :: Unit }
39+
setX2 = Record.set x_ unit { x: 0 }
40+
41+
-- we can modify the field value with a function
42+
modifyX :: { x :: Int }
43+
modifyX = Record.modify x_ (\value -> value + 1) { x: 0 }
44+
45+
-- we can also merge two records
46+
mergedXY :: { x :: Int , y :: Int }
47+
mergedXY = Record.merge { x: 1 } { y: 1 }
48+
```
49+
50+
See the [tests](./test/Main.purs) for more examples.
51+
52+
If you need to combine multiple operations and avoid intermediate values, you might consider using either [Record.Builder](https://pursuit.purescript.org/packages/purescript-record/docs/Record.Builder) or [Record.ST](https://pursuit.purescript.org/packages/purescript-record/docs/Record.ST).
53+
54+
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.
55+
1456
## Documentation
1557

1658
Module documentation is [published on Pursuit](http://pursuit.purescript.org/packages/purescript-record).

test/Examples.purs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
module Examples where
2+
3+
import Prelude
4+
5+
import Record as Record
6+
import Type.Prelude (SProxy(..))
7+
8+
x_ = SProxy :: SProxy "x"
9+
y_ = SProxy :: SProxy "y"
10+
z_ = SProxy :: SProxy "z"
11+
12+
gotX :: Int
13+
gotX = Record.get x_ { x: 1 }
14+
15+
insertedX :: { x :: Int }
16+
insertedX = Record.insert x_ 1 {}
17+
18+
deletedX :: {}
19+
deletedX = Record.delete x_ { x: 1 }
20+
21+
setX1 :: { x :: Int }
22+
setX1 = Record.set x_ 1 { x: 0 }
23+
24+
setX2 :: { x :: Unit }
25+
setX2 = Record.set x_ unit { x: 0 }
26+
27+
modifyX :: { x :: Int }
28+
modifyX = Record.modify x_ (\value -> value + 1) { x: 0 }
29+
30+
mergedXY :: { x :: Int , y :: Int }
31+
mergedXY = Record.merge { x: 1 } { y: 1 }

0 commit comments

Comments
 (0)