From 509849d8c2f1cda49334281bcf6019359a42ccdd Mon Sep 17 00:00:00 2001 From: Seggan Date: Wed, 1 Nov 2023 16:50:14 -0400 Subject: [PATCH] `remove` --- docs/lang/core/list.papyri | 6 ++++++ docs/lang/core/string.papyri | 4 ++++ docs/lang/core/table.papyri | 4 ++++ .../metis/runtime/intrinsics/ValueInit.kt | 17 +++++++++++++++++ 4 files changed, 31 insertions(+) diff --git a/docs/lang/core/list.papyri b/docs/lang/core/list.papyri index 6d75c2d..5cf0777 100644 --- a/docs/lang/core/list.papyri +++ b/docs/lang/core/list.papyri @@ -25,6 +25,12 @@ Creates a new list with the same elements as the given list. Returns a string containing the elements of the list separated by the given separator. +@h3 { remove(self, value: any): null } +Removes the first occurrence of the given value from the list. + +@h3 { remove_at(self, index: number): any } +Removes the element at the given index from the list and returns it. + @h3 { reverse(self): list } Returns a new list with the elements of the given list in reverse order. diff --git a/docs/lang/core/string.papyri b/docs/lang/core/string.papyri index fe76f0b..8d04650 100644 --- a/docs/lang/core/string.papyri +++ b/docs/lang/core/string.papyri @@ -25,6 +25,10 @@ Encode the string using the specified encoding. The default encoding is UTF-8. @h3 { equal_ignore_case(self, other: string): boolean } Compare two strings for equality, ignoring case. +@h3 { remove(self, start: number, end: number \| null): string } +Remove a range of characters from the string from `start` to `end` (exclusive). If `end` is `null`, the substring will +end at the end of the string. + @h3 { repeat(self, count: number): string } Repeat the string `count` times. diff --git a/docs/lang/core/table.papyri b/docs/lang/core/table.papyri index 5cbf7aa..ec4c008 100644 --- a/docs/lang/core/table.papyri +++ b/docs/lang/core/table.papyri @@ -19,6 +19,10 @@ from `other` is used. @h3 { keys(self): list } Returns a list of all keys in the table. +@h3 { remove(self, key: any): null } +Removes the key `key` from the table `self` and returns the value mapped to it. If the key does not exist, raises +`KeyError`. + @h3 { values(self): list } Returns a list of all values in the table. diff --git a/lang/src/main/kotlin/io/github/seggan/metis/runtime/intrinsics/ValueInit.kt b/lang/src/main/kotlin/io/github/seggan/metis/runtime/intrinsics/ValueInit.kt index b12764c..aaf4e87 100644 --- a/lang/src/main/kotlin/io/github/seggan/metis/runtime/intrinsics/ValueInit.kt +++ b/lang/src/main/kotlin/io/github/seggan/metis/runtime/intrinsics/ValueInit.kt @@ -46,6 +46,13 @@ internal fun initString() = buildTable { table -> else Charset.forName(encoding.stringValue()) Value.Bytes(self.stringValue().toByteArray(actualEncoding)) } + table["remove"] = threeArgFunction { self, start, end -> + if (end == Value.Null) { + self.stringValue().removeRange(start.intValue(), self.stringValue().length).metisValue() + } else { + self.stringValue().removeRange(start.intValue(), end.intValue()).metisValue() + } + } table["replace"] = threeArgFunction(true) { self, value, toReplace -> self.stringValue().replace(value.stringValue(), toReplace.stringValue()).metisValue() } @@ -177,6 +184,9 @@ internal fun initTable() = Value.Table(mutableMapOf(), null).also { table -> table["values"] = oneArgFunction(true) { self -> self.convertTo().values.metisValue() } + table["remove"] = twoArgFunction(true) { self, key -> + self.convertTo().remove(key).orNull() + } table.metatable = table } @@ -213,6 +223,13 @@ internal fun initList() = buildTable { table -> self.convertTo().add(value) Value.Null } + table["remove"] = twoArgFunction(true) { self, value -> + self.convertTo().remove(value) + Value.Null + } + table["remove_at"] = twoArgFunction(true) { self, index -> + self.convertTo().removeAt(index.intValue()) + } table["slice"] = threeArgFunction(true) { self, start, end -> if (end == Value.Null) { self.convertTo().subList(start.intValue(), self.convertTo().size).metisValue()