Category talk:Wren-seq: Difference between revisions

m
→‎Source code: Now uses Wren S/H lexer.
m (→‎Source code: Added quotes to 'lang' attribute.)
m (→‎Source code: Now uses Wren S/H lexer.)
 
(11 intermediate revisions by the same user not shown)
Line 1:
===Source code===
 
<syntaxhighlight lang="ecmascriptwren">/* Module "seq.wren" */
 
import "./trait" for Cloneable, CloneableSeq
Line 17:
}
return true
}
 
// Returns true if any adjacent elements of the sequence are duplicates, false otherwise.
static hasAdjDup(s) {
isSeq_(s)
var iter = s.iterate(null)
if (!iter) return false
var prev = s.iteratorValue(iter)
while (iter = s.iterate(iter)) {
var next = s.iteratorValue(iter)
if (next == prev) return true
prev = next
}
return false
}
 
Line 28 ⟶ 42:
count = s.count - count
if (count <= 0) count = 0
return sSkipSequence.skipnew(s, count)
}
 
Line 38 ⟶ 52:
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)
}
 
// Returns the first element of a sequence without removing it.
// Returns null if the sequence is empty.
static peek(s) {
isSeq_(s)
for (value in s) {
return value
}
return null
}
 
// Writes a sequence in list format to stdout
// but without having to convert it to a list first.
static write(s) {
isSeq_(s)
System.write("[")
for (e in s) System.write("%(e), ")
System.write("\b\b]")
}
 
// As 'write' followed by a new line.
static print(s) {
write(s)
System.print()
}
}
Line 122 ⟶ 191:
static refill(a, filler, copier) {
Lst.isList_(a)
for (c in 0...a.count) a[c] = !copier ? filler : copier.call(filerfiller)
return a
}
Line 147 ⟶ 216:
static refill2(a, filler) { refill2(a, filler, null) }
static refill3(a, filler) { refill3(a, filler, null) }
 
// Creates a new list of size 'size' and copies all the elements (or the first 'size' elements)
// of 'a' into it starting from index 0. Any remaining slots are filled by a copy of 'filler'.
// 'copier' is a function which takes a single argument and returns a copy of that argument.
static resize(a, size, filler, copier) {
Lst.isList_(a)
if (size.type != Num || !size.isInteger || size < 0) {
Fiber.abort("'size' must be a non-negative integer.")
}
var res = List.filled(size, null)
var rc = a.count.min(size)
for (i in 0...rc) res[i] = !copier ? a[i] : copier.call(a[i])
if (size > a.count) {
for (i in rc...size) res[i] = !copier ? filler : copier.call(filler)
}
return res
}
 
// Overloads of above method where elements/filler are immutable, not needed or null.
static resize(a, size, filler) { resize (a, size, filler, null) }
static resize(a, size) { resize (a, size, null, null) }
 
// Creates a list and fills it with a series of numbers starting from 'start'
Line 580 ⟶ 670:
// Removes all elements of 'a' from index 'start' to the end and returns it.
static truncate(a, start) { clearPart(a, start, -1) }
 
// Expands a list 'a' in place to a length of 'newCount'
// by adding 'element' the required number of times to it.
// If the list's length is already >= newCount, does nothing.
// Returns 'a'.
static expand(a, newCount, element) {
isList_(a)
if (newCount.type != Num || !newCount.isInteger || newCount < 0) {
Fiber.abort("newCount must be a non-negative integer.")
}
if (a.count >= newCount) return a
for (i in a.count...newCount) a.add(element)
return a
}
 
// Returns a clone of 'a' by recursively cloning any elements which are
Line 681 ⟶ 785:
// As 'sortPart' above but uses the default comparer: {|i, j| i < j }.
static sortPart(a, start, end) { sortPart(a, start, end) {|i, j| i < j } }
 
// Returns a list of numbers between 'start' and 'end' inclusive
// having a positive step of 'step' between successive numbers.
// If 'ascend' is true, the numbers are incremented by 'step'
// otherwise they are decremented by 'step'.
// If `ascend' is true and start > end, an empty list is returned.
// If 'ascend' is false and start < end, an empty list is returned.
static between(start, end, step, ascend) {
if (step <= 0) Fiber.abort("Step must be a positive number.")
if ((start > end && ascend) || (start < end && !ascend)) return []
var lst = []
var i = start
if (ascend) {
while (i <= end) {
lst.add(i)
i = i + step
}
} else {
while (i >= end) {
lst.add(i)
i = i - step
}
}
return lst
}
 
// Convenience versions of the above method which use default values for some parameters.
static between(start, end, step) { between(start, end, step, start <= end) }
static between(start, end) { between(start, end, 1, start <= end) }
 
// Generates and returns a list of 'n' numbers starting from and including 'first'
// where each term is found by multiplying the previous one by 'multiplier'.
static multiples(first, n, multiplier) {
if (!(first is Num)) Fiber.abort("'first' must be a number.")
if (!(n is Num) || !n.isInteger || n < 1) {
Fiber.abort("'n' must be a positive integer.")
}
if (!(multiplier is Num)) Fiber.abort("'multiplier' must be a number.")
var lst = List.filled(n, 0)
lst[0] = first
if (n == 1) return lst
for (i in 1...n) {
first = first * multiplier
lst[i] = first
}
return lst
}
 
// As 'multiples' but where 'first' and 'multiplier' are the same.
static powers(first, n) { multiples(first, n, first) }
 
// Builds and returns a list of corresponding columns from a non-empty list 'a'
// of rows, where each row is a non-empty list of any element type. The number
// of columns in each row must be the same.
static columns(a) {
var nr = a.count
if (nr == 0) Fiber.abort("List must contain at least one row.")
var nc = a[0].count
if (nc == 0) Fiber.abort("Rows must have at least one column.")
if (a.skip(1).any { |e| e.count != nc }) {
Fiber.abort("Rows must all have the same number of columns.")
}
var cols = List.filled(nc, null)
for (j in 0...nc) {
cols[j] = List.filled(nr, 0)
for (i in 0...nr) cols[j][i] = a[i][j]
}
return cols
}
}
 
Line 746 ⟶ 919:
pop() {
var item = peek()
if (!(item !=is nullNull)) {
_stack.removeAt(-1)
}
9,476

edits