golist

package module
v1.4.5 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2022 License: MIT Imports: 6 Imported by: 1

README

golist

Build Status Status GoDoc Go Report Card

golist

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

To use module

  • Import
import (
    "github.com/emylincon/golist"
)
  • Download
go get github.com/emylincon/golist

Here are all of the methods of the list objects:

To Contribute

  • Download and install pre-commit: pip install pre-commit
  • Install precommit hook in repo: pre-commit install
  • before push run fmt: gofmt -w .

list.Get(i int) interface{}

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

list.Index(x interface{}) int

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

list.String()

Returns a string representation of the object

list := golist.NewList([]int{3,2,1})
fmt.Println(list.String())  // [3, 2, 1]

list.Append(x interface{})

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]

list.Extend(slice interface{})

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]

list.Insert(x interface{}, i int) error

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

list.Remove(x interface{}) error

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]

list.Pop(i int) interface{}

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]

list.Clear()

Remove all items from the list.

list := golist.NewList([]int{1, 2, 3})
list.Clear()
fmt.Println(list) // []

list.Slice(start int, end int) (*golist.List, error)

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]

list.Count(x interface{}) int

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

list.Sort(reverse bool)

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]

list.Sorted(reverse bool) *golist.List

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]

list.Reverse() *golist.List

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]

list.Copy() (*golist.List, error)

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]

list.Sum() interface{}

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"

list.List() interface{}

This is a getter that returns underlying slice interface.

list := golist.NewList([]int{3, 2, 1})
fmt.Println(list.List())  // [3 2 1]

list.Join(joiner string) string

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"

list.Replace(x interface{}, i int) error

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"]

list.Max() (interface{}, error)

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

list.Min() (interface{}, error)

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

Loop through List

list := golist.NewList([]int{3, 2, 1})
for i := 0; i < list.Len(); i++ {
    fmt.Println(list.Get(i))
}

Output

3
2
1

list.GCF() (interface{}, error)

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

list.LCM() (interface{}, error)

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

list.Type() string

returns the type of list

list := golist.NewList([]string{"Hello", "World"})
fmt.Println(list.Type())  // golist.List[]string

list.Rand() interface{}

Returns a random element from list.

list := golist.NewList([]string{"Hello", "World"})
fmt.Println(list.Rand())  // World

list.Contains(element interface{}) bool

returns true if element exists, returns false otherwise

list := golist.NewList([]string{"Hello", "World"})
fmt.Println(list.Contains("okay"))  // false

list.Combinations(n int, joiner string) (*golist.List, error)

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) then n = 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>

list.CombinationsMax(n int, joiner string) (*golist.List, error)

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>

list.IsEqual(other *golist.List) bool

returns true if both lists are equal, returns false otherwise

list := golist.NewList([]string{"Hello", "World"})
fmt.Println(list.IsEqual([]string{"a", "b"}))  // false

list.Set() (*golist.List, err)

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]

list.Add(other *golist.List) (golist.List, err)

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]

list.ListSum(other *golist.List) (*golist.List, err)

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]

list.ListSumNo(no interface{}) (*golist.List, err)

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]

list.ConvertTo(t golist.ListType) (*golist.List, err)

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

list.ListSubtract(other *golist.List) (*golist.List, err)

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]

list.ListSubtractNo(no interface{}) (*golist.List, err)

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]

list.ListMultiply(other *golist.List) (*golist.List, err)

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]

list.ListMultiplyNo(no interface{}) (*golist.List, err)

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]

list.ListDivide(other *golist.List) (*golist.List, err)

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]

list.ListDivideNo(no interface{}) (*golist.List, err)

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]

list.ConvertToSliceFloat32() ([]float32, error)

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

list.ConvertToSliceFloat64() ([]float64, error)

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

list.ConvertToSliceInt64() ([]int64, error)

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

list.ConvertToSliceInt32() ([]int32, error)

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

