From 4a18c33b18c0a5df9249529e5e3af0b25a8e89fe Mon Sep 17 00:00:00 2001 From: salano_ym <53254905+salano-ym@users.noreply.github.com> Date: Thu, 9 May 2024 15:17:25 +0000 Subject: [PATCH 1/3] =?UTF-8?q?arr.insert=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + docs/primitive-props.md | 6 ++++++ src/interpreter/primitive-props.ts | 9 +++++++++ test/index.ts | 18 ++++++++++++++++++ 4 files changed, 34 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ef6934a..eb7dbe2a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ # 未リリース分 - `Date:year`系の関数に0を渡すと現在時刻になる問題を修正 - シンタックスエラーなどの位置情報を修正 +- `arr.insert`を追加 # 0.18.0 - `Core:abort`でプログラムを緊急停止できるように diff --git a/docs/primitive-props.md b/docs/primitive-props.md index 609cc6ee..7f0df3f6 100644 --- a/docs/primitive-props.md +++ b/docs/primitive-props.md @@ -184,6 +184,12 @@ _fromIndex_ および _toIndex_ に関する挙動は`arr.slice`に準拠しま `arr.copy`同様シャローコピーであり、配列やオブジェクトの参照は維持されます。 _times_ には0以上の整数値を指定します。それ以外ではエラーになります。 +### @(_v_: arr).insert(_index_: num, _item_: value): null +**【この操作は配列を書き換えます】** +配列の _index_ の位置に _item_ を挿入します。\ +_index_ が負の場合は末尾から数えます。\ +_index_ が最後の要素より後の場合は末尾に追加します。 + ### @(_v_: arr).every(_func_: @(_item_: value, _index_: num) { bool }): bool 配列の全ての要素に対して _func_ が true を返す時のみ true 返します。空配列には常に true を返します。 diff --git a/src/interpreter/primitive-props.ts b/src/interpreter/primitive-props.ts index c154c45a..db40623e 100644 --- a/src/interpreter/primitive-props.ts +++ b/src/interpreter/primitive-props.ts @@ -301,6 +301,15 @@ const PRIMITIVE_PROPS: { } return FALSE; }), + + insert: (target: VArr): VFn => FN_NATIVE(async ([index, item], opts) => { + assertNumber(index); + expectAny(item); + + target.value.splice(index.value, 0, item); + + return NULL; + }), }, error: { diff --git a/test/index.ts b/test/index.ts index fed26c01..26571233 100644 --- a/test/index.ts +++ b/test/index.ts @@ -2920,6 +2920,24 @@ describe('primitive props', () => { FALSE, ])); }); + + test.concurrent('insert', async () => { + const res = await exe(` + let arr1 = [0, 1, 2] + let res = [] + res.push(arr1.insert(3, 10)) // [0, 1, 2, 10] + res.push(arr1.insert(2, 20)) // [0, 1, 20, 2, 10] + res.push(arr1.insert(0, 30)) // [30, 0, 1, 20, 2, 10] + res.push(arr1.insert(-1, 40)) // [30, 0, 1, 20, 2, 40, 10] + res.push(arr1.insert(-4, 50)) // [30, 0, 1, 50, 20, 2, 40, 10] + res.push(arr1.insert(100, 60)) // [30, 0, 1, 50, 20, 2, 40, 10, 60] + <: [null,null,null,null,null,null,arr1] + `); + eq(res, ARR([ + NULL, NULL, NULL, NULL, NULL, NULL, + ARR([NUM(30), NUM(0), NUM(1), NUM(50), NUM(20), NUM(2), NUM(40), NUM(10), NUM(60)]) + ])); + }); }); }); From 86fe815239c2b90cfb7067fcc3d5b405d8822335 Mon Sep 17 00:00:00 2001 From: salano_ym <53254905+salano-ym@users.noreply.github.com> Date: Thu, 9 May 2024 15:24:58 +0000 Subject: [PATCH 2/3] fix test - arr.insert --- test/index.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/index.ts b/test/index.ts index 26571233..6f34f61a 100644 --- a/test/index.ts +++ b/test/index.ts @@ -2931,7 +2931,8 @@ describe('primitive props', () => { res.push(arr1.insert(-1, 40)) // [30, 0, 1, 20, 2, 40, 10] res.push(arr1.insert(-4, 50)) // [30, 0, 1, 50, 20, 2, 40, 10] res.push(arr1.insert(100, 60)) // [30, 0, 1, 50, 20, 2, 40, 10, 60] - <: [null,null,null,null,null,null,arr1] + res.push(arr1) + <: res `); eq(res, ARR([ NULL, NULL, NULL, NULL, NULL, NULL, From f6993b211524ae2c57c5de080481d4e789b43afd Mon Sep 17 00:00:00 2001 From: salano_ym <53254905+salano-ym@users.noreply.github.com> Date: Thu, 9 May 2024 15:29:50 +0000 Subject: [PATCH 3/3] =?UTF-8?q?arr.remove=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 2 +- docs/primitive-props.md | 6 ++++++ src/interpreter/primitive-props.ts | 8 ++++++++ test/index.ts | 19 +++++++++++++++++++ 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eb7dbe2a..c137df0d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ # 未リリース分 - `Date:year`系の関数に0を渡すと現在時刻になる問題を修正 - シンタックスエラーなどの位置情報を修正 -- `arr.insert`を追加 +- `arr.insert`,`arr.remove`を追加 # 0.18.0 - `Core:abort`でプログラムを緊急停止できるように diff --git a/docs/primitive-props.md b/docs/primitive-props.md index 7f0df3f6..2b76d495 100644 --- a/docs/primitive-props.md +++ b/docs/primitive-props.md @@ -190,6 +190,12 @@ _times_ には0以上の整数値を指定します。それ以外ではエラ _index_ が負の場合は末尾から数えます。\ _index_ が最後の要素より後の場合は末尾に追加します。 +### @(_v_: arr).remove(_index_: num): value | null +**【この操作は配列を書き換えます】** +配列から _index_ の位置の要素を取り除き、その要素を返します。\ +_index_ が負の場合は末尾から数えます。\ +_index_ が最後の要素より後の場合は取り除かず、`null`を返します。 + ### @(_v_: arr).every(_func_: @(_item_: value, _index_: num) { bool }): bool 配列の全ての要素に対して _func_ が true を返す時のみ true 返します。空配列には常に true を返します。 diff --git a/src/interpreter/primitive-props.ts b/src/interpreter/primitive-props.ts index db40623e..efda34b9 100644 --- a/src/interpreter/primitive-props.ts +++ b/src/interpreter/primitive-props.ts @@ -310,6 +310,14 @@ const PRIMITIVE_PROPS: { return NULL; }), + + remove: (target: VArr): VFn => FN_NATIVE(async ([index], opts) => { + assertNumber(index); + + const removed = target.value.splice(index.value, 1); + + return removed[0] ?? NULL; + }), }, error: { diff --git a/test/index.ts b/test/index.ts index 6f34f61a..04796634 100644 --- a/test/index.ts +++ b/test/index.ts @@ -2939,6 +2939,25 @@ describe('primitive props', () => { ARR([NUM(30), NUM(0), NUM(1), NUM(50), NUM(20), NUM(2), NUM(40), NUM(10), NUM(60)]) ])); }); + + test.concurrent('remove', async () => { + const res = await exe(` + let arr1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] + let res = [] + res.push(arr1.remove(9)) // 9 [0, 1, 2, 3, 4, 5, 6, 7, 8] + res.push(arr1.remove(3)) // 3 [0, 1, 2, 4, 5, 6, 7, 8] + res.push(arr1.remove(0)) // 0 [1, 2, 4, 5, 6, 7, 8] + res.push(arr1.remove(-1)) // 8 [1, 2, 4, 5, 6, 7] + res.push(arr1.remove(-5)) // 2 [1, 4, 5, 6, 7] + res.push(arr1.remove(100)) // null [1, 4, 5, 6, 7] + res.push(arr1) + <: res + `); + eq(res, ARR([ + NUM(9), NUM(3), NUM(0), NUM(8), NUM(2), NULL, + ARR([NUM(1), NUM(4), NUM(5), NUM(6), NUM(7)]) + ])); + }); }); });