1
1
mirror of https://github.com/tweag/nickel.git synced 2024-09-20 16:08:14 +03:00
nickel/stdlib/record.ncl

53 lines
1.3 KiB
Plaintext
Raw Normal View History

{
record = {
2021-08-26 14:59:03 +03:00
map : forall a b. (Str -> a -> b) -> {_: a} -> {_: b}
| doc m%"
2021-08-26 14:59:03 +03:00
Maps a function on every field of a record. The string argument of the function argument is the name of the
field.
2021-08-26 14:59:03 +03:00
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,
2021-03-11 13:39:23 +03:00
2022-02-23 13:32:10 +03:00
fields | { ; Dyn} -> Array Str
| doc m%"
2022-02-23 13:32:10 +03:00
Given a record, results in a array of the string representation of all fields in the record.
2021-08-26 14:59:03 +03:00
```nickel
fields { one = 1, two = 2 } =>
2021-08-26 14:59:03 +03:00
[ "one", "two" ]
```
"%m
= fun r => %fields% r,
2021-08-26 14:59:03 +03:00
2022-02-23 13:32:10 +03:00
values | { ; Dyn} -> Array
| doc m%"
2022-02-23 13:32:10 +03:00
Given a record, results in a array containing all the values in that record.
2021-08-26 14:59:03 +03:00
```nickel
values { one = 1, world = "world" }
2021-08-26 14:59:03 +03:00
[ 1, "world" ]
```
"%m
= fun r => %values% r,
2021-08-26 14:59:03 +03:00
has_field : Str -> Dyn -> Bool
| doc m%"
2021-08-26 14:59:03 +03:00
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 } =>
2021-08-26 14:59:03 +03:00
false
has_field "one" { one = 1, two = 2 } =>
2021-08-26 14:59:03 +03:00
true
"%m
= fun field r => %has_field% field r,
}
}