Skip to content

Commit

Permalink
Remove len and __len__
Browse files Browse the repository at this point in the history
  • Loading branch information
Seggan committed Dec 14, 2023
1 parent 6030f9e commit e1fd564
Show file tree
Hide file tree
Showing 15 changed files with 59 additions and 61 deletions.
10 changes: 5 additions & 5 deletions docs/lang/core/bytes.papyri
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ Concatenate a list of bytes objects into a single bytes object.

@h2 { Member functions }

@h3 { decode(encoding: string = "UTF-8"): string }
@h3 { decode(self, encoding: string = "UTF-8"): string }
Decode the bytes object into a string using the given encoding. If no encoding is given, UTF-8 is
used.

@h3 { slice(start: number, length: number): bytes }
@h3 { size(self): number }
Get the length of the bytes object.

@h3 { slice(self, start: number, length: number): bytes }
Create a new bytes object from a slice of the current bytes object. The slice starts at the given
index and has the given length.

Expand All @@ -48,8 +51,5 @@ Get the byte at the given index.
@h3 { __iter__(self): any }
Get an iterator for the bytes object.

@h3 { __len__(self): number }
Get the length of the bytes object.

@h3 { __set__(self, index: number, value: number) }
Set the byte at the given index to the given value.
3 changes: 0 additions & 3 deletions docs/lang/core/index.papyri
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ If the prompt is `null`, then no prompt is printed. Returns the line of text ent
Returns an iterator for the given value. Requires the `__iter__` metamethod to be defined on the value.
The iterator must have the `hasNext(self): boolean` and the `next(self): any` methods defined.

@h3 { len(value: any): number }
Returns the length of the value. Requires the `__len__` metamethod to be defined on the value.

@h3 { loadChunk(chunk: string): callable }
Loads the given Metis code as a callable function. Returns the function.

Expand Down
8 changes: 4 additions & 4 deletions docs/lang/core/list.papyri
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,12 @@ Removes the first occurrence of the given value from the list.
@h3 { removeAt(self, index: number): any }
Removes the element at the given index from the list and returns it.

@h3 { reverse(self): list }
@h3 { reversed(self): list }
Returns a new list with the elements of the given list in reverse order.

@h3 { size(self): number }
Returns the number of elements in the list.

@h3 { slice(self, start: number, end: number): list }
Returns a new list containing the elements from `start` to `end` (exclusive).

Expand All @@ -73,9 +76,6 @@ Returns the element at the given index.
@h3 { __iter__(self): any }
Returns an iterator over the elements of the list.

@h3 { __len__(self): number }
Returns the number of elements in the list.

@h3 { __plus__(self, other: list \| any): list }
Returns a new list containing the elements of the given list followed by the
elements of the other list or the given value.
Expand Down
8 changes: 5 additions & 3 deletions docs/lang/core/range.papyri
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ The end of the range (exclusive).
@h3 { step: number }
The step of the range.

@h2 { Member functions }

@h3 { size(self): number }
Returns the length of the range.

@h2 Metamethods

@h3 { __index__(self, key: number): number }
Returns the value at the given index in the range.

@h3 { __iter__(self): table }
Returns an iterator for the range.

@h3 { __len__(self): number }
Returns the length of the range.
16 changes: 8 additions & 8 deletions docs/lang/core/string.papyri
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,11 @@ Repeat the string `count` times.
@h3 { replace(self, pattern: string, replacement: string): string }
Replace all occurrences of `pattern` with `replacement`.

@h3 { reverse(self): string }
Reverse the string.
@h3 { reversed(self): string }
Return a reversed copy of the string.

@h3 { size(self): number }
Get the number of characters in the string.

@h3 { split(self, separator: string): list\[string\] }
Split the string into an array of strings, using `separator` as the delimiter.
Expand Down Expand Up @@ -97,9 +100,6 @@ Get the character at the specified index.
@h3 { __iter__(self): table }
Get an iterator for the string.

@h3 { __len__(self): number }
Get the length of the string.

@h3 { __plus__(self, other: string): string }
Concatenate two strings.

Expand All @@ -125,14 +125,14 @@ Delete a character from the builder at the specified index.
@h3 { deleteLast(self, count: number): self }
Delete the last `count` characters from the builder.

@h3 { size(self): number }
Get the number of characters in the builder.

@h2 Metamethods

@h3 { __index__(self, index: number): string }
Get the character at the specified index.

@h3 { __len__(self): number }
Get the length of the builder.

