abstract rtconst class sys::List
sys::Obj sys::List
List represents an liner sequence of Objects indexed by an Int.
See examples.
- add
-
@Operator
abstract This add(List^V item)Add the specified item to the end of the list. The item will have an index of size. Size is incremented by 1. Return this. Throw ReadonlyErr if readonly.
- addAll
-
abstract This addAll(List<List^V> list)
Add all the items in the specified list to the end of this list. Size is incremented by list.size. Return this. Throw ReadonlyErr if readonly.
- addIfNotNull
-
This addIfNotNull(List^V? item)
Call
add
if item is non-null otherwise do nothing. Return this. - all
-
Bool all(Func<Bool,List^V,Int> c)
Return true if c returns true for all of the items in the list. If the list is empty, return true. This method is readonly safe.
Example:
list := ["ant", "bear"] list.all |Str v->Bool| { return v.size >= 3 } => true list.all |Str v->Bool| { return v.size >= 4 } => false
- any
-
Bool any(Func<Bool,List^V,Int> c)
Return true if c returns true for any of the items in the list. If the list is empty, return false. This method is readonly safe.
Example:
list := ["ant", "bear"] list.any |Str v->Bool| { return v.size >= 4 } => true list.any |Str v->Bool| { return v.size >= 5 } => false
- binaryFind
-
virtual Int binaryFind(Func<Int,List^V,Int> c)
Find an element in the list using a binary search algorithm. The specified comparator function returns a negative integer, zero, or a positive integer if the desired object is less than, equal to, or greater than specified item. The list must be sorted in ascending order according to the specified comparator function. If the key is not found, then return a negative value which is
-(insertation point) - 1
. - binarySearch
-
virtual Int binarySearch(List^V key, Func<Int,List^V,List^V>? c := null)
Search the list for the index of the specified key using a binary search algorithm. The list must be sorted in ascending order according to the specified comparator function. If the list contains multiple matches for key, no guarantee is made to which one is returned. If the comparator is null then then it is assumed to be the
<=>
operator (shortcut for thecompare
method). If the key is not found, then return a negative value which is-(insertation point) - 1
. - capacity
-
abstract Int capacity
The number of items this list can hold without allocating more memory. Capacity is always greater or equal to size. If adding a large number of items, it may be more efficient to manually set capacity. See the
trim
method to automatically set capacity to size. Throw ArgErr if attempting to set capacity less than size. Getting capacity is readonly safe, setting capacity throws ReadonlyErr if readonly. - clear
-
abstract This clear()
Remove all items from the list and set size to 0. Return this. Throw ReadonlyErr if readonly.
- contains
-
Return if this list contains the specified item. Equality is determined by
Obj.equals
. This method is readonly safe. - containsAll
-
virtual Bool containsAll(List<List^V> list)
Return if this list contains every item in the specified list. Equality is determined by
Obj.equals
. This method is readonly safe. - containsAny
-
virtual Bool containsAny(List<List^V> list)
Return if this list contains any one of the items in the specified list. Equality is determined by
Obj.equals
. This method is readonly safe. - containsSame
-
Bool containsSame(List^V item)
Return if this list contains the specified item. Equality is determined by
===
. This method is readonly safe. - defVal
- dup
-
Create a shallow duplicate copy of this List. The items themselves are not duplicated. This method is readonly safe.
- each
-
virtual Void each(Func<Void,List^V,Int> c)
Call the specified function for every item in the list starting with index 0 and incrementing up to size-1. This method is readonly safe.
Example:
["a", "b", "c"].each |Str s| { echo(s) }
- eachRange
-
virtual Void eachRange(Range r, Func<Void,List^V,Int> c)
Iterate the list usnig the specified range. Negative indexes may be used to access from the end of the list. This method is readonly safe. Throw IndexErr if range is invalid.
- eachWhile
-
virtual Obj? eachWhile(Func<Obj?,List^V,Int> c, Int offset := 0)
Iterate every item in the list starting with index 0 up to size-1 until the function returns non-null. If function returns non-null, then break the iteration and return the resulting object. Return null if the function returns null for every item. This method is readonly safe.
- eachr
-
virtual Void eachr(Func<Void,List^V,Int> c)
Reverse each - call the specified function for every item in the list starting with index size-1 and decrementing down to 0. This method is readonly safe.
Example:
["a", "b", "c"].eachr |Str s| { echo(s) }
- eachrWhile
-
virtual Obj? eachrWhile(Func<Obj?,List^V,Int> c, Int offset := -1)
Reverse
eachWhile
. Iterate every item in the list starting with size-1 down to 0. If the function returns non-null, then break the iteration and return the resulting object. Return null if the function returns null for every item. This method is readonly safe. - equals
-
virtual override Bool equals(Obj? other)
Two Lists are equal if they have the same type, the same number of items, and all the items at each index return true for
equals
.Examples:
[2, 3] == [2, 3] => true [2, 3] == [3, 2] => false [2, 3] == Num[2, 3] => false Str[,] == [,] => false Str[,] == Str?[,] => false
- exclude
-
List<List^V> exclude(Func<Bool,List^V,Int> c)
Return a new list containing the items for which c returns false. If c returns true for every item, then return an empty list. The inverse of this method is findAll(). This method is readonly safe.
Example:
list := [0, 1, 2, 3, 4] list.exclude |Int v->Bool| { return v%2==0 } => [1, 3]
- fill
-
virtual This fill(List^V val, Int times)
Append a value to the end of the list the given number of times. Return this. Throw ReadonlyErr if readonly.
Example:
Int[,].fill(0, 3) => [0, 0, 0]
- find
-
List^V? find(Func<Bool,List^V,Int> c)
Return the first item in the list for which c returns true. If c returns false for every item, then return null. This method is readonly safe.
Example:
list := [0, 1, 2, 3, 4] list.find |Int v->Bool| { return v.toStr == "3" } => 3 list.find |Int v->Bool| { return v.toStr == "7" } => null
- findAll
-
List<List^V> findAll(Func<Bool,List^V,Int> c)
Return a new list containing the items for which c returns true. If c returns false for every item, then return an empty list. The inverse of this method is exclude(). This method is readonly safe.
Example:
list := [0, 1, 2, 3, 4] list.findAll |Int v->Bool| { return v%2==0 } => [0, 2, 4]
- findIndex
-
virtual Int findIndex(Func<Bool,List^V,Int> c, Int offset := 0)
Return the index of the first item in the list for which c returns true. If c returns false for every item, then return -1. This method is readonly safe.
Example:
list := [5, 6, 7] list.findIndex |Int v->Bool| { return v.toStr == "7" } => 2 list.findIndex |Int v->Bool| { return v.toStr == "9" } => -1
- findrIndex
-
virtual Int findrIndex(Func<Bool,List^V,Int> c, Int offset := -1)
- first
-
virtual List^V? first()
Return the item at index 0, or if empty return null. This method is readonly safe.
- flatMap
-
List<Obj?> flatMap(Func<List<Obj?>,List^V,Int> c)
This is a combination of
map
andflatten
. Each item in this list is mapped to zero or more new items by the given function and the results are returned in a single flattened list. Note unlikeflatten
only one level of flattening is performed. The new list is typed based on the return type of c. This method is readonly safe.Example:
list := ["a", "b"] list.flatMap |v->Str[]| { [v, v.upper] } => ["a", "A", "b", "B"]
- flatten
-
Return a new list which recursively flattens any list items into a one-dimensional result. This method is readonly safe.
Examples:
[1,2,3].flatten => [1,2,3] [[1,2],[3]].flatten => [1,2,3] [1,[2,[3]],4].flatten => [1,2,3,4]
- get
-
@Operator
abstract List^V get(Int index)Get is used to return the item at the specified the index. A negative index may be used to access an index from the end of the list. The get method is accessed via the [] shortcut operator. Throw IndexErr if index is out of range. This method is readonly safe.
- getRange
-
@Operator
abstract List<List^V> getRange(Range r)Return a sub-list based on the specified range. Negative indexes may be used to access from the end of the list. This method is accessed via the
[]
operator. This method is readonly safe. Throw IndexErr if range illegal.Examples:
list := [0, 1, 2, 3] list[0..2] => [0, 1, 2] list[3..3] => [3] list[-2..-1] => [2, 3] list[0..<2] => [0, 1] list[1..-2] => [1, 2]
- getSafe
-
List^V? getSafe(Int index, List^V? defV := null)
Get the item at the specified index, but if index is out of range, then return
def
parameter. A negative index may be used according to the same semantics asget
. This method is readonly safe. - hash
-
virtual override Int hash()
Return platform dependent hashcode based a hash of the items of the list.
- index
-
Int index(List^V item, Int offset := 0)
Return the integer index of the specified item using the
==
operator (shortcut for equals method) to check for equality. UseindexSame
to find with===
operator. The search starts at the specified offset and returns the first match. The offset may be negative to access from end of list. Throw IndexErr if offset is out of range. If the item is not found return -1. This method is readonly safe. - indexSame
-
Int indexSame(List^V item, Int offset := 0)
Return integer index just like
List.index
except use===
same operator instead of the==
equals operator. - indexr
-
Int indexr(List^V item, Int offset := -1)
Reverse index lookup. This method works just like
index
except that it searches backward from the starting offset. - insert
-
abstract This insert(Int index, List^V item)
Insert the item at the specified index. A negative index may be used to access an index from the end of the list. Size is incremented by 1. Return this. Throw IndexErr if index is out of range. Throw ReadonlyErr if readonly.
- insertAll
-
abstract This insertAll(Int index, List<List^V> list)
Insert all the items in the specified list into this list at the specified index. A negative index may be used to access an index from the end of the list. Size is incremented by list.size. Return this. Throw IndexErr if index is out of range. Throw ReadonlyErr if readonly.
- isEmpty
-
Bool isEmpty()
Return if size == 0. This method is readonly safe.
- isImmutable
-
abstract override Bool isImmutable()
- isRO
-
abstract Bool isRO()
Return if this List is readonly. A readonly List is guaranteed to be immutable (although its items may be mutable themselves). Any attempt to modify a readonly List will result in ReadonlyErr. Use
rw
to get a read-write List from a readonly List. Methods documented as "readonly safe" may be used safely with a readonly List. This method is readonly safe. - isRW
-
Bool isRW()
Return if this List is read-write. A read-write List is mutable and may be modified. Use
ro
to get a readonly List from a read-write List. This method is readonly safe. - join
-
Str join(Str separator := "", Func<Str,List^V,Int>? c := null)
Return a string by concatenating each item's toStr result using the specified separator string. If c is non-null then it is used to format each item into a string, otherwise Obj.toStr is used. This method is readonly safe.
Example:
["a", "b", "c"].join => "abc" ["a", "b", "c"].join("-") => "a-b-c" ["a", "b", "c"].join("-") |Str s->Str| { return "($s)" } => "(a)-(b)-(c)"
- last
-
virtual List^V? last()
Return the item at index-1, or if empty return null. This method is readonly safe.
- make
-
static new make(Int capacity)
Constructor with of type and initial capacity.
- map
-
List<Obj?> map(Func<Obj?,List^V,Int> c)
Create a new list which is the result of calling c for every item in this list. The new list is typed based on the return type of c. This method is readonly safe.
Example:
list := [3, 4, 5] list.map |Int v->Int| { return v*2 } => [6, 8, 10]
- max
-
List^V? max(Func<Int,List^V,List^V>? c := null)
Return the maximum value of the list. If c is provided, then it implements the comparator returning -1, 0, or 1. If c is null then the <=> operator is used (shortcut for compare method). If the list is empty, return null. This method is readonly safe.
Example:
list := ["albatross", "dog", "horse"] list.max => "horse" list.max |Str a, Str b->Int| { return a.size <=> b.size } => "albatross"
- min
-
List^V? min(Func<Int,List^V,List^V>? c := null)
Return the minimum value of the list. If c is provided, then it implements the comparator returning -1, 0, or 1. If c is null then the <=> operator is used (shortcut for compare method). If the list is empty, return null. This method is readonly safe.
Example:
list := ["albatross", "dog", "horse"] list.min => "albatross" list.min |Str a, Str b->Int| { return a.size <=> b.size } => "dog"
- moveTo
-
virtual This moveTo(List^V? item, Int toIndex)
Find the given item, and move it to the given index. All the other items are shifted accordingly. Negative indexes may used to access an index from the end of the list. If the item is null or not found then this is a no op. Return this. Throw ReadonlyErr if readonly.
Examples:
[10, 11, 12].moveTo(11, 0) => [11, 10, 12] [10, 11, 12].moveTo(11, -1) => [10, 12, 11]
- peek
-
List^V? peek()
Return the item at index-1, or if empty return null. This method has the same semantics as last(). This method is readonly safe.
- pop
-
virtual List^V? pop()
Remove the last item and return it, or return null if the list is empty. This method as the same semantics as remove(-1), with the exception of has an empty list is handled. Throw ReadonlyErr if readonly.
- privateMake
-
new privateMake()
- push
-
Add the specified item to the end of the list. Return this. This method has the same semantics as add(item). Throw ReadonlyErr if readonly.
- random
-
List^V? random()
Return a random item from the list. If the list is empty return null. This method is readonly safe. Also see
Int.random
,Float.random
,Range.random
, andRandom
. - reduce
-
Obj? reduce(Obj? init, Func<Obj?,Obj?,List^V,Int> c)
Reduce is used to iterate through every item in the list to reduce the list into a single value called the reduction. The initial value of the reduction is passed in as the init parameter, then passed back to the closure along with each item. This method is readonly safe.
Example:
list := [1, 2, 3] list.reduce(0) |Obj r, Int v->Obj| { return (Int)r + v } => 6
- remove
-
virtual List^V? remove(List^V item)
Remove the specified value from the list. The value is compared using the == operator (shortcut for equals method). Use
removeSame
to remove with the === operator. Return the removed value and decrement size by 1. If the value is not found, then return null. Throw ReadonlyErr if readonly. - removeAll
-
virtual This removeAll(List<List^V> list)
Remove every item in this list which is found in the
toRemove
list using same semantics asremove
(compare for equality via the == operator). If any value is not found, it is ignored. Return this. Throw ReadonlyErr if readonly. - removeAt
-
abstract List^V? removeAt(Int index)
Remove the object at the specified index. A negative index may be used to access an index from the end of the list. Size is decremented by 1. Return the item removed. Throw IndexErr if index is out of range. Throw ReadonlyErr if readonly.
- removeRange
-
abstract This removeRange(Range r)
Remove a range of indices from this list. Negative indexes may be used to access from the end of the list. Throw ReadonlyErr if readonly. Throw IndexErr if range illegal. Return this (*not* the removed items).
- removeSame
-
virtual List^V? removeSame(List^V item)
Remove the item just like
remove
except use the === operator instead of the == equals operator. - reverse
-
virtual This reverse()
Reverse the order of the items of this list in-place. Return this. Throw ReadonlyErr if readonly.
Example:
[1, 2, 3, 4].reverse => [4, 3, 2, 1]
- ro
-
virtual This ro()
Get a readonly List instance with the same contents as this List (although the items may be mutable themselves). If this List is already readonly, then return this. Only methods documented as "readonly safe" may be used safely with a readonly List, all others will throw ReadonlyErr. This method is readonly safe. See
Obj.isImmutable
andObj.toImmutable
for deep immutability. - rw
-
virtual This rw()
Get a read-write, mutable List instance with the same contents as this List. If this List is already read-write, then return this. This method is readonly safe.
- set
-
@Operator
abstract This set(Int index, List^V item)Set is used to overwrite the item at the specified the index. A negative index may be used to access an index from the end of the list. The set method is accessed via the []= shortcut operator. If you wish to use List as a sparse array and set values greater then size, then manually set size larger. Return this. Throw IndexErr if index is out of range. Throw ReadonlyErr if readonly.
- shuffle
-
This shuffle()
Shuffle this list's items into a randomized order. Return this. Throw ReadonlyErr if readonly.
- size
-
abstract Int size
The number of items in the list. Getting size is readonly safe, setting size throws ReadonlyErr if readonly.
If the size is set greater than the current size then the list is automatically grown to be a sparse list with new items defaulting to null. However if this is a non-nullable list, then growing a list will throw ArgErr.
If the size is set less than the current size then any items with indices past the new size are automatically removed. Changing size automatically allocates new storage so that capacity exactly matches the new size.
- slice
-
virtual List<List^V> slice(Range r)
Return a sub-list based on the specified range
- sort
-
virtual This sort(Func<Int,List^V,List^V>? c := null)
Perform an in-place sort on this list. If a method is provided it implements the comparator returning -1, 0, or 1. If the comparator method is null then sorting is based on the value's <=> operator (shortcut for
compare
method). Return this. Throw ReadonlyErr if readonly.Example:
s := ["candy", "ate", "he"] s.sort // s now evaluates to [ate, candy, he] s.sort |Str a, Str b->Int| { return a.size <=> b.size } // s now evaluates to ["he", "ate", "candy"]
- sortr
-
This sortr(Func<Int,List^V,List^V>? c := null)
Reverse sort - perform an in-place reverse sort on this list. If a method is provided it implements the comparator returning -1, 0, or 1. If the comparator method is null then sorting is based on the items <=> operator (shortcut for
compare
method). Return this. Throw ReadonlyErr if readonly.Example:
[3, 2, 4, 1].sortr => [4, 3, 2, 1]
- swap
-
virtual This swap(Int indexA, Int indexB)
Swap the items at the two specified indexes. Negative indexes may used to access an index from the end of the list. Return this. Throw ReadonlyErr if readonly.
- toCode
-
Str toCode()
Get this list as a Fantom expression suitable for code generation. The individual items must all respond to the
toCode
method. - toImmutable
- toStr
-
virtual override Str toStr()
Return a string representation the list. This method is readonly safe.
- trim
-
virtual This trim()
Trim the capacity such that the underlying storage is optimized for the current size. Return this. Throw ReadonlyErr if readonly.