A customized go list with index, sort, append, pop, count, clear and last item methods.
It supports all of the following data structures although the examples below are mostly int
:
int
int32
int64
float32
float64
string
- Import
import (
"github.com/emylincon/golist"
)
- Download
go get github.com/emylincon/golist
Here are all of the methods of the list objects:
- Download and install pre-commit:
pip install pre-commit
- Install precommit hook in repo:
pre-commit install
- before push run fmt:
gofmt -w .
Get an item in the list by index. i
represents the index. Returns nil
if index don't exist.
list := golist.NewList([]int{1,2,3})
item := list.Get(0)
fmt.Println(item) // 1
Get an item's index in the list. works in reverse of list.Get(i)
. x
represents the item. Returns -1
if item don't exist.
list := golist.NewList([]int{1,2,3})
index := list.Index(2)
fmt.Println(index) // 1
Returns a string representation of the object
list := golist.NewList([]int{3,2,1})
fmt.Println(list.String()) // [3, 2, 1]
Add an item to the end of the list. Items must be of the same type.
list := golist.NewList([]int{1,2,3})
list.Append(7)
fmt.Println(list) // [1, 2, 3, 7]
Extend the list by appending all the items from a slice or array.
list := golist.NewList([]int{1,2,3})
list.Extend([]int{4,5})
fmt.Println(list) // [1, 2, 3, 4, 5]
Insert an item at a given position. The first argument is the element while the second is the index to insert the element, so list.insert(x, 0)
inserts at the front of the list, and list.Insert(x, len(a))
is equivalent to list.Append(x)
. Returns error is any
list := golist.NewList([]int{1, 2, 3})
err := list.Insert(4, 3)
if err != nil {
fmt.Println(err) // handle error
}
fmt.Println(list) // [1, 2, 3, 4]
- The above inserts item 4 to position 3 which is the end of the list
Remove the first item from the list whose value is equal to x. It raises a ValueError if there is no such item.
list := golist.NewList([]int{1, 2, 3})
err := list.Remove(2)
if err != nil {
fmt.Println(err) // handle error
}
fmt.Println(list) // [1, 3]
Remove the item at the given position in the list, and return it. i
is the index of the element to be popped.
list := golist.NewList([]int{1, 2, 3})
popped := list.Pop(0)
fmt.Println(popped) // 1
fmt.Println(list) // [2, 3]
Remove all items from the list.
list := golist.NewList([]int{1, 2, 3})
list.Clear()
fmt.Println(list) // []
The arguments start and end are interpreted as in the slice notation and are used to return a particular subsequence of the list. The returned index is computed relative to the beginning of the full sequence rather than the start argument.
list := golist.NewList([]int{1, 2, 3, 2})
start, stop := 0, 2
NewList, err := list.Slice(start, stop)
if err != nil {
fmt.Println(err) // handle error
}
fmt.Println(NewList) // [1, 2]
Return the number of times x appears in the list. Returns -1 if element not found or the given element type does not match the type of list
list := golist.NewList([]int{1, 2, 3, 2})
count := list.Count(2)
fmt.Println(count) // 2
Sort the items of the list in place (the argument can be used for sort customization. reverse
is bool
so can be true
or false
.
list := golist.NewList([]int{3, 2, 1})
reverse := false
list.Sort(reverse)
fmt.Println(list) // [1, 2, 3]
Returns a list of Sorted items (the argument can be used for sort customization. reverse
is bool
so can be true
or false
).
list := golist.NewList([]int{3, 2, 1})
reverse := false
Newlist := list.Sorted(reverse)
fmt.Println(Newlist) // [1, 2, 3]
Returns new list with the elements of the list reversed.
list := golist.NewList([]int{5, 2, 7, 1})
newList := list.Reverse()
fmt.Println(newList) // [1, 7, 2, 5]
Return a shallow copy of the list.
list := golist.NewList([]int{3, 2, 1})
copy, err := list.Copy()
if err != nil {
fmt.Println(err) // handle error
}
fmt.Println(copy) // [3, 2, 1]
Returns sum of the elements in the list. If it is list of string, it joins the strings with a space and returns it
list := golist.NewList([]int{3, 2, 1})
fmt.Println(list.Sum()) // 6
list := golist.NewList([]string{"Hello", "World"})
fmt.Println(list.Sum()) // "Hello World"
This is a getter that returns underlying slice interface.
list := golist.NewList([]int{3, 2, 1})
fmt.Println(list.List()) // [3 2 1]
This only works with string data types, panics otherwise. joiner
is a string used to join the list.
list := golist.NewList([]string{"Hello", "World"})
fmt.Println(list.Join("-")) // "Hello-World"
Replaces an element at index i with element x. returns error if index does not exist. index of -1
is equivalent to last item. This method is equivalent to working with slice (a
) a[1] = 10
list := golist.NewList([]string{"Hello", "World"})
err := list.Replace("golang", -1)
if err != nil {
fmt.Println(err) // handle error
}
fmt.Println(list) // ["Hello", "golang"]
Returns max item in list. returns err if list is empty
list := golist.NewList([]string{"Hello", "World"})
max, err := list.Max()
if err != nil {
fmt.Println(err) // handles error
}
fmt.Println(max) // "World"
list := golist.NewList([]int{3, 2, 1})
max, err := list.Max()
if err != nil {
fmt.Println(err) // handles error
}
fmt.Println(max) // 3
Returns min item in list. returns err if list is empty
list := golist.NewList([]string{"Hello", "World"})
min, err := list.Min()
if err != nil {
fmt.Println(err) // handles error
}
fmt.Println(min) // "Hello"
list := golist.NewList([]int{3, 2, 1})
min, err := list.Min()
if err != nil {
fmt.Println(err) // handles error
}
fmt.Println(min) // 1
list := golist.NewList([]int{3, 2, 1})
for i := 0; i < list.Len(); i++ {
fmt.Println(list.Get(i))
}
Output
3
2
1
Returns the Greatest Common Factor (GCF) or Highest Common Factor (HCF) of the numbers in the list. Only works with numbers. Returns error if called on list of strings. Uses Euclidean algorithm
list := golist.NewList([]int{10, 15, 5})
gcf, err := list.GCF()
if err != nil {
fmt.Println(err) // handle error
}
fmt.Println(gcf) // 5
Returns the Least Common Multiple (LCM) of the numbers in the list. Only works with numbers. Returns error if called on list of strings. Uses Euclidean algorithm
list := golist.NewList([]int{10, 15, 5})
lcm, err := list.LCM()
if err != nil {
fmt.Println(err) // handle error
}
fmt.Println(lcm) // 30
returns the type of list
list := golist.NewList([]string{"Hello", "World"})
fmt.Println(list.Type()) // golist.List[]string
Returns a random element from list.
list := golist.NewList([]string{"Hello", "World"})
fmt.Println(list.Rand()) // World
returns true if element exists, returns false otherwise
list := golist.NewList([]string{"Hello", "World"})
fmt.Println(list.Contains("okay")) // false
This is adapted from Link. joiner
is a string used to join the strings. Combinations returns combinations of n number of elements for a given string array.e.g if n=2
it will return only 2 combined elements.
Furthermore NewList([]string{"a", "b", "c"}).Combinations(2, "") = ["ab", "ac", "bc"]
.
- For
n < 1
, it equals to All and returns all combinations. - For
n > len(list)
thenn = len(list)
list := golist.NewList([]string{"a", "b", "c"})
combinedList := list.Combinations(2, " ")
fmt.Println(combinedList) // ["a b", "a c", "b c"]
combinedList = list.Combinations(2, ",")
fmt.Println(combinedList) // ["a,b", "a,c", "b,c"] <nil>
Variation of list.Combinations
. Difference is that for a given n
, it returns combination lengths <= n, rather than only n.
list:= golist.NewList([]string{"a", "b", "c"})
fmt.Println(list.CombinationsMax(2, "")) // ["a", "b", "ab", "c", "ac", "bc"] <nil>
returns true if both lists are equal, returns false otherwise
list := golist.NewList([]string{"Hello", "World"})
fmt.Println(list.IsEqual([]string{"a", "b"})) // false
returns a new list with duplicates removed
list := golist.NewList([]int{1, 1, 1, 2, 3, 3, 4, 5, 6, 6})
fmt.Println(list.Set()) // [1, 2, 3, 4, 5, 6]
Adds two list together and returns a new list which is the result of the addition.
list := golist.NewList([]int{1, 0, 1})
other := golist.NewList([]int{0, 2, 0})
newList, err := list.Add(other)
if err != nil {
log.Println(err) // handle error
}
fmt.Println(newList) // [1, 0, 1, 0, 2, 0]
Add the content of two lists. The lists must be of the same type and have equal length. Example:
list1 := golist.NewList([]int{1,1})
list2 := golist.NewList([]int{2,2})
list3 := list1.ListSum(list2)
fmt.Println(list3) // [3,3]
Add number to all elements in list. Example
list1 := golist.NewList([]int{1,1})
no := 2
list3 := list1.ListSumNo(no)
fmt.Println(list3) // [3,3]
converts list from type a
to type b
. Example
list := golist.NewList([]int{1,1})
fmt.Println(list.Type()) // golist.List[]int
list.ConvertTo(golist.TypeListInt32)
fmt.Println(list.Type()) // golist.List[]int32
Subtract the content of two lists. The lists must be of the same type and have equal length. Example:
list1 := golist.NewList([]int{1,1})
list2 := golist.NewList([]int{2,2})
list3, err := list1.ListSubtract(list2)
if err != nil {
fmt.Println(err) // handle error
}
fmt.Println(list3) // [-1, -1]
Subtract number from all elements in list. Example
list1 := golist.NewList([]int{1,1})
var no int = 2
list3, err := list1.ListSubtractNo(no)
if err != nil {
fmt.Println(err) // handle error
}
fmt.Println(list3) // [-1, -1]
Multiply the content of two lists. The lists must be of the same type and have equal length. Example:
list1 := golist.NewList([]int{1,1})
list2 := golist.NewList([]int{2,2})
list3, err := list1.ListMultiply(list2)
if err != nil {
fmt.Println(err) // handle error
}
fmt.Println(list3) // [2, 2]
Multiply a number with all elements in list. Example
list1 := golist.NewList([]int{1,1})
var no int = 2
list3, err := list1.ListMultiplyNo(no)
if err != nil {
fmt.Println(err) // handle error
}
fmt.Println(list3) // [2, 2]
Divide the content of two lists. The lists must be of the same type and have equal length. Example:
list1 := golist.NewList([]int{8,6})
list2 := golist.NewList([]int{2,2})
list3, err := list1.ListDivide(list2)
if err != nil {
fmt.Println(err) // handle error
}
fmt.Println(list3) // [4, 3]
Divide all elements in list with no. Example
list1 := golist.NewList([]int{12,2})
var no int = 2
list3, err := list1.ListDivideNo(no)
if err != nil {
fmt.Println(err) // handle error
}
fmt.Println(list3) // [6, 1]
Converts golist to []float32. Example
list := golist.NewList([]int{12,2})
slice, err := list.ConvertToSliceFloat32()
if err != nil {
fmt.Println(err) // handle error
}
fmt.Printf("%T", slice) // []float32
Converts golist to []float64. Example
list := golist.NewList([]int{12,2})
slice, err := list.ConvertToSliceFloat64()
if err != nil {
fmt.Println(err) // handle error
}
fmt.Printf("%T", slice) // []float64
Converts golist to []int64. Example
list := golist.NewList([]int{12,2})
slice, err := list.ConvertToSliceInt64()
if err != nil {
fmt.Println(err) // handle error
}
fmt.Printf("%T", slice) // []int64
Converts golist to []int32. Example
list := golist.NewList([]int{12,2})
slice, err := list.ConvertToSliceInt32()
if err != nil {
fmt.Println(err) // handle error
}
fmt.Printf("%T", slice) // []int32
Converts golist to []int. Example
list := golist.NewList([]int{12,2})
slice, err := list.ConvertToSliceInt()
if err != nil {
fmt.Println(err) // handle error
}
fmt.Printf("%T", slice) // []int
Converts golist to []string. Example
list := golist.NewList([]int{12,2})
slice, err := list.ConvertToSliceString()
if err != nil {
fmt.Println(err) // handle error
}
fmt.Printf("%T", slice) // []string
Difference returns the elements in list
that aren't in other
. Example
list := golist.NewList([]int{1,2,3,4})
other := golist.NewList([]int{3,4,5})
diff, err := list.Difference(other)
if err != nil {
fmt.Println(err) // handle error
}
fmt.Println(diff) // [1, 2]
DifferenceBoth returns the elements that aren't in both lists. Example
list := golist.NewList([]int{1,2,3,4})
other := golist.NewList([]int{3,4,5})
diff, err := list.DifferenceBoth(other)
if err != nil {
fmt.Println(err) // handle error
}
fmt.Println(diff) // [1, 2, 5]