Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add std.contract.Sequence #1452

Merged
merged 5 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions core/stdlib/std.ncl
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,27 @@
"%
= fun pred label value => if pred value then value else %blame% label,

Sequence
| doc m%"
Apply multiple contracts from left to right.
Radvendii marked this conversation as resolved.
Show resolved Hide resolved

Type: `Array Contract -> Contract`
(for technical reasons, this function isn't actually statically typed)

# Examples

```nickel
x | std.contract.Sequence [ Foo Bar ]
Radvendii marked this conversation as resolved.
Show resolved Hide resolved
```

is equivalent to

```nickel
(x | Foo) | Bar
```
"%
= fun contracts label value =>
std.array.fold_right (fun contract acc => std.contract.apply contract label acc) value contracts,
Radvendii marked this conversation as resolved.
Show resolved Hide resolved
label
| doc m%"
The label submodule provides functions that manipulate the label
Expand Down
6 changes: 6 additions & 0 deletions core/tests/integration/fail/contracts/sequence.ncl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# test.type = 'error'
#
# [test.metadata]
# error = 'EvalError::BlameError'

3 | std.contract.Sequence [ Number, std.contract.from_predicate (fun x => x < 5), std.contract.from_predicate (fun x => x > 5) ]
6 changes: 6 additions & 0 deletions core/tests/integration/pass/contracts/contracts.ncl
Original file line number Diff line number Diff line change
Expand Up @@ -166,5 +166,11 @@ let {check, Assert, ..} = import "../lib/assert.ncl" in
= fun r => %record_insert% "g" r g in
let res = f { z = 3 }
in true,

# std.contract.Sequence
let three = 3 | std.contract.Sequence [ Number, std.contract.from_predicate (fun x => x < 5) ] in
# preserves order
let tag = "some_tag" | std.contract.Sequence [ std.enum.TagOrString, [| 'some_tag |] ]
in true
]
|> check