Skip to content

Commit

Permalink
fixed std.array.split_at behavior at right boundary. (#1803)
Browse files Browse the repository at this point in the history
* fixed IndexedArrayFun behavior at right boundary.

* fixed typo.

* fix code block annotation.

* parameterized IndexedArrayFun contract.

* corrected contract annotation position.

* updated snapshots.

* Fixed doc test due to line number change in std.ncl

---------

Co-authored-by: Ben Yang <ben@ya.ng>
  • Loading branch information
suimong and Ben Yang authored Feb 8, 2024
1 parent 53eac90 commit 249f1e7
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 16 deletions.
2 changes: 1 addition & 1 deletion HACKING.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ nickel-lang-lsp 0.1.0

To only build the main crate `nickel-lang-core`, run:

```console
```shell
cargo build -p nickel-lang-core
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ error: contract broken by the caller of `at`
invalid array indexing
┌─ <stdlib/std.ncl>:162:9
162| std.contract.unstable.IndexedArrayFun
------------------------------------- expected type
162| std.contract.unstable.IndexedArrayFun 'Index
-------------------------------------------- expected type
┌─ [INPUTS_PATH]/errors/array_at_empty_array.ncl:3:16
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ error: contract broken by the caller of `at`
invalid array indexing
┌─ <stdlib/std.ncl>:162:9
162| std.contract.unstable.IndexedArrayFun
------------------------------------- expected type
162| std.contract.unstable.IndexedArrayFun 'Index
-------------------------------------------- expected type
┌─ [INPUTS_PATH]/errors/array_at_out_of_bound.ncl:3:16
Expand Down
24 changes: 15 additions & 9 deletions core/stdlib/std.ncl
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@

at
: forall a. Number -> Array a -> a
| std.contract.unstable.IndexedArrayFun
| std.contract.unstable.IndexedArrayFun 'Index
| doc m%"
Retrieves the n-th element from an array, with indices starting at 0.

Expand Down Expand Up @@ -570,15 +570,15 @@

split_at
: forall a. Number -> Array a -> { left : Array a, right : Array a }
| std.contract.unstable.IndexedArrayFun
| std.contract.unstable.IndexedArrayFun 'Split
| doc m%"
Splits an array in two at a given index and puts all the elements
to the left of the element at the given index (excluded) in the
`left` field, and the rest of the array in the `right` field.

# Preconditions

In `split_at inded value`, `index` must be a positive integer such
In `split_at index value`, `index` must be a positive integer such
that `0 <= index <= std.array.length value`.

# Examples
Expand Down Expand Up @@ -1325,6 +1325,7 @@
(fun start => RangeSecond start -> Codomain),

IndexedArrayFun
| [| 'Index, 'Split |] -> Dyn -> Dyn
| doc m%"
**Warning**: this is an unstable item. It might be renamed,
modified or deleted in any subsequent minor Nickel version.
Expand Down Expand Up @@ -1358,11 +1359,12 @@
value
in

let ArrayIndexSecond = fun min_size label value =>
let ArrayIndexSecond = fun type min_size label value =>
if %typeof% min_size == 'Number && %typeof% value == 'Array then
if min_size >= %length% value then
let max_idx = type |> match { 'Index => %length% value - 1, 'Split => %length% value } in
if min_size > max_idx then
let index_as_str = %to_str% min_size in
let max_as_str = %to_str% (%length% value - 1) in
let max_as_str = %to_str% max_idx in
let note =
if %length% value == 0 then
"Can't index into an empty array"
Expand All @@ -1378,9 +1380,13 @@
else
value
in
DependentFun
ArrayIndexFirst
(fun index => ArrayIndexSecond index -> Dyn),
let contract = fun type label function =>
DependentFun
ArrayIndexFirst
(fun index => ArrayIndexSecond type index -> Dyn)
label
function
in contract,

ArraySliceFun
| doc m%"
Expand Down
4 changes: 2 additions & 2 deletions doc/manual/merging.md
Original file line number Diff line number Diff line change
Expand Up @@ -621,9 +621,9 @@ error: missing definition for `required_field2`
8 │ & { foo.required_field1 = "here" }
│ ------------------------ in this record
┌─ <stdlib/std.ncl>:2991:18
┌─ <stdlib/std.ncl>:2997:18
2991 │ = fun x y => %deep_seq% x y,
2997 │ = fun x y => %deep_seq% x y,
│ ------------ accessed here
```

Expand Down

0 comments on commit 249f1e7

Please sign in to comment.