Category talk:Wren-seq: Difference between revisions

Content added Content deleted
(→‎Source code: Added Lst.lshift/rshift methods and made some existing methods 'chaining' friendly.)
(→‎Source code: Added Lst.reverse and Lst.removeBy, removed Lst.toMap (going to new MapUtil module).)
Line 493: Line 493:
if (final > 0) res.add(a[first..-1])
if (final > 0) res.add(a[first..-1])
return res
return res
}

// Reverses a list in place and returns it.
static reverse(a) {
isList_(a)
var c = a.count
if (c < 2) return a
var i = 0
var j = a.count - 1
while (i < j) {
a.swap(i, j)
i = i + 1
j = j - 1
}
return a
}
}


Line 517: Line 532:
}
}
return found ? value : null
return found ? value : null
}

// Removes all elements from 'a' which satisfy the predicate
// function 'fn' and returns a list of the elements removed.
static removeBy(a, fn) {
isList_(a)
if (a.isEmpty) return []
var removals = []
for (i in a.count-1..0) {
var e = a[i]
if (fn.call(e)) {
removals.add(a.remove(e))
}
}
return reverse(removals)
}
}


Line 534: Line 564:
// Removes all elements of 'a' from index 'start' to the end and returns it.
// Removes all elements of 'a' from index 'start' to the end and returns it.
static truncate(a, start) { clearPart(a, start, -1) }
static truncate(a, start) { clearPart(a, start, -1) }

// Return a map whose keys are 'k' and corresponding values 'v'
// where 'k' and 'v' are suitable lists.
static toMap(k, v) {
isList_(k)
isList_(v)
if (k.count != v.count) Fiber.abort("The number of keys and values must be the same.")
var m = {}
for (i in 0...k.count) m[k[i]] = v[i]
return m
}


// Returns a clone of 'a' by recursively cloning any elements which are
// Returns a clone of 'a' by recursively cloning any elements which are