feat: extract map and where into Sequence base class for List and Range
Extract the common `map` and `where` methods from `List` into a new `Sequence` base class, making `List is Sequence` and `Range is Sequence`. Remove duplicate native method registrations for Range in wren_core.c and add new test files for Range map/where operations.
This commit is contained in:
+21
-17
@@ -1,4 +1,22 @@
|
||||
class List {
|
||||
class Sequence {
|
||||
map (f) {
|
||||
var result = []
|
||||
for (element in this) {
|
||||
result.add(f.call(element))
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
where (f) {
|
||||
var result = []
|
||||
for (element in this) {
|
||||
if (f.call(element)) result.add(element)
|
||||
}
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
class List is Sequence {
|
||||
toString {
|
||||
var result = "["
|
||||
for (i in 0...count) {
|
||||
@@ -16,20 +34,6 @@ class List {
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
map (f) {
|
||||
var result = []
|
||||
for (element in this) {
|
||||
result.add(f.call(element))
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
where (f) {
|
||||
var result = []
|
||||
for (element in this) {
|
||||
if (f.call(element)) result.add(element)
|
||||
}
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
class Range is Sequence {}
|
||||
|
||||
Reference in New Issue
Block a user