arraylist

package
v0.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 3, 2024 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package arraylist provides a resizable array implementation.

Index

Examples

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

func Of(elements ...any) ArrayList

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

func Repeat(element any, count int) ArrayList

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

func (l ArrayList) At(index int) (element any, err error)

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

func (l *ArrayList) Clear() *ArrayList

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

func (l ArrayList) Concat(another ArrayList) ArrayList

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

func (l ArrayList) Copy() ArrayList

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

func (l ArrayList) Count(element any) (count int)

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

func (l ArrayList) Empty() bool

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

func (l ArrayList) Equal(another ArrayList) bool

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

func (l ArrayList) Every(condition func(any) bool) bool

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

func (l *ArrayList) Fill(element any, area ...int) *ArrayList

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

func (l ArrayList) Filter(condition func(any) bool) ArrayList

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

func (l ArrayList) Find(by func(any) bool) (element any, found bool)

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

func (l ArrayList) FindIndex(by func(any) bool) (index int)

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

func (l ArrayList) FindIndexes(by func(any) bool, counts ...int) (indexes []int)

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

func (l ArrayList) FindLast(by func(any) bool) (element any, found bool)

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

func (l ArrayList) FindLastIndex(by func(any) bool) (index int)

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

func (l ArrayList) FindLastIndexes(by func(any) bool, counts ...int) (indexes []int)

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

func (l ArrayList) FindLasts(by func(any) bool, counts ...int) (elements []any)

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

func (l ArrayList) Finds(by func(any) bool, counts ...int) (elements []any)

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

func (l *ArrayList) ForEach(action func(any) any) *ArrayList

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

func (l ArrayList) Head() (element any, err error)

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

func (l ArrayList) Includes(element any) bool

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

func (l ArrayList) IndexOf(element any) (index int)

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

func (l *ArrayList) Insert(index int, element any) *ArrayList

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

func (l ArrayList) LastIndexOf(element any) (index int)

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

func (l ArrayList) Len() int

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

func (l ArrayList) Map(handler func(any) any) ArrayList

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

func (l *ArrayList) Pop(indexes ...int) (element any, err error)

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

func (l *ArrayList) Push(elements ...any) (length int)

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

func (l ArrayList) Reduce(handler func(any, any) any, initial ...any) (result any, err error)

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

func (l ArrayList) ReduceRight(handler func(any, any) any, initial ...any) (result any, err error)

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

func (l *ArrayList) Remove(element any, counts ...int) *ArrayList

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

func (l *ArrayList) RemoveIf(condition func(any) bool, counts ...int) ArrayList

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

func (l *ArrayList) RemoveRight(element any, counts ...int) *ArrayList

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

func (l *ArrayList) RemoveRightIf(condition func(any) bool, counts ...int) ArrayList

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

func (l *ArrayList) Replace(oldElement, newElement any, counts ...int) *ArrayList

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

func (l *ArrayList) ReplaceIf(condition func(any) bool, newElement any, counts ...int) ArrayList

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

func (l *ArrayList) ReplaceRight(oldElement, newElement any, counts ...int) *ArrayList

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

func (l *ArrayList) Reverse() *ArrayList

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

func (l *ArrayList) Set(index int, element any) (err error)

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

func (l *ArrayList) Shift() (element any, err error)

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

func (l ArrayList) Slice(args ...int) ArrayList

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

func (l ArrayList) Some(condition func(any) bool) bool

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

func (l *ArrayList) Splice(start, deleteCount int, items ...any) ArrayList

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

func (l ArrayList) Tail() (element any, err error)

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

func (l ArrayList) ToReversed() ArrayList

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

func (l ArrayList) ToSpliced(start, deleteCount int, items ...any) ArrayList

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

func (l *ArrayList) Unshift(elements ...any) (length int)

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]

func (ArrayList) With

func (l ArrayList) With(index int, element any) ArrayList

With returns a new list after setting the specified index with the provided element in the original list.

Example
list := ArrayList{1, 2, 3, 4, 5}
newList := list.With(2, 10)
fmt.Println(newList)
fmt.Println(list)
Output:

[1 2 10 4 5]
[1 2 3 4 5]

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL