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)) {
Fiber.abort("FirstSecond argument must be a sequencepositive other than a rangeinteger.")
}
_seq = seq
_step = (step < 1) ? 1 : step // minimum step of 1
}
 
Line 80 ⟶ 83:
}
 
/*
Reversed wraps a Sequence (other than a range) so it can be iterated in reverse
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)) ||Fiber.abort("First seqargument ismust Range)be {a sequence.")
if (!((step is Num) && step.isInteger && step > 0)) {
Fiber.abort("First argument must be a sequence other than a range.")
if (seq is Range) Fiber.abort("FirstSecond argument cannotmust be a rangepositive integer.")
}
_seq = (seq is List) ? seq : seq.toList
_step = (step < 1) ? 1 : step // minimum step of 1
}
 
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 (other than a range) so its elements can be iterated over
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 = !reversed(seq is List) ? Stepped.new(seq, step) : Reversedseq.new(seq, step)toList
_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.
constructstatic new(seq) { new(seq, 1, false) }
if (!(seq is Sequence) || seq is Range) {
Fiber.abort("Argument must be a sequence other than a range.")
}
_seq = seq
}
 
// 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.
9,482

edits