1
0
forked from Mirror/wren
Files
wren/builtin/core.wren
Bob Nystrom abe80e6d4b Initial map implementation.
Still lots of methods missing and clean up and tests to do.
Also still no literal syntax.

But the core hash table code is there and working. The supported
key types are all, uh, supported.
2015-01-24 22:27:35 -08:00

97 lines
1.5 KiB
Plaintext

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
}
all(f) {
for (element in this) {
if (!f.call(element)) return false
}
return true
}
reduce(acc, f) {
for (element in this) {
acc = f.call(acc, element)
}
return acc
}
reduce(f) {
var iter = iterate(null)
if (!iter) Fiber.abort("Can't reduce an empty sequence.")
// Seed with the first element.
var result = iteratorValue(iter)
while (iter = iterate(iter)) {
result = f.call(result, iteratorValue(iter))
}
return result
}
join { join("") }
join(sep) {
var first = true
var result = ""
for (element in this) {
if (!first) result = result + sep
first = false
result = result + element.toString
}
return result
}
}
class String is Sequence {}
class List is Sequence {
addAll(other) {
for (element in other) {
add(element)
}
return other
}
toString { "[" + join(", ") + "]" }
+(other) {
var result = this[0..-1]
for (element in other) {
result.add(element)
}
return result
}
contains(element) {
for (item in this) {
if (element == item) {
return true
}
}
return false
}
}
class Map {
// TODO: Implement this.
toString { "{}" }
}
class Range is Sequence {}