Category talk:Wren-seq: Difference between revisions

→‎Source code: Added Lst.lshift/rshift methods and made some existing methods 'chaining' friendly.
(→‎Source code: Added insertAll, clearPart and truncate methods to Lst lass.)
(→‎Source code: Added Lst.lshift/rshift methods and made some existing methods 'chaining' friendly.)
Line 119:
 
// The following analogous methods refill existing one, two or three dimensional lists with 'filler'
// or, if appropriate, a copy thereof and return the list.
static refill(a, filler, copier) {
Lst.isList_(a)
for (c in 0...a.count) a[c] = !copier ? filler : copier.call(filer)
return a
}
 
Line 130 ⟶ 131:
for (c in 0...a[0].count) a[r][c] = !copier ? filler : copier.call(filler)
}
return a
}
 
Line 139 ⟶ 141:
}
}
return a
}
 
Line 158 ⟶ 161:
}
 
// Analogous method to serialFill which refills an existing list with a series of numbers and returns it.
static serialRefill(a, start, step) {
isList_(a)
Line 164 ⟶ 167:
if (step.type != Num) Fiber.abort("'step' must be a number.")
for (i in 0...a.count) a[i] = start + i*step
return a
}
 
Line 194 ⟶ 198:
return other
}
 
// Performs a circular shift of the elements of 'a' 'n' places to the left.
// If 'n' is negative performs a circular right shift by '-n' places instead.
// Returns 'a' after any mutation.
static lshift(a, n) {
Lst.isList_(a)
if (!(n is Num) || !n.isInteger) Fiber.abort("'n' must be an integer.")
var count = a.count
if (count < 2) return a
if (n < 0) return rshift(a, -n)
n = n % count
if (n == 0) return a
for (i in 1..n) {
var t = a[0]
for (j in 0..count-2) a[j] = a[j+1]
a[-1] = t
}
return a
}
 
// Performs a circular shift of the elements of 'a' 'n' places to the right.
// If 'n' is negative performs a circular left shift by '-n' places instead.
// Returns 'a' after any mutation.
static rshift(a, n) {
Lst.isList_(a)
if (!(n is Num) || !n.isInteger) Fiber.abort("'n' must be an integer.")
var count = a.count
if (count < 2) return a
if (n < 0) return lshift(a, -n)
n = n % count
if (n == 0) return a
for (i in 1..n) {
var t = a[-1]
for (j in count-2..0) a[j+1] = a[j]
a[0] = t
}
return a
}
 
// Convenience versions of the above methods which shift by just 1 place.
static lshift(a) { lshift(a, 1) }
static rshift(a) { rshift(a, 1) }
 
// Searches an unsorted list linearly for a particular value from a start index.
Line 411 ⟶ 457:
}
 
// Removes consecutive repeated elements from a list (not a copy) and returns it.
// If the list is sorted, it removes all duplicates.
static prune(a) {
isList_(a)
var c = a.count
if (c < 2) return a
for (i in c-1..1) {
if (a[i-1] == a[i]) a.removeAt(i)
}
return a
}
 
Line 472 ⟶ 519:
}
 
// Removes all elements of 'a' between indices 'start' and 'end' inclusive. and returns it.
static clearPart(a, start, end) {
isList_(a)
if (a.isEmpty) return a
checkStart_(a, start)
checkStart_(a, end)
Line 482 ⟶ 529:
if (end < start) Fiber.abort("'end' cannot be less than 'start'.")
for (i in end..start) a.removeAt(i)
return a
}
 
// Removes all elements of 'a' from index 'start' to the end and returns it.
static truncate(a, start) { clearPart(a, start, -1) }
clearPart(a, start, -1)
}
 
// Return a map whose keys are 'k' and corresponding values 'v'
9,476

edits