Documentation
¶
Overview ¶
Package arraylist provides a resizable array implementation.
Index ¶
- type ArrayList
- func (l ArrayList) At(index int) (element any, err error)
- func (l *ArrayList) Clear() *ArrayList
- func (l ArrayList) Concat(another ArrayList) ArrayList
- func (l ArrayList) Copy() ArrayList
- func (l ArrayList) Count(element any) (count int)
- func (l ArrayList) Empty() bool
- func (l ArrayList) Equal(another ArrayList) bool
- func (l ArrayList) Every(condition func(any) bool) bool
- func (l *ArrayList) Fill(element any, area ...int) *ArrayList
- func (l ArrayList) Filter(condition func(any) bool) ArrayList
- func (l ArrayList) Find(by func(any) bool) (element any, found bool)
- func (l ArrayList) FindIndex(by func(any) bool) (index int)
- func (l ArrayList) FindIndexes(by func(any) bool, counts ...int) (indexes []int)
- func (l ArrayList) FindLast(by func(any) bool) (element any, found bool)
- func (l ArrayList) FindLastIndex(by func(any) bool) (index int)
- func (l ArrayList) FindLastIndexes(by func(any) bool, counts ...int) (indexes []int)
- func (l ArrayList) FindLasts(by func(any) bool, counts ...int) (elements []any)
- func (l ArrayList) Finds(by func(any) bool, counts ...int) (elements []any)
- func (l *ArrayList) ForEach(action func(any) any) *ArrayList
- func (l ArrayList) Head() (element any, err error)
- func (l ArrayList) Includes(element any) bool
- func (l ArrayList) IndexOf(element any) (index int)
- func (l *ArrayList) Insert(index int, element any) *ArrayList
- func (l ArrayList) LastIndexOf(element any) (index int)
- func (l ArrayList) Len() int
- func (l ArrayList) Map(handler func(any) any) ArrayList
- func (l *ArrayList) Pop(indexes ...int) (element any, err error)
- func (l *ArrayList) Push(elements ...any) (length int)
- func (l ArrayList) Reduce(handler func(any, any) any, initial ...any) (result any, err error)
- func (l ArrayList) ReduceRight(handler func(any, any) any, initial ...any) (result any, err error)
- func (l *ArrayList) Remove(element any, counts ...int) *ArrayList
- func (l *ArrayList) RemoveIf(condition func(any) bool, counts ...int) ArrayList
- func (l *ArrayList) RemoveRight(element any, counts ...int) *ArrayList
- func (l *ArrayList) RemoveRightIf(condition func(any) bool, counts ...int) ArrayList
- func (l *ArrayList) Replace(oldElement, newElement any, counts ...int) *ArrayList
- func (l *ArrayList) ReplaceIf(condition func(any) bool, newElement any, counts ...int) ArrayList
- func (l *ArrayList) ReplaceRight(oldElement, newElement any, counts ...int) *ArrayList
- func (l *ArrayList) ReplaceRightIf(condition func(any) bool, newElement any, counts ...int) ArrayList
- func (l *ArrayList) Reverse() *ArrayList
- func (l *ArrayList) Set(index int, element any) (err error)
- func (l *ArrayList) Shift() (element any, err error)
- func (l ArrayList) Slice(args ...int) ArrayList
- func (l ArrayList) Some(condition func(any) bool) bool
- func (l *ArrayList) Splice(start, deleteCount int, items ...any) ArrayList
- func (l ArrayList) Tail() (element any, err error)
- func (l ArrayList) ToReversed() ArrayList
- func (l ArrayList) ToSpliced(start, deleteCount int, items ...any) ArrayList
- func (l *ArrayList) Unshift(elements ...any) (length int)
- func (l ArrayList) With(index int, element any) ArrayList
Examples ¶
- ArrayList
- ArrayList.At
- ArrayList.Clear
- ArrayList.Concat
- ArrayList.Copy
- ArrayList.Count
- ArrayList.Empty
- ArrayList.Equal
- ArrayList.Every
- ArrayList.Fill
- ArrayList.Filter
- ArrayList.Find
- ArrayList.FindIndex
- ArrayList.FindIndexes
- ArrayList.FindLast
- ArrayList.FindLastIndex
- ArrayList.FindLastIndexes
- ArrayList.FindLasts
- ArrayList.Finds
- ArrayList.ForEach
- ArrayList.Head
- ArrayList.Includes
- ArrayList.IndexOf
- ArrayList.Insert
- ArrayList.LastIndexOf
- ArrayList.Len
- ArrayList.Map
- ArrayList.Pop
- ArrayList.Push
- ArrayList.Reduce
- ArrayList.ReduceRight
- ArrayList.Remove
- ArrayList.RemoveIf
- ArrayList.RemoveRight
- ArrayList.RemoveRightIf
- ArrayList.Replace
- ArrayList.ReplaceIf
- ArrayList.ReplaceRight
- ArrayList.ReplaceRightIf
- ArrayList.Reverse
- ArrayList.Set
- ArrayList.Shift
- ArrayList.Slice
- ArrayList.Some
- ArrayList.Splice
- ArrayList.Tail
- ArrayList.ToReversed
- ArrayList.ToSpliced
- ArrayList.Unshift
- ArrayList.With
- Of
- Repeat
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ArrayList ¶
type ArrayList []any
ArrayList is an enhanced []any, which provides many convenient methods for array manipulation.
Example ¶
al := []any{1, 2, 3, 4, 5} fmt.Println(ArrayList(al))
Output: [1 2 3 4 5]
func Of ¶
Of creates a new ArrayList with the specified elements.
Example ¶
list := Of(1, true, "hello", 2.5, []int{1, 2, 3}) fmt.Println(list)
Output: [1 true hello 2.5 [1 2 3]]
func Repeat ¶
Repeat creates a new ArrayList by repeating the specified element a given number of times.
Example ¶
repeated := Repeat("hello", 3) fmt.Println(repeated)
Output: [hello hello hello]
func (ArrayList) At ¶
At returns the element at the specified index in the list, or an error if the index is out of range.
Example ¶
list := ArrayList{1, 2, 3, 4, 5} element, _ := list.At(2) fmt.Println(element) element, _ = list.At(-1) fmt.Println(element) _, err := list.At(10) fmt.Println(err)
Output: 3 5 the index is out of range
func (*ArrayList) Clear ¶
Clear removes all elements from the list
Example ¶
list := ArrayList{1, 2, 3, 4, 5} fmt.Println(list) list.Clear() fmt.Println(list)
Output: [1 2 3 4 5] []
func (ArrayList) Concat ¶
Concat merges the original list with another list and returns a new merged list.
Example ¶
list1 := ArrayList{1, 2, 3} list2 := ArrayList{4, 5, 6} concatenated := list1.Concat(list2) fmt.Println(concatenated)
Output: [1 2 3 4 5 6]
func (ArrayList) Copy ¶
Copy creates a new ArrayList and copies all elements from the original list to the new list.
Example ¶
l := ArrayList{1, 2, 3, 4, 5} backup := l.Copy() fmt.Println(backup) fmt.Println(l.Equal(backup))
Output: [1 2 3 4 5] true
func (ArrayList) Count ¶
Count returns the number of occurrences of an element in the array list.
Example ¶
al := ArrayList{1, 2, 3, 4, 5, 5, 5} fmt.Println(al.Count(5)) fmt.Println(al.Count(6))
Output: 3 0
func (ArrayList) Empty ¶
Empty checks if the array list is empty.
Example ¶
al := ArrayList{} fmt.Println(al.Empty()) al2 := ArrayList{1, 2, 3} fmt.Println(al2.Empty())
Output: true false
func (ArrayList) Equal ¶
Equal checks if two array lists are equal in terms of length and elements.
Example ¶
al1 := ArrayList{1, 2, 3} al2 := ArrayList{1, 2, 3} al3 := ArrayList{1, 2, 3, 4} al4 := ArrayList{1, 2, 4} fmt.Println(al1.Equal(al2)) fmt.Println(al1.Equal(al3)) fmt.Println(al1.Equal(al4))
Output: true false false
func (ArrayList) Every ¶
Every checks if all items in the list satisfy the given condition function.
Example ¶
list := ArrayList{1, 2, 3, 4, 5} allEven := list.Every(func(item any) bool { return item.(int)%2 == 0 }) fmt.Println(allEven) allPositive := list.Every(func(item any) bool { return item.(int) > 0 }) fmt.Println(allPositive)
Output: false true
func (*ArrayList) Fill ¶
Fill fills a section of the list with the specified element
Example ¶
list := ArrayList{1, 2, 3, 4, 5} fmt.Println(list) list.Fill(0, 1, 3) fmt.Println(list)
Output: [1 2 3 4 5] [1 0 0 4 5]
func (ArrayList) Filter ¶
Filter creates a new list with all items that pass the condition function.
Example ¶
list := ArrayList{1, 2, 3, 4, 5} condition := func(item any) bool { return item.(int)%2 == 0 } filteredList := list.Filter(condition) fmt.Println(filteredList)
Output: [2 4]
func (ArrayList) Find ¶
Find returns the first element in the list that satisfies the provided function.
Example ¶
list := ArrayList{1, 2, 3, 4, 5} even := func(num any) bool { return num.(int)%2 == 0 } fmt.Println(list.Find(even)) negative := func(num any) bool { return num.(int) < 0 } fmt.Println(list.Find(negative))
Output: 2 true <nil> false
func (ArrayList) FindIndex ¶
FindIndex returns the index of the first element in the list that satisfies the provided function, or -1 if no such element is found.
Example ¶
list := ArrayList{1, 2, 3, 4, 5} even := func(num any) bool { return num.(int)%2 == 0 } fmt.Println(list.FindIndex(even)) negative := func(num any) bool { return num.(int) < 0 } fmt.Println(list.FindIndex(negative))
Output: 1 -1
func (ArrayList) FindIndexes ¶
FindIndexes returns the indexes of elements in the list that satisfy the provided function, with a maximum count for each occurrence.
Example ¶
l := ArrayList{1, 2, 3, 4, 5, 2} condition := func(val any) bool { return val.(int) > 2 } fmt.Println(l.FindIndexes(condition)) fmt.Println(l.FindIndexes(condition, 2))
Output: [2 3 4] [2 3]
func (ArrayList) FindLast ¶
FindLast returns the last element in the list that satisfies the provided function.
Example ¶
list := ArrayList{1, 2, 3, 4, 5} even := func(num any) bool { return num.(int)%2 == 0 } fmt.Println(list.Find(even)) fmt.Println(list.FindLast(even))
Output: 2 true 4 true
func (ArrayList) FindLastIndex ¶
FindLastIndex returns the index of the last element in the list that satisfies the provided function, or -1 if no such element is found.
Example ¶
list := ArrayList{1, 2, 3, 4, 5} even := func(num any) bool { return num.(int)%2 == 0 } fmt.Println(list.FindIndex(even)) fmt.Println(list.FindLastIndex(even))
Output: 1 3
func (ArrayList) FindLastIndexes ¶
FindLastIndexes returns the indexes of elements in the list that satisfy the provided function, with a maximum count for each occurrence, in reverse order.
Example ¶
l := ArrayList{1, 2, 3, 4, 5, 2} condition := func(val any) bool { return val.(int) > 2 } fmt.Println(l.FindIndexes(condition)) fmt.Println(l.FindLastIndexes(condition))
Output: [2 3 4] [4 3 2]
func (ArrayList) FindLasts ¶
FindLasts returns the elements in the list that satisfy the provided function, with a maximum count for each occurrence, in reverse order.
Example ¶
l := ArrayList{1, 2, 3, 4, 5, 2} condition := func(val any) bool { return val.(int) > 2 } fmt.Println(l.Finds(condition)) fmt.Println(l.FindLasts(condition))
Output: [3 4 5] [5 4 3]
func (ArrayList) Finds ¶
Finds returns the elements in the list that satisfy the provided function, with a maximum count for each occurrence.
Example ¶
l := ArrayList{1, 2, 3, 4, 5, 2} condition := func(val any) bool { return val.(int) > 2 } fmt.Println(l.Finds(condition)) fmt.Println(l.Finds(condition, 2))
Output: [3 4 5] [3 4]
func (*ArrayList) ForEach ¶
ForEach applies the specified action to each element of the list
Example ¶
list := ArrayList{1, 2, 3, 4, 5} fmt.Println(list) list.ForEach(func(item any) any { return item.(int) * 2 }) fmt.Println(list)
Output: [1 2 3 4 5] [2 4 6 8 10]
func (ArrayList) Head ¶
Head returns the first element of the list, or an error if the list is empty.
Example ¶
list := ArrayList{1, 2, 3, 4, 5} fmt.Println(list.Head()) fmt.Println(ArrayList{}.Head())
Output: 1 <nil> <nil> the input list is empty
func (ArrayList) Includes ¶
Includes checks whether the array list includes a specific element.
Example ¶
al := ArrayList{1, 2, 3, 4, 5} fmt.Println(al.Includes(3)) fmt.Println(al.Includes(6))
Output: true false
func (ArrayList) IndexOf ¶
IndexOf returns the index of the first occurrence of the specified element in the list, or -1 if the element is not found.
Example ¶
list := ArrayList{1, 2, 3, 4, 5} fmt.Println(list.IndexOf(3)) fmt.Println(list.IndexOf(6))
Output: 2 -1
func (*ArrayList) Insert ¶
Insert inserts the specified element at the specified index
Example ¶
list := ArrayList{1, 2, 3} fmt.Println(list) list.Insert(1, 5) fmt.Println(list)
Output: [1 2 3] [1 5 2 3]
func (ArrayList) LastIndexOf ¶
LastIndexOf returns the index of the last occurrence of the specified element in the list, or -1 if the element is not found.
Example ¶
list := ArrayList{1, 2, 3, 4, 3} fmt.Println(list.IndexOf(3)) fmt.Println(list.LastIndexOf(3))
Output: 2 4
func (ArrayList) Len ¶
Len returns the length of the array list.
Example ¶
al := ArrayList{1, 2, 3, 4, 5} length := al.Len() fmt.Println(length)
Output: 5
func (ArrayList) Map ¶
Map applies the given handler function to each item in the list and returns a new list containing the results.
Example ¶
list := ArrayList{1, 2, 3, 4, 5} handler := func(val any) any { return val.(int) * 2 } newList := list.Map(handler) fmt.Println(newList) fmt.Println(list)
Output: [2 4 6 8 10] [1 2 3 4 5]
func (*ArrayList) Pop ¶
Pop removes the element at the specified index (or the last element if no index is provided) and returns it
Example ¶
l := ArrayList{1, 2, 3, 4, 5} fmt.Println(l) // Remove the last element element, _ := l.Pop() fmt.Println(element) fmt.Println(l) // Remove the element at a specific index element, _ = l.Pop(2) fmt.Println(element) fmt.Println(l)
Output: [1 2 3 4 5] 5 [1 2 3 4] 3 [1 2 4]
func (*ArrayList) Push ¶
Push appends elements to the end of the list and returns the new length
Example ¶
list := ArrayList{1, 2, 3} fmt.Println(list) list.Push(4, 5, 6) fmt.Println(list)
Output: [1 2 3] [1 2 3 4 5 6]
func (ArrayList) Reduce ¶
Reduce reduces the list to a single value by applying the handler function cumulatively to the items. It returns the accumulated result value and an error.
Example ¶
list := ArrayList{1, 2, 3, 4, 5} // Example 1: Summing up the elements of the list sum, _ := list.Reduce(func(a, b any) any { return a.(int) + b.(int) }) fmt.Println(sum) // Example 2: Finding the maximum element in the list max, _ := list.Reduce(func(a, b any) any { if a.(int) > b.(int) { return a } return b }, 0) fmt.Println(max) // Example 3: Summing up the elements of the list with initial value sum, _ = list.Reduce(func(a, b any) any { return a.(int) + b.(int) }, 10) fmt.Println(sum)
Output: 15 5 25
func (ArrayList) ReduceRight ¶
ReduceRight reduces the list to a single value by applying the handler function cumulatively from right to left. It returns the accumulated result value and an error.
Example ¶
list := ArrayList{1, 2, 3, 4, 5} f := func(a, b any) any { return a.(int) - b.(int) } result1, _ := list.Reduce(f) fmt.Println(result1) result2, _ := list.ReduceRight(f) fmt.Println(result2)
Output: -13 -5
func (*ArrayList) Remove ¶
Remove removes the specified number of occurrences of the element from the list
Example ¶
l := ArrayList{1, 2, 2, 3, 4, 4, 4, 5, 5, 5, 5, 5} fmt.Println(l) l.Remove(2) fmt.Println(l) l.Remove(4, -1) fmt.Println(l) l.Remove(5, 2) fmt.Println(l)
Output: [1 2 2 3 4 4 4 5 5 5 5 5] [1 2 3 4 4 4 5 5 5 5 5] [1 2 3 5 5 5 5 5] [1 2 3 5 5 5]
func (*ArrayList) RemoveIf ¶
RemoveIf removes elements from the list that satisfy the specified condition
Example ¶
list := ArrayList{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} evenCondition := func(val any) bool { num := val.(int) return num%2 == 0 } fmt.Println(list) removed := list.RemoveIf(evenCondition, -1) fmt.Println(list) fmt.Println(removed)
Output: [1 2 3 4 5 6 7 8 9 10] [1 3 5 7 9] [2 4 6 8 10]
func (*ArrayList) RemoveRight ¶
RemoveRight removes the specified number of occurrences of the element from the end of the list
Example ¶
l := ArrayList{1, 2, 3, 4, 5, 4, 3, 2, 1} l2 := l.Copy() fmt.Println(l) l.Remove(4) l2.RemoveRight(4) fmt.Println(l) fmt.Println(l2)
Output: [1 2 3 4 5 4 3 2 1] [1 2 3 5 4 3 2 1] [1 2 3 4 5 3 2 1]
func (*ArrayList) RemoveRightIf ¶
RemoveRightIf removes elements from the end of the list that satisfy the specified condition
Example ¶
list := ArrayList{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} list2 := list.Copy() condition := func(val any) bool { return val.(int)%2 == 0 } fmt.Println(list) removed1 := list.RemoveRightIf(condition, 3) removed2 := list2.RemoveIf(condition, 3) fmt.Println(list) fmt.Println(removed1) fmt.Println(list2) fmt.Println(removed2)
Output: [1 2 3 4 5 6 7 8 9 10] [1 2 3 4 5 7 9] [10 8 6] [1 3 5 7 8 9 10] [2 4 6]
func (*ArrayList) Replace ¶
Replace replaces the specified number of occurrences of the old element with the new element
Example ¶
l := ArrayList{1, 2, 2, 3, 4, 4, 4, 5, 5, 5, 5, 5} fmt.Println(l) l.Replace(2, -2) fmt.Println(l) l.Replace(4, -4, -1) fmt.Println(l) l.Replace(5, -5, 2) fmt.Println(l)
Output: [1 2 2 3 4 4 4 5 5 5 5 5] [1 -2 2 3 4 4 4 5 5 5 5 5] [1 -2 2 3 -4 -4 -4 5 5 5 5 5] [1 -2 2 3 -4 -4 -4 -5 -5 5 5 5]
func (*ArrayList) ReplaceIf ¶
ReplaceIf replaces elements in the list that satisfy the specified condition with the new element
Example ¶
list := ArrayList{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} evenCondition := func(val any) bool { num := val.(int) return num%2 == 0 } fmt.Println(list) replaced := list.ReplaceIf(evenCondition, -1, -1) fmt.Println(list) fmt.Println(replaced)
Output: [1 2 3 4 5 6 7 8 9 10] [1 -1 3 -1 5 -1 7 -1 9 -1] [2 4 6 8 10]
func (*ArrayList) ReplaceRight ¶
ReplaceRight replaces the specified number of occurrences of the old element with the new element, starting from the end of the list
Example ¶
l := ArrayList{1, 2, 3, 4, 5, 4, 3, 2, 1} l2 := l.Copy() fmt.Println(l) l.Replace(4, 0) l2.ReplaceRight(4, 0) fmt.Println(l) fmt.Println(l2)
Output: [1 2 3 4 5 4 3 2 1] [1 2 3 0 5 4 3 2 1] [1 2 3 4 5 0 3 2 1]
func (*ArrayList) ReplaceRightIf ¶
func (l *ArrayList) ReplaceRightIf(condition func(any) bool, newElement any, counts ...int) ArrayList
ReplaceRightIf replaces elements in the list, starting from the end, that satisfy the specified condition with the new element
Example ¶
list := ArrayList{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} list2 := list.Copy() condition := func(val any) bool { return val.(int)%2 == 0 } fmt.Println(list) replaced1 := list.ReplaceRightIf(condition, 0, 3) replaced2 := list2.ReplaceIf(condition, 0, 3) fmt.Println(list) fmt.Println(replaced1) fmt.Println(list2) fmt.Println(replaced2)
Output: [1 2 3 4 5 6 7 8 9 10] [1 2 3 4 5 0 7 0 9 0] [10 8 6] [1 0 3 0 5 0 7 8 9 10] [2 4 6]
func (*ArrayList) Reverse ¶
Reverse reverses the order of elements in the list
Example ¶
l := ArrayList{1, 2, 3, 4, 5} fmt.Println(l) l.Reverse() fmt.Println(l)
Output: [1 2 3 4 5] [5 4 3 2 1]
func (*ArrayList) Set ¶
Set sets the element at the specified index to the provided value
Example ¶
list := ArrayList{1, 2, 3} fmt.Println(list) _ = list.Set(2, 6) fmt.Println(list) _ = list.Set(-2, 5) fmt.Println(list) err := list.Set(5, 6) fmt.Println(err) fmt.Println(list)
Output: [1 2 3] [1 2 6] [1 5 6] the index is out of range [1 5 6]
func (*ArrayList) Shift ¶
Shift removes the first element from the list and returns it
Example ¶
list := ArrayList{1, 2, 3, 4, 5} fmt.Println(list) element, _ := list.Shift() fmt.Println(element) fmt.Println(list)
Output: [1 2 3 4 5] 1 [2 3 4 5]
func (ArrayList) Slice ¶
Slice returns a new ArrayList containing elements from the original list based on the specified slice parameters.
Example ¶
list := ArrayList{1, 2, 3, 4, 5} slice1 := list.Slice(1, 4) fmt.Println(slice1) slice2 := list.Slice(0, 3, 2) fmt.Println(slice2) slice3 := list.Slice(3, 0, -1) fmt.Println(slice3)
Output: [2 3 4] [1 3] [4 3 2]
func (ArrayList) Some ¶
Some checks if at least one item in the list satisfies the given condition function.
Example ¶
list := ArrayList{1, 2, 3, 4, 5} result := list.Some(func(i any) bool { return i.(int) > 3 }) fmt.Println(result) result = list.Some(func(i any) bool { return i.(int) > 5 }) fmt.Println(result)
Output: true false
func (*ArrayList) Splice ¶
Splice removes a section of the list and replaces it with the specified items
Example ¶
arr := ArrayList{1, 2, 3, 4, 5} fmt.Println(arr) arr.Splice(2, 2, 6, 7, 8) fmt.Println(arr)
Output: [1 2 3 4 5] [1 2 6 7 8 5]
func (ArrayList) Tail ¶
Tail returns the last element of the list, or an error if the list is empty.
Example ¶
list := ArrayList{1, 2, 3, 4, 5} fmt.Println(list.Tail()) fmt.Println(ArrayList{}.Tail())
Output: 5 <nil> <nil> the input list is empty
func (ArrayList) ToReversed ¶
ToReversed returns a new list with elements in reverse order compared to the original list.
Example ¶
l := ArrayList{1, 2, 3, 4, 5} reversed := l.ToReversed() fmt.Println(reversed) fmt.Println(l)
Output: [5 4 3 2 1] [1 2 3 4 5]
func (ArrayList) ToSpliced ¶
ToSpliced returns a new list after performing the splice operation on the original list and returns the modified list.
Example ¶
arr := ArrayList{1, 2, 3, 4, 5} newArr := arr.ToSpliced(2, 2, 6, 7, 8) fmt.Println(newArr) fmt.Println(arr)
Output: [1 2 6 7 8 5] [1 2 3 4 5]
func (*ArrayList) Unshift ¶
Unshift prepends elements to the beginning of the list and returns the new length
Example ¶
l := ArrayList{1, 2, 3} fmt.Println(l) l.Unshift(4, 5) fmt.Println(l)
Output: [1 2 3] [4 5 1 2 3]