Category talk:Wren-trait: Difference between revisions
→Source code: Improved the robustness of the iteration classes.
m (→Source code: Added quotes to 'lang' attribute.) |
(→Source code: Improved the robustness of the iteration classes.) |
||
Line 13:
*/
class CloneableSeq is Sequence {
clone() { this } /* to be overridden by child class */
}
Line 29:
< (other) { compare(other) < 0 }
> (other) { compare(other) > 0 }
<=(other) { compare(other) <= 0 }
>=(other) { compare(other) >= 0 }
Line 41:
construct new(seq, step) {
if (!(seq is Sequence)) Fiber.abort("First argument must be a sequence.")
if (!((step is Num) && step.isInteger && step > 0)) {
}▼
_seq = seq
_step =
}
Line 80 ⟶ 83:
}
/*
Reversed wraps a Sequence
and by steps other than 1. To ensure this always works, non-lists
are converted internally to lists.
*/
class Reversed is Sequence {
// Constructs a new reversed sequence.
construct new(seq, step) {
if (!(seq is Sequence))
if (!((step is Num) && step.isInteger && step > 0)) {
▲ Fiber.abort("First argument must be a sequence other than a range.")
}
_seq = (seq is List) ? seq : seq.toList
_step =
}
Line 104 ⟶ 109:
} else if (it == false) {
it = _seq.count - 1 - _step
} else {
it = it - 1 - _step
}
return (it >= 0) ? it : false
}
Line 130 ⟶ 135:
/*
Indexed wraps a Sequence
together with their zero-based indices. To ensure this always works, non-lists
are converted internally to lists.
*/
class Indexed is Sequence {
// Constructs a new indexed sequence with a step of 'step' and optionally reversed.
construct new(seq, step, reversed) {
▲ if (seq is Range) Fiber.abort("First argument cannot be a range.")
if (!(reversed is Bool)) Fiber.abort("Third argument must be true or false.")
_seq =
_seq = !reversed ? Stepped.new(_seq, step) : Reversed.new(_seq, step)
}
Line 144 ⟶ 150:
static new(seq, step) { new(seq, step, false) }
// Constructs a new indexed sequence with a step of 1 and reversed set to false.
▲ }
// Iterator protocol methods.
Line 184 ⟶ 185:
static [key] { __map ? __map[key] : null }
// Retrieves a ByKey object's key from the object it wraps.
// If the same object has been wrapped more than once, returns the first key found which may
// not be the lowest. Returns 0 if the object is unwrapped.
|