list.ConvertToSliceInt() ([]int, error)

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

list.ConvertToSliceString() ([]string, error)

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

list.Difference(other *List) (*List, error)

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]

list.DifferenceBoth(other *List) (*List, error)

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]

Documentation

Overview

Example
package main

import (
	"fmt"

	"github.com/emylincon/golist"
)

func main() {
	list := golist.NewList([]int{1, 2, 3})
	// Get an item in the list by index. `i` represents the index. Returns `nil` if index don't exist.
	item := list.Get(0)
	fmt.Println("Get(0) :", item)

	// To return an item's index in the list, use list.index. works in reverse of `list.Get(i)`. `x` represents the item. Returns `-1` if item don't exist.
	index := list.Index(2)
	fmt.Println("Index(2) :", index)

	// Append an item to the end of the list. Items must be of the same type.
	list.Append(7)
	fmt.Println("Append(7) :", list)

	// Extend the list by appending all the items from a slice or array.
	list.Extend([]int{4, 5})
	fmt.Println("Extend([]int{4, 5) :", list)

	// Reverse elements in list.
	fmt.Println("Reverse() :", list.Reverse())

	// sum elements in list
	fmt.Println("Sum() :", list.Sum())

	// remove a given element from list
	err := list.Remove(7)
	if err != nil {
		fmt.Println(err) // handle error
	}
	fmt.Println("Remove(7) :", list)

	// Subtract the content of two lists.
	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("ListSubtract :", list3)

	// Multiply all elements in list with no. 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("ListMultiplyNo :", list3)

	// Divide all elements with other list. 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("ListDivide :", list3)

	// convert list to slice of strings
	slice, err := list.ConvertToSliceString()
	if err != nil {
		fmt.Println(err) // handle error
	}
	fmt.Printf("ConvertToSliceString: %T\n", slice)

	list = golist.NewList([]int{1, 2, 3, 4})
	other := golist.NewList([]int{3, 4})
	diff, err := list.Difference(other)
	if err != nil {
		fmt.Println(err) // handle error
	}
	fmt.Printf("Difference: %v\n", diff)

}
Output:

Get(0) : 1
Index(2) : 1
Append(7) : [1, 2, 3, 7]
Extend([]int{4, 5) : [1, 2, 3, 7, 4, 5]
Reverse() : [5, 4, 7, 3, 2, 1]
Sum() : 22
Remove(7) : [1, 2, 3, 4, 5]
ListSubtract : [-1, -1]
ListMultiplyNo : [2, 2]
ListDivide : [4, 3]
ConvertToSliceString: []string
Difference: [1, 2]

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrTypeNotsupported    = errors.New("type error: interface type should be []float32, []float64, []int32, []int, []int64, or []string")
	ErrStringsNotsupported = errors.New("operation error: strings are not supported for this operation")
	ErrIndexOutOfRange     = errors.New("index error: list index out of range")
	ErrListsNotOfSameType  = errors.New("type error: lists not of same type")
	ErrListsNotOfSameLen   = errors.New("length error: lists not of same length")
	ErrTypeNotSame         = errors.New("type error: list type and no type are not same")
)

Error variables used within the module

Functions

This section is empty.

Types

type List

type List struct {
	// contains filtered or unexported fields
}

List struct

func NewList

func NewList(list interface{}) *List

NewList : list constructor

Example
package main

import (
	"fmt"

	"github.com/emylincon/golist"
)

func main() {
	// create new list object
	list := golist.NewList([]int{1, 2, 3})
	fmt.Println(list)
}
Output:

[1, 2, 3]

func (*List) Add added in v1.2.0

func (arr *List) Add(other *List) (newList *List, err error)

Add : adds two list together and returns new list and possible error

func (*List) Append

func (arr *List) Append(element interface{}) error

Append appends an element to list

func (*List) Clear

func (arr *List) Clear()

Clear : removes all element in list

func (*List) Combinations added in v1.1.6

