luxe: containers
import "luxe: containers" for ListsThe
ListsAPI works with the built in WrenListtype, offering more tools to operate on them.
Lists.first(.)
Section titled “Lists.first(.)”Lists.first(list : Sequence) : AnyReturns the first element of a Sequence, null if the sequence is empty.
var list = ["twig", 2, null, "tree", "petals", "faeries", 9]Lists.first(list) //"twig"
Lists.first(..)
Section titled “Lists.first(..)”Lists.first(list : Sequence, callback : Fn) : AnySearches for the first element in a sequence that fulfills a requirement and returns it. If no element fulfills the requirement, null is returned.
var list = ["twig", 2, null, "tree", "petals", "faeries", 9]Lists.first(list){|elem| elem is String && elem.count > 4} //"petals"
Lists.binary_search(..)
Section titled “Lists.binary_search(..)”Lists.binary_search(list : List, value : Any) : NumSearches for
valueinlistusing a binary search. Binary searches can be more efficient for finding items when there are many. This requires the list to be sorted, and values in the list to be comparable with>/<.Returns the index in the list, or
-1if not found.var to_find = 9var list = [1,3,7,9,23,54]var index = Lists.binary_search(list, to_find) //index is 3
Lists.binary_search_first(…)
Section titled “Lists.binary_search_first(…)”Lists.binary_search_first(list : List, value : Any, fn : Fn) : NumSimilar to
binary_searchbut handles comparison via a callback. The callback should return 0 for equal, -1 for lower and 1 for higher. The callback puts the input value in the first argument.Returns the index in the list, or
-1if not found.var list = [1,3,7,9,23,54]var index = Lists.binary_search_first(list, 9) {|value, other|if(value == to_find) return 0if(value < to_find) return -1return 1}
Lists.equal(..)
Section titled “Lists.equal(..)”Lists.equal(a : List, b : List) : BoolCompares two flat lists, returning true if the contents are the same and in the same order. Does not recurse nested lists. Uses
a[i] != b[i]to compare.var listA = [1,9,7]var listB = [1,7,9]var equalA = Lists.equal(listA, [1,7,9]) //falsevar equalB = Lists.equal(listB, [1,7,9]) //true
Lists.equalish(..)
Section titled “Lists.equalish(..)”Lists.equalish(a : List, b : List) : BoolSimilar to
equalbut values don’t need to be in the same order.var listA = [1,9,7]var listB = [1,7,9]var equalA = Lists.equal(listA, [1,7,9]) //truevar equalB = Lists.equal(listB, [1,7,9]) //true
Lists.flatten(.)
Section titled “Lists.flatten(.)”Lists.flatten(list : List) : ListConverts a nested list of lists to a single flat list of values.
var list = [1,[2,3,[4,[5]]]]var flat = Lists.flatten(list) //[1,2,3,4,5]
Lists.add_unique(..)
Section titled “Lists.add_unique(..)”Lists.add_unique(list : List, value : Any) : BoolAdd an item to a list if the value doesn’t already exist in the list. Uses
list.indexOfto check.Returns true if the value was unique and added to the list.
var list = [1,2,3]Lists.add_unique(list, 0) //trueLists.add_unique(list, 1) //false, already found
Lists.append(..)
Section titled “Lists.append(..)”Lists.append(into : List, list : List) : NoneAppend
listat the end ofintowithout allocating a new list. This function modifiesinto.Note that in Wren,
Listimplements+, which is append too, but that makes a new list with the two combined.[1] + [2] = [1, 2]var list = [1,2]Lists.append(list, [3,4,5])Log.print(list) //[1,2,3,4,5]
Lists.prepend(..)
Section titled “Lists.prepend(..)”Lists.prepend(into : List, list : List) : NoneSimilar to
append, but adds the items fromlistto the front ofinto. This function modifiesinto.var list = [1,2]Lists.prepend(list, [3,4,5])Log.print(list) //[3,4,5,1,2]
Lists.remove_where(…)
Section titled “Lists.remove_where(…)”Lists.remove_where(list : List, value : Any, fn : Fn) : AnySimilar to
list.removebut uses a function for the find/equality check. UsesLists.index_of_whereto find the index, so the callback msut return true if the values are equal or false if not.Returns the value if it was removed, or null if it wasn’t found.
var list = [1,2,3]var fn = Fn.new {|value, other| value == other }Lists.remove_where(list, 3, fn) //3Lists.remove_where(list, 6, fn) //nullLog.print(list) //[1,2]
Lists.remove_where(..)
Section titled “Lists.remove_where(..)”Lists.remove_where(list : List, fn : Fn) : AnySimilar to
list.removebut uses a function for the find/equality check. UsesLists.index_of_whereto find the index, so the callback msut return true if the values are equal or false if not.Returns the value if it was removed, or null if it wasn’t found.
var list = [1,2,3]var fn = Fn.new {|value, other| value == other }Lists.remove_where(list, fn) //3Lists.remove_where(list, fn) //nullLog.print(list) //[1,2]
Lists.contains(..)
Section titled “Lists.contains(..)”Lists.contains(list : Any, item : Any) : unknownDeprecated. Use list.contains(item) Returns true if the list contains the item.
Lists.remove(..)
Section titled “Lists.remove(..)”Lists.remove(list : Any, to_remove : Any) : unknownDeprecated. Use list.remove(item) Returns the item if removed, or null.
Lists.index_of(..)
Section titled “Lists.index_of(..)”Lists.index_of(list : Any, item : Any) : unknownDeprecated. Use list.indexOf(item) Returns the index, or -1 if the item isn’t found.
Lists.index_of_where(..)
Section titled “Lists.index_of_where(..)”Lists.index_of_where(list : List, fn : Fn) : NumReturns the index of
valueinlistor-1if not found, where comparison is handled by a callback function.var list = [1,2,3]Lists.index_of_where(list, 3) {|value, other| value == other } //2
Lists.index_of_where(…)
Section titled “Lists.index_of_where(…)”Lists.index_of_where(list : List, value : Any, fn : Fn) : NumReturns the index of
iteminlistor-1if not found, where comparison is handled by a callback function.var list = [1,2,3]Lists.index_of_where(list, 3) {|value, other| value == other } //2
Lists.bubble_sort(..)
Section titled “Lists.bubble_sort(..)”Lists.bubble_sort(list : List, compare : Fn) : NoneIn-place sorting of
listusing thecomparefunction. Modifieslist. Uses bubble sort. The compare function should return0for equal,-1for lower values and1for higher values.var list = [5,2,67,23]Lists.bubble_sort(list) {|a, b| b - a }Log.print(list) // [67, 23, 5, 2]Lists.bubble_sort(list) {|a, b| a - b }Log.print(list) // [2, 5, 23, 67]
Lists.quicksort(..)
Section titled “Lists.quicksort(..)”Lists.quicksort(list : List, compare : Fn) : ListIn-place sorting of
listusing thecomparefunction. Modifieslist. Uses quick sort. The compare function should return0for equal,-1for lower values and1for higher values.var list = [5,2,67,23]Lists.quicksort(list) {|a, b| b - a }Log.print(list) // [67, 23, 5, 2]Lists.quicksort(list) {|a, b| a - b }Log.print(list) // [2, 5, 23, 67]
Lists.quicksort(…)
Section titled “Lists.quicksort(…)”Lists.quicksort(list : List, low : Num, high : Num, compare : Fn) : ListSame as
quicksortbut a low and high index can be specified to sort just a portion of a list. The default forquicksort(list, compare)islow = 0,high = list.count-1.var list = [5,2,34,89,11,60,45]Lists.quicksort(list, 2, 5) {|a, b| a - b }Log.print(list) // [5, 2, |11, 34, 60, 89|, 45]//note only the range between | was sorted
MapOrdered
Section titled “MapOrdered”import "luxe: containers" for MapOrderedA
Mapwrapper that keeps the order of the keys the same in which they’re added. Note: The WrenMapclass doesn’t guarantee order of keys.
MapOrdered.keys
Section titled “MapOrdered.keys”MapOrdered.keys : ListReturns the list of
keysin the Map. Don’t modify this.var map = MapOrdered.new()map["one"] = 1map["two"] = 2Log.print(map.keys) //["one", "two"]
MapOrdered.map
Section titled “MapOrdered.map”MapOrdered.map : MapAccess to the underlying Wren
Mapdata. Normally you don’t modify this directly.var map = MapOrdered.new()map["one"] = 1map["two"] = 2Log.print(map.map) //{two: 2, one: 1}
MapOrdered.new(.)
Section titled “MapOrdered.new(.)”MapOrdered.new() : MapOrderedCreate a new ordered map.
var map = MapOrdered.new()
MapOrdered.get(.)
Section titled “MapOrdered.get(.)”MapOrdered.get(key : Any) : unknownReturn the value associated with
key, ornullif not found. You can also usemap[key]as an alternative.var map = MapOrdered.new()map["one"] = 1Log.print(map.get("one")) //1Log.print(map.get("two")) //null
MapOrdered.set(..)
Section titled “MapOrdered.set(..)”MapOrdered.set(key : Any, value : Any) : unknownSet a
valuefor a givenkey. You can also usemap[key] = valueas an alternative.var map = MapOrdered.new()map.set("one", 1)
MapOrdered.containsKey(.)
Section titled “MapOrdered.containsKey(.)”MapOrdered.containsKey(key : Any) : unknownReturns true if
keyis found in the map.var map = MapOrdered.new()map["one"] = 1Log.print(map.containsKey("one")) //trueLog.print(map.containsKey("two")) //false
MapOrdered [key : Any] : unknownReturn the value associated with
key, ornullif not found.var map = MapOrdered.new()map["one"] = 1Log.print(map["one"]) //1Log.print(map["two"]) //null
MapOrdered [key : Any]=(value : Any) : unknownMapOrdered.iterate(.)
Section titled “MapOrdered.iterate(.)”MapOrdered.iterate(iterator : Any) : unknownImplementation details for the Wren iterator protocol.
MapOrdered.iteratorValue(.)
Section titled “MapOrdered.iteratorValue(.)”MapOrdered.iteratorValue(iterator : Any) : unknownImplementation details for the Wren iterator protocol.