1
1
mirror of https://github.com/tweag/nickel.git synced 2024-09-20 08:05:15 +03:00
nickel/stdlib/record.ncl
2022-03-02 16:38:46 +01:00

53 lines
1.3 KiB
Plaintext

{
record = {
map : forall a b. (Str -> a -> b) -> {_: a} -> {_: b}
| doc m%"
Maps a function on every field of a record. The string argument of the function argument is the name of the
field.
For example:
```nickel
map (fun s x => s) { hi = 2 } =>
{ hi = "hi" }
map (fun s x => x + 1) { hello = 1, world = 2 } =>
{ hello = 2, world = 3 }
```
"%m
= fun f r => %record_map% r f,
fields | { ; Dyn} -> Array Str
| doc m%"
Given a record, results in a array of the string representation of all fields in the record.
```nickel
fields { one = 1, two = 2 } =>
[ "one", "two" ]
```
"%m
= fun r => %fields% r,
values | { ; Dyn} -> Array
| doc m%"
Given a record, results in a array containing all the values in that record.
```nickel
values { one = 1, world = "world" }
[ 1, "world" ]
```
"%m
= fun r => %values% r,
has_field : Str -> Dyn -> Bool
| doc m%"
Given the name of a field and a record, checks if the record contains the given field.
```nickel
has_field "hello" { one = 1, two = 2 } =>
false
has_field "one" { one = 1, two = 2 } =>
true
"%m
= fun field r => %has_field% field r,
}
}