From 4677b57b844c2701e45f46a34aa3a30d018b4e55 Mon Sep 17 00:00:00 2001 From: Ben Yang Date: Tue, 6 Feb 2024 19:24:19 +0800 Subject: [PATCH] parameterized IndexedArrayFun contract. --- core/stdlib/std.ncl | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/core/stdlib/std.ncl b/core/stdlib/std.ncl index 6275be4971..008950a8f7 100644 --- a/core/stdlib/std.ncl +++ b/core/stdlib/std.ncl @@ -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. @@ -570,7 +570,7 @@ 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 @@ -1358,11 +1358,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) in + let max_as_str = %to_str% max_idx in let note = if %length% value == 0 then "Can't index into an empty array" @@ -1378,9 +1379,15 @@ else value in - DependentFun - ArrayIndexFirst - (fun index => ArrayIndexSecond index -> Dyn), + let contract + | [| 'Index, 'Split |] -> Dyn -> Dyn + = fun type label function => + DependentFun + ArrayIndexFirst + (fun index => ArrayIndexSecond type index -> Dyn) + label + function + in contract, ArraySliceFun | doc m%"