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

fixed std.array.split_at behavior at right boundary. #1803

Merged
merged 7 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
yannham marked this conversation as resolved.
Show resolved Hide resolved
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
Loading