slc
Slice domain over ilusm lists - create an empty slice, append a single element, extend with another list, extract a half-open sub-range [lo, hi) with automatic clamping, shallow copy, get the last element, pop the last element, and set an element by index (returns a new list).
Load with: use slc
What this module does
slc adds type-checked, ergonomic list operations on top of the
raw trl primitives. Every function verifies that its list argument
is actually a list and throws a clear eru.bang error otherwise.
Slicing uses half-open ranges (like Python) and silently clamps out-of-bound
indices, so there are no index-out-of-range panics.
Quick example
use slc
xs = slcne() # []
xs = slcap(xs, 10) # [10]
xs = slcap(xs, 20)
xs = slcap(xs, 30)
xs = slcex(xs, [40, 50]) # [10, 20, 30, 40, 50]
sub = slcsu(xs, 1, 4) # [20, 30, 40]
prn(slcle(xs)) # 5
prn(slcla(xs)) # 50 (last)
xs2 = slcpo(xs) # [10, 20, 30, 40] (pop last)
xs3 = slcse(xs, 2, 99) # [10, 20, 99, 40, 50] (set index)
cp = slccp(xs) # shallow copy
Functions
Construction
slcne() / slc.new()Returns an empty slice (empty list).
Modification
slcap(xs, v) / slc.app(xs, v)Returns a new list with v appended at the end.
slcex(xs, ys) / slc.ext(xs, ys)Returns a new list with all elements of ys appended.
slcse(xs, i, v) / slc.set(xs, i, v)Returns a new list with element at index i replaced by v. Throws if i is out of bounds.
Access and slicing
slcsu(xs, lo, hi) / slc.sub(xs, lo, hi)Returns elements from index lo (inclusive) to hi (exclusive). Both bounds are clamped to [0, len(xs)]. Returns [] if hi <= lo.
slccp(xs) / slc.cpy(xs)Returns a shallow copy of the list.
slcle(xs) / slc.len(xs)Returns the length of the list. Throws if not a list.
slcla(xs) / slc.last(xs)Returns the last element. Throws if the list is empty.
slcpo(xs) / slc.pop(xs)Returns a new list with the last element removed. Throws if empty.
Notes
- Requires
trlanderu.