func (arr *List) Combinations(n int, joiner string) (*List, error)

Combinations returns Combinations of n number of elements for a given string array. adapted from https://github.com/mxschmitt/golang-combinations 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); n = len(list)

func (*List) CombinationsMax added in v1.3.0

func (arr *List) CombinationsMax(n int, joiner string) (*List, error)

CombinationsMax returns combinations of n number of elements for a given string array. adapted from https://github.com/mxschmitt/golang-combinations e.g if n=2 it will return combinations <= 2 furthermore NewList([]string{"a", "b", "c"}).CombinationsMax(2, "") = ["a", "b", "c", "ab", "ac", "bc"] For n < 1, it equals to All and returns all combinations. for n > len(list); n = len(list)

func (*List) Contains added in v1.1.6

func (arr *List) Contains(element interface{}) bool

Contains : returns true if element exists, returns false otherwise ```golang list := golist.NewList([]string{"Hello", "World"}) fmt.Println(list.Contains("okay")) // false ```

func (*List) ConvertTo added in v1.4.0

func (arr *List) ConvertTo(t ListType) (*List, error)

ConvertTo converts list to a new type

func (*List) ConvertToSliceFloat32 added in v1.4.4

func (arr *List) ConvertToSliceFloat32() ([]float32, error)

ConvertToSliceFloat32 converts golist to []float32

func (*List) ConvertToSliceFloat64 added in v1.4.4

func (arr *List) ConvertToSliceFloat64() ([]float64, error)

ConvertToSliceFloat64 converts golist to []float64

func (*List) ConvertToSliceInt added in v1.4.4

func (arr *List) ConvertToSliceInt() ([]int, error)

ConvertToSliceInt converts golist to []int

func (*List) ConvertToSliceInt32 added in v1.4.4

func (arr *List) ConvertToSliceInt32() ([]int32, error)

ConvertToSliceInt32 converts golist to []int32

func (*List) ConvertToSliceInt64 added in v1.4.4

func (arr *List) ConvertToSliceInt64() ([]int64, error)

ConvertToSliceInt64 converts golist to []int64

func (*List) ConvertToSliceString added in v1.4.4

func (arr *List) ConvertToSliceString() ([]string, error)

ConvertToSliceString converts golist to []string

func (*List) Copy

func (arr *List) Copy() (*List, error)

Copy : returns a Copy of the list

func (*List) Count

func (arr *List) Count(element interface{}) int

Count returns a count of how many times a given element appears in the list Returns -1 if element not found or the given element type does not match the type of list

func (*List) Difference added in v1.4.4

func (arr *List) Difference(other *List) (*List, error)

Difference returns the elements in `list` that aren't in `other`.

func (*List) DifferenceBoth added in v1.4.4

func (arr *List) DifferenceBoth(other *List) (*List, error)

DifferenceBoth returns the elements that aren't in both lists.

func (*List) Extend

func (arr *List) Extend(other interface{}) error

Extend : This extends current list with input slice. errors if both underlying lists are not of the same type

func (*List) GCF added in v1.1.5

func (arr *List) GCF() (gcf interface{}, err error)

GCF : returns greatest common factor of the list. returns error if type of list is string or any of the unsupported types

func (*List) Get

func (arr *List) Get(index int) interface{}

Get : returns element from list using index. if index dont exist returns nil

func (*List) HCF added in v1.1.5

func (arr *List) HCF() (gcf interface{}, err error)

HCF : returns greatest common factor of the list. returns error if type of list is string or any of the unsupported types

func (*List) Index

func (arr *List) Index(element interface{}) int

Index : returns index of a given element. returns -1 if element don't exist

func (*List) Insert

func (arr *List) Insert(element interface{}, index int) error

Insert : inserts an element at a given location. returns error if index is out of range

func (*List) IsEqual added in v1.1.6

func (arr *List) IsEqual(other *List) bool

