Skip to content

Commit

Permalink
remove
Browse files Browse the repository at this point in the history
  • Loading branch information
Seggan committed Nov 1, 2023
1 parent e97afdd commit 509849d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/lang/core/list.papyri
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
4 changes: 4 additions & 0 deletions docs/lang/core/string.papyri
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
4 changes: 4 additions & 0 deletions docs/lang/core/table.papyri
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -177,6 +184,9 @@ internal fun initTable() = Value.Table(mutableMapOf(), null).also { table ->
table["values"] = oneArgFunction(true) { self ->
self.convertTo<Value.Table>().values.metisValue()
}
table["remove"] = twoArgFunction(true) { self, key ->
self.convertTo<Value.Table>().remove(key).orNull()
}

table.metatable = table
}
Expand Down Expand Up @@ -213,6 +223,13 @@ internal fun initList() = buildTable { table ->
self.convertTo<Value.List>().add(value)
Value.Null
}
table["remove"] = twoArgFunction(true) { self, value ->
self.convertTo<Value.List>().remove(value)
Value.Null
}
table["remove_at"] = twoArgFunction(true) { self, index ->
self.convertTo<Value.List>().removeAt(index.intValue())
}
table["slice"] = threeArgFunction(true) { self, start, end ->
if (end == Value.Null) {
self.convertTo<Value.List>().subList(start.intValue(), self.convertTo<Value.List>().size).metisValue()
Expand Down

0 comments on commit 509849d

Please sign in to comment.