Array Utilities
Array Utilities
Generic, dependency-free array helpers (IsEmpty, Last, MapTo, Filter, FindFirst, Any, All, CountWhere, Swap) as parametric extension methods.
Original, compile-verified Verse by Bizanator (Biloxi Studios). Clean-room sample you can drop into a UEFN project and adapt.
# Array Utilities — Biloxi Studios Inc.
#
# Small, generic, dependency-free helpers for working with arrays in Verse,
# written as parametric (`where t:type`) functions and extension methods so they
# work for any element type. Import the module and call e.g. `MyArray.IsEmpty[]`.
#
# Teaches: parametric types, <decides>/<transacts> effects, extension-method
# syntax `(Receiver:type).Method()`, predicate callbacks, and option results.
using { /Verse.org/Simulation }
array_utils<public> := module:
# Succeeds when the array has no elements. Use in a failure context:
# if (MyArray.IsEmpty[]) { ... }
(Source:[]t where t:type).IsEmpty<public>()<decides><transacts>:void =
Source.Length = 0
# Return the last element, or false (the empty option) when the array is empty.
(Source:[]t where t:type).Last<public>()<transacts>:?t =
if (Element := Source[Source.Length - 1]):
option{Element}
else:
false
# Apply Transform to every element and collect the results into a new array.
(Source:[]t where t:type).MapTo<public>(Transform(Element:t):u where u:type):[]u =
var Result:[]u = array{}
for (Element : Source):
set Result += array{Transform(Element)}
Result
# Keep only the elements for which Predicate succeeds.
(Source:[]t where t:type).Filter<public>(Predicate(Element:t)<decides><transacts>:void)<transacts>:[]t =
var Result:[]t = array{}
for (Element : Source):
if (Predicate[Element]):
set Result += array{Element}
Result
# Find the first element matching Predicate, or false when none match.
(Source:[]t where t:type).FindFirst<public>(Predicate(Element:t)<decides><transacts>:void)<transacts>:?t =
for (Element : Source):
if (Predicate[Element]):
return option{Element}
false
# Succeeds when ANY element matches Predicate.
(Source:[]t where t:type).Any<public>(Predicate(Element:t)<decides><transacts>:void)<decides><transacts>:void =
var Matched:logic = false
for (Element : Source):
if (Predicate[Element]):
set Matched = true
Matched?
# Succeeds when EVERY element matches Predicate (true for the empty array).
(Source:[]t where t:type).All<public>(Predicate(Element:t)<decides><transacts>:void)<decides><transacts>:void =
for (Element : Source):
Predicate[Element]
# Count how many elements match Predicate.
(Source:[]t where t:type).CountWhere<public>(Predicate(Element:t)<decides><transacts>:void)<transacts>:int =
var Count:int = 0
for (Element : Source):
if (Predicate[Element]):
set Count += 1
Count
# Return a copy with the elements at the two indices swapped. Fails (in a
# failure context) if either index is out of range.
(Source:[]t where t:type).Swap<public>(FirstIndex:int, SecondIndex:int)<decides><transacts>:[]t =
Swapped := Source.ReplaceElement[FirstIndex, Source[SecondIndex]]
Swapped.ReplaceElement[SecondIndex, Source[FirstIndex]]