IsEqual : checks if two lists are equal and returns a boolean

func (*List) Join added in v1.1.4

func (arr *List) Join(joiner string) string

Join : joins elements in a string list with a joiner

func (*List) LCM added in v1.1.5

func (arr *List) LCM() (lcm interface{}, err error)

LCM : returns Lowest common multiple of a list. returns error if list type is string

func (*List) Last

func (arr *List) Last() (interface{}, error)

Last : returns last element in the list

func (*List) Len

func (arr *List) Len() int

Len : returns length of list

func (*List) List added in v1.1.3

func (arr *List) List() interface{}

List : returns underlying slice or array

func (*List) ListDivide added in v1.4.0

func (arr *List) ListDivide(other *List) (*List, error)

ListDivide divides list with other list

func (*List) ListDivideNo added in v1.4.0

func (arr *List) ListDivideNo(no interface{}) (*List, error)

ListDivideNo Divide all elements in list with a number

func (*List) ListMultiply added in v1.4.0

func (arr *List) ListMultiply(other *List) (*List, error)

ListMultiply returns the product of contents of two lists

func (*List) ListMultiplyNo added in v1.4.0

func (arr *List) ListMultiplyNo(no interface{}) (*List, error)

ListMultiplyNo multiply a given number with all elements in list

func (*List) ListSubtract added in v1.4.0

func (arr *List) ListSubtract(other *List) (*List, error)

ListSubtract subtracts the contents of two lists

func (*List) ListSubtractNo added in v1.4.0

func (arr *List) ListSubtractNo(no interface{}) (*List, error)

ListSubtractNo subtracts a given number from all elements in list

func (*List) ListSum added in v1.4.0

func (arr *List) ListSum(other *List) (*List, error)

ListSum returns the sum of contents of two lists

func (*List) ListSumNo added in v1.4.0

func (arr *List) ListSumNo(no interface{}) (*List, error)

ListSumNo adds a given number to all elements in list

func (*List) Max added in v1.1.4

func (arr *List) Max() (interface{}, error)

Max : returns max element in list

func (*List) Min added in v1.1.4

func (arr *List) Min() (interface{}, error)

Min : returns min element in list

func (*List) Pop

func (arr *List) Pop(index int) interface{}

Pop : removes element in list using index and returns it

func (*List) Rand added in v1.1.6

func (arr *List) Rand() interface{}

Rand : ## list.Rand() interface{} Returns a random element from list.

```golang list := golist.NewList([]string{"Hello", "World"}) fmt.Println(list.Rand()) // World ```

func (*List) Remove

func (arr *List) Remove(element interface{}) error

Remove : removes a given element from list. returns error if element do not exist

func (*List) Replace added in v1.1.4

func (arr *List) Replace(element interface{}, index int) error

Replace : replaces an element in the lsit using the elements index. returns error if index do not exist.

func (*List) Reverse

func (arr *List) Reverse() *List

Reverse : reverse elements in the list

func (*List) Set added in v1.1.6

func (arr *List) Set() (setList *List, err error)

Set : removes duplicates from list and returns new list

func (*List) Slice

func (arr *List) Slice(start int, stop int) (*List, error)

Slice a list. similar to []int{1,2,3,4}[start:stop]

func (*List) Sort

func (arr *List) Sort(reverse bool)

Sort : sorts elements in the list in ascending order or descending order in place.

func (*List) Sorted added in v1.1.4

func (arr *List) Sorted(reverse bool) *List

Sorted : sorts elements in the list in ascending order or descending order and returns new list.

func (*List) String added in v1.1.2

func (arr *List) String() string

String : returns string representation of the list

func (*List) Sum added in v1.1.3

func (arr *List) Sum() interface{}

Sum : sums all element in the list

func (*List) Type added in v1.1.5

func (arr *List) Type() ListType

Type returns string representation of the list.

type ListType added in v1.4.0

type ListType string

