Category talk:Wren-seq: Difference between revisions

→‎Source code: Added 'takeWhile' and 'skipWhile' methods to Seq class plus other small changes.
(→‎Source code: Added Seq.write and Seq.print methods.)
(→‎Source code: Added 'takeWhile' and 'skipWhile' methods to Seq class plus other small changes.)
Line 18:
return true
}
 
// Returns a new 'lazy' sequence that iterates only the last 'count' elements of
// the original sequence.
Line 28 ⟶ 27:
count = s.count - count
if (count <= 0) count = 0
return sSkipSequence.skipnew(s, count)
}
 
Line 38 ⟶ 37:
Fiber.abort("Count must be a non-negative integer.")
}
count = as.count - count
if (count <= 0) count = 0
return aTakeSequence.takenew(s, count)
}
 
// Returns a new 'lazy' sequence that iterates the elements of
// the original sequence only while they continue to satisfy a predicate.
static takeWhile(s, pred) {
isSeq_(s)
if (!((pred is Fn) && pred.arity == 1)) {
Fiber.abort("Predicate must be a function which takes a single argument.")
}
var count = 0
for (e in s) {
if (!pred.call(e)) break
count = count + 1
}
return TakeSequence.new(s, count)
}
 
// Returns a new 'lazy' sequence that skips the elements of
// the original sequence only while they continue to satisfy a predicate.
static skipWhile(s, pred) {
isSeq_(s)
if (!((pred is Fn) && pred.arity == 1)) {
Fiber.abort("Predicate must be a function which takes a single argument.")
}
var count = 0
for (e in s) {
if (!pred.call(e)) break
count = count + 1
}
return SkipSequence.new(s, count)
}
 
9,485

edits