@h3 { __set__(self, index: number, value: string): self }
Set the character at the specified index.

Expand Down
6 changes: 3 additions & 3 deletions docs/lang/core/table.papyri
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ Returns a list of all keys in the table.
Removes the key `key` from the table `self` and returns the value mapped to it. If the key does not exist, raises
`KeyError`.

@h3 { size(self): number }
Returns the number of keys in the table.

@h3 { values(self): list }
Returns a list of all values in the table.

Expand All @@ -35,9 +38,6 @@ Returns `true` if the key `key` exists in the table `self`, `false` otherwise.
Returns the value of the key `key` in the table `self`. If the key does not exist,
raises `KeyError`.

@h3 { __len__(self): number }
Returns the number of keys in the table `self`.

@h3 { __plus__(self, other: table): table }
Returns a new table containing the contents of `self` and `other`. If a key exists
in both tables, the value from `other` is used.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ private val sbMetatable = buildTable { table ->
self.asObj<StringBuilder>().clear()
self
}
table["__len__"] = oneArgFunction(true) { self ->
table["size"] = oneArgFunction(true) { self ->
Value.Number.of(self.asObj<StringBuilder>().length)
}
table["__str__"] = oneArgFunction(true) { self ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal fun initString() = buildTable { table ->
table["__plus__"] = twoArgFunction(true) { self, other ->
Value.String(self.stringValue() + other.stringValue())
}
table["__len__"] = oneArgFunction(true) { self ->
table["size"] = oneArgFunction(true) { self ->
self.stringValue().length.toDouble().metisValue()
}
table["__index__"] = twoArgFunction(true) { self, key ->
Expand Down Expand Up @@ -218,7 +218,7 @@ internal fun initTable() = Value.Table(mutableMapOf(), null).also { table ->
self.setOrError(key, value)
Value.Null
}
table["__len__"] = oneArgFunction(true) { self ->
table["size"] = oneArgFunction(true) { self ->
self.tableValue().size.metisValue()
}
table["__contains__"] = twoArgFunction(true) { self, key ->
Expand Down Expand Up @@ -255,7 +255,7 @@ internal fun initList() = buildTable { table ->
self.setOrError(key, value)
Value.Null
}
table["__len__"] = oneArgFunction(true) { self ->
table["size"] = oneArgFunction(true) { self ->
self.listValue().size.metisValue()
}
table["__contains__"] = twoArgFunction(true) { self, key ->
Expand Down Expand Up @@ -331,7 +331,7 @@ internal fun initBytes() = buildTable { table ->
self.setOrError(key, value)
Value.Null
}
table["__len__"] = oneArgFunction(true) { self ->
table["size"] = oneArgFunction(true) { self ->
self.bytesValue().size.metisValue()
}
table["__contains__"] = twoArgFunction(true) { self, key ->
Expand Down
1 change: 0 additions & 1 deletion metis-lang/src/main/resources/core.metis
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
global fn str(x) = x.__str__()
global fn len(x) = x.__len__()
global fn iter(x) = x.__iter__()
global fn cmp(x, y) = x.__cmp__(y)

Expand Down
14 changes: 7 additions & 7 deletions metis-lang/src/main/resources/core/collection.metis
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ fn iterator.next(self)
return res
end

fn iterator.hasNext(self) = self.index < len(self.coll)
fn iterator.hasNext(self) = self.index < self.coll.size()

fn collection.__iter__(self) = withMetatable({ "coll" = self, "index" = 0 }, iterator)

fn collection.__eq__(self, other)
if type(self) != type(other) or len(self) != len(other)
if type(self) != type(other) or self.size() != other.size()
return false
end
for i in 0..<len(self)
for i in 0..<self.size()
if self[i] != other[i]
return false
end
Expand All @@ -25,7 +25,7 @@ fn collection.__eq__(self, other)
end

fn collection.map(self, f)
let res = list.new(len(self))
let res = list.new(self.size())
for v in self
res.append(f(v))
end
Expand Down Expand Up @@ -90,10 +90,10 @@ end

fn collection.last(self, f)
if f is null
return self[len(self) - 1]
return self[self.size() - 1]
end

let i = len(self) - 1
let i = self.size() - 1
while i >= 0
if f(self[i])
return self[i]
Expand All @@ -103,4 +103,4 @@ fn collection.last(self, f)
return null
end

fn collection.isEmpty(self) = len(self) == 0
fn collection.isEmpty(self) = self.size() == 0
20 changes: 10 additions & 10 deletions metis-lang/src/main/resources/core/list.metis
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn list.__str__(self)
end
sb.append(", ")
end
if len(self) > 1
if self.size() > 1
sb.deleteLast(2)
end
return str(sb.append("]"))
Expand Down Expand Up @@ -38,23 +38,23 @@ fn list.join(self, sep)
for v in self
sb.append(v).append(sep)
end
sb.deleteLast(len(sep))
sb.deleteLast(sep.size())
return str(sb)
end

fn list.pop(self) = self.removeAt(len(self) - 1)
fn list.pop(self) = self.removeAt(self.size() - 1)

fn list.reverse(self)
let result = list.new(len(self))
for i in range.inclusive(len(self) - 1, 0, -1)
fn list.reversed(self)
let result = list.new(self.size())
for i in range.inclusive(self.size() - 1, 0, -1)
result.append(self[i])
end
return result
end

fn list.sort(self, compare)
compare ?:= cmp
let pivot = self[len(self) // 2]
let pivot = self[self.size() // 2]
let less = list.new()
let equal = list.new()
let greater = list.new()
Expand All @@ -68,10 +68,10 @@ fn list.sort(self, compare)
equal.append(v)
end
end
if len(less) > 1
if less.size() > 1
less.sort(compare)
end
if len(greater) > 1
if greater.size() > 1
greater.sort(compare)
end
self.clear()
Expand All @@ -87,7 +87,7 @@ fn list.sorted(self, compare)
end

fn list.clone(self)
let result = list.new(len(self))
let result = list.new(self.size())
for v in self
result.append(v)
end
Expand Down
4 changes: 2 additions & 2 deletions metis-lang/src/main/resources/core/range.metis
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ fn range.__index__(self, i)
if type(i) == "number"
return self.start + i * self.step
else
return table.__index__(self, i)
return table["__index__"](self, i)
end
end

fn range.__str__(self)
return "range(" + str(self.start) + ", " + str(self.stop) + ", " + str(self.step) + ")"
end

fn range.__len__(self) = (self.stop - self.start) / self.step
fn range.size(self) = (self.stop - self.start) / self.step

fn range.exclusive(start, stop, step) = withMetatable({
"start" = start,
Expand Down
14 changes: 7 additions & 7 deletions metis-lang/src/main/resources/core/string.metis
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ do
let sbMeta = string.builder().metatable
fn sbMeta.append(self, value) = self.__append(str(value))
fn sbMeta.deleteLast(self, n)
let length = len(self)
let length = self.size()
self.delete(length - n, length)
return self
end
Expand All @@ -13,16 +13,16 @@ end
fn string.charCode(self) = self.encode("UTF-8")[0]

fn string.repeat(self, n)
let sb = string.builder(len(self) * n)
let sb = string.builder(self.size() * n)
for _ in 0..<n
sb.append(self)
end
return str(sb)
end

fn string.reverse(self)
let sb = string.builder(len(self))
for i in range.inclusive(len(self) - 1, 0, -1)
fn string.reversed(self)
let sb = string.builder(self.size())
for i in range.inclusive(self.size() - 1, 0, -1)
sb.append(self[i])
end
return str(sb)
Expand All @@ -32,14 +32,14 @@ fn string.strip(self) = self.stripLeft().stripRight()

fn string.stripLeft(self)
let i = 0
while i < len(self) and self[i].isWhitespace()
while i < self.size() and self[i].isWhitespace()
i += 1
end
return self.sub(i)
end

fn string.stripRight(self)
let i = len(self) - 1
let i = self.size() - 1
while i >= 0 and self[i].isWhitespace()
i -= 1
end
Expand Down
4 changes: 2 additions & 2 deletions metis-lang/src/main/resources/core/table.metis
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ fn table.__str__(self)
.append(v)
.append(", ")
end
sb.deleteAt(len(sb) - 2)
sb.deleteAt(sb.size() - 2)
return str(sb.append("}"))
end

fn table.__eq__(self, other)
if len(self) != len(other)
if self.size() != other.size()
return false
end
for k in self.keys()
Expand Down
2 changes: 1 addition & 1 deletion metis-lang/src/main/resources/std/json.metis
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ end
global fn parse(text) = withMetatable({
"text" = text,
"pos" = 0,
"len" = len(text)
"len" = text.size()
}, parser).parseValue()

global fn stringify(value)
Expand Down

0 comments on commit e1fd564

Please sign in to comment.