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:
Bob Nystrom
2014-02-16 17:20:31 +00:00
parent b044447464
commit 31ee167168
8 changed files with 68 additions and 53 deletions
+21 -17
View File
@@ -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 {}