ListType is a string representation of list type

const (
	TypeListInt     ListType = "golist.List[]int"
	TypeListInt32   ListType = "golist.List[]int32"
	TypeListInt64   ListType = "golist.List[]int64"
	TypeListFloat32 ListType = "golist.List[]float32"
	TypeListFloat64 ListType = "golist.List[]float64"
	TypeListString  ListType = "golist.List[]string"
	TypeListUnknown ListType = "golist.List[]unknown"
)

List types

type Lists added in v1.1.6

type Lists interface {
	Add(other *List) (*List, error)                      // add 2 lists
	Append(element interface{}) error                    // append item
	Clear()                                              // remove all elements in list
	Combinations(n int, joiner string) (*List, error)    // combinations of items in list
	CombinationsMax(n int, joiner string) (*List, error) // combinations of items in list
	Contains(element interface{}) bool                   // check if item in list
	ConvertTo(t ListType) (*List, error)                 // convert to another list type
	ConvertToSliceFloat32() ([]float32, error)           // ConvertToSliceFloat32 converts golist to []float32
	ConvertToSliceFloat64() ([]float64, error)           // ConvertToSliceFloat64 converts golist to []float64
	ConvertToSliceInt() ([]int, error)                   // ConvertToSliceInt converts golist to []int
	ConvertToSliceInt32() ([]int32, error)               // ConvertToSliceInt32 converts golist to []int32
	ConvertToSliceInt64() ([]int64, error)               // ConvertToSliceInt64 converts golist to []int64
	ConvertToSliceString() ([]string, error)             // ConvertToSliceString converts golist to []string
	Copy() (*List, error)                                // return a copy of list
	Count(element interface{}) int                       // count how many times an item appears in list
	Difference(other *List) (*List, error)               // Difference returns the elements in `list` that aren't in `other`.
	DifferenceBoth(other *List) (*List, error)           // DifferenceBoth returns the elements that aren't in both lists.
	Extend(element interface{}) error                    // extend list
	GCF() (interface{}, error)                           // gcf of list
	Get(index int) interface{}                           // get item with index
	HCF() (interface{}, error)                           // same as GCF
	Index(element interface{}) int                       // get item's index
	Insert(element interface{}, index int) error         // insert item
	IsEqual(other *List) bool                            // checks 2 lists are equal
	Join(joiner string) string                           // join elements in list
	Last() (interface{}, error)                          // get last item
	LCM() (interface{}, error)                           // lcm of lest
	Len() int                                            // length of list
	List() interface{}                                   // returns underlying slice
	ListDivide(other *List) (*List, error)               // divide elements in  2 lists
	ListDivideNo(no interface{}) (*List, error)          // divide all elements in list with no
	ListMultiply(other *List) (*List, error)             // multiply elements in  2 lists
	ListMultiplyNo(no interface{}) (*List, error)        // multiply no with all elements in list
	ListSubtract(other *List) (*List, error)             // subtract elements of list from other list
	ListSubtractNo(no interface{}) (*List, error)        // subtract no to all elements in list
	ListSum(other *List) (*List, error)                  // sum elements in 2 lists
	ListSumNo(no interface{}) (*List, error)             // add no to all elements in list
	Max() (interface{}, error)                           // max item
	Min() (interface{}, error)                           // min item
	Pop(index int) interface{}                           // remove item
	Rand() interface{}                                   // return random item
	Remove(element interface{}) error                    // remove item
	Replace(element interface{}, index int) error        // replace item
	Reverse() *List                                      // reverse list
	Set() (*List, error)                                 // remove duplicates
	Slice(start int, stop int) (*List, error)            // return sub list
	Sort(reverse bool)                                   // sort list in place
	Sorted(reverse bool) *List                           // returns new sorted list
	String() string                                      // string repr
	Sum() interface{}                                    // sum list items
	Type() ListType                                      // type of list
}

Lists : List interface and all methods

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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