Category talk:Wren-trait: Difference between revisions

Content added Content deleted
m (→‎Source code: Added quotes to 'lang' attribute.)
(→‎Source code: Improved the robustness of the iteration classes.)
Line 13: Line 13:
*/
*/
class CloneableSeq is Sequence {
class CloneableSeq is Sequence {
clone() { this } /* to be overridden by child class */
clone() { this } /* to be overridden by child class */
}
}


Line 29: Line 29:


< (other) { compare(other) < 0 }
< (other) { compare(other) < 0 }
> (other) { compare(other) > 0 }
> (other) { compare(other) > 0 }
<=(other) { compare(other) <= 0 }
<=(other) { compare(other) <= 0 }
>=(other) { compare(other) >= 0 }
>=(other) { compare(other) >= 0 }
Line 41: Line 41:
construct new(seq, step) {
construct new(seq, step) {
if (!(seq is Sequence)) Fiber.abort("First argument must be a sequence.")
if (!(seq is Sequence)) Fiber.abort("First argument must be a sequence.")
if (!((step is Num) && step.isInteger && step > 0)) {
Fiber.abort("Second argument must be a positive integer.")
}
_seq = seq
_seq = seq
_step = (step < 1) ? 1 : step // minimum step of 1
_step = step
}
}


Line 80: Line 83:
}
}


/*
/*
Reversed wraps a Sequence (other than a range) so it can be iterated in reverse
Reversed wraps a Sequence so it can be iterated in reverse
and by steps other than 1.
and by steps other than 1. To ensure this always works, non-lists
are converted internally to lists.
*/
*/
class Reversed is Sequence {
class Reversed is Sequence {
// Constructs a new reversed sequence.
// Constructs a new reversed sequence.
construct new(seq, step) {
construct new(seq, step) {
if (!(seq is Sequence) || seq is Range) {
if (!(seq is Sequence)) Fiber.abort("First argument must be a sequence.")
if (!((step is Num) && step.isInteger && step > 0)) {
Fiber.abort("First argument must be a sequence other than a range.")
Fiber.abort("Second argument must be a positive integer.")
}
}
_seq = seq
_seq = (seq is List) ? seq : seq.toList
_step = (step < 1) ? 1 : step // minimum step of 1
_step = step
}
}


Line 104: Line 109:
} else if (it == false) {
} else if (it == false) {
it = _seq.count - 1 - _step
it = _seq.count - 1 - _step
} else {
} else {
it = it - 1 - _step
it = it - 1 - _step
}
}
return (it >= 0) ? it : false
return (it >= 0) ? it : false
}
}
Line 130: Line 135:


/*
/*
Indexed wraps a Sequence (other than a range) so its elements can be iterated over
Indexed wraps a Sequence so its elements can be iterated over
together with their zero-based indices.
together with their zero-based indices. To ensure this always works, non-lists
are converted internally to lists.
*/
*/
class Indexed is Sequence {
class Indexed is Sequence {
// Constructs a new indexed sequence with a step of 'step' and optionally reversed.
// Constructs a new indexed sequence with a step of 'step' and optionally reversed.
construct new(seq, step, 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.")
if (!(reversed is Bool)) Fiber.abort("Third argument must be true or false.")
_seq = !reversed ? Stepped.new(seq, step) : Reversed.new(seq, step)
_seq = (seq is List) ? seq : seq.toList
_seq = !reversed ? Stepped.new(_seq, step) : Reversed.new(_seq, step)
}
}


Line 144: Line 150:
static new(seq, step) { new(seq, step, false) }
static new(seq, step) { new(seq, step, false) }


// Constructs a new indexed sequence with a step of 1.
// Constructs a new indexed sequence with a step of 1 and reversed set to false.
construct new(seq) {
static 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.
// Iterator protocol methods.
Line 184: Line 185:
static [key] { __map ? __map[key] : null }
static [key] { __map ? __map[key] : null }


// Retrieves a ByKey object's key from the object it wraps.
// 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
// 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.
// not be the lowest. Returns 0 if the object is unwrapped.