Category talk:Wren-seq: Difference between revisions
Content added Content deleted
(→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: | Line 18: | ||
return true |
return true |
||
} |
} |
||
// Returns a new 'lazy' sequence that iterates only the last 'count' elements of |
// Returns a new 'lazy' sequence that iterates only the last 'count' elements of |
||
// the original sequence. |
// the original sequence. |
||
Line 28: | Line 27: | ||
count = s.count - count |
count = s.count - count |
||
if (count <= 0) count = 0 |
if (count <= 0) count = 0 |
||
return |
return SkipSequence.new(s, count) |
||
} |
} |
||
Line 38: | Line 37: | ||
Fiber.abort("Count must be a non-negative integer.") |
Fiber.abort("Count must be a non-negative integer.") |
||
} |
} |
||
count = |
count = s.count - count |
||
if (count <= 0) count = 0 |
if (count <= 0) count = 0 |
||
return |
return TakeSequence.new(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) |
|||
} |
} |
||