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 ¶
- Variables
- type List
- func (arr *List) Add(other *List) (newList *List, err error)
- func (arr *List) Append(element interface{}) error
- func (arr *List) Clear()
- func (arr *List) Combinations(n int, joiner string) (*List, error)
- func (arr *List) CombinationsMax(n int, joiner string) (*List, error)
- func (arr *List) Contains(element interface{}) bool
- func (arr *List) ConvertTo(t ListType) (*List, error)
- func (arr *List) ConvertToSliceFloat32() ([]float32, error)
- func (arr *List) ConvertToSliceFloat64() ([]float64, error)
- func (arr *List) ConvertToSliceInt() ([]int, error)
- func (arr *List) ConvertToSliceInt32() ([]int32, error)
- func (arr *List) ConvertToSliceInt64() ([]int64, error)
- func (arr *List) ConvertToSliceString() ([]string, error)
- func (arr *List) Copy() (*List, error)
- func (arr *List) Count(element interface{}) int
- func (arr *List) Difference(other *List) (*List, error)
- func (arr *List) DifferenceBoth(other *List) (*List, error)
- func (arr *List) Extend(other interface{}) error
- func (arr *List) GCF() (gcf interface{}, err error)
- func (arr *List) Get(index int) interface{}
- func (arr *List) HCF() (gcf interface{}, err error)
- func (arr *List) Index(element interface{}) int
- func (arr *List) Insert(element interface{}, index int) error
- func (arr *List) IsEqual(other *List) bool
- func (arr *List) Join(joiner string) string
- func (arr *List) LCM() (lcm interface{}, err error)
- func (arr *List) Last() (interface{}, error)
- func (arr *List) Len() int
- func (arr *List) List() interface{}
- func (arr *List) ListDivide(other *List) (*List, error)
- func (arr *List) ListDivideNo(no interface{}) (*List, error)
- func (arr *List) ListMultiply(other *List) (*List, error)
- func (arr *List) ListMultiplyNo(no interface{}) (*List, error)
- func (arr *List) ListSubtract(other *List) (*List, error)
- func (arr *List) ListSubtractNo(no interface{}) (*List, error)
- func (arr *List) ListSum(other *List) (*List, error)
- func (arr *List) ListSumNo(no interface{}) (*List, error)
- func (arr *List) Max() (interface{}, error)
- func (arr *List) Min() (interface{}, error)
- func (arr *List) Pop(index int) interface{}
- func (arr *List) Rand() interface{}
- func (arr *List) Remove(element interface{}) error
- func (arr *List) Replace(element interface{}, index int) error
- func (arr *List) Reverse() *List
- func (arr *List) Set() (setList *List, err error)
- func (arr *List) Slice(start int, stop int) (*List, error)
- func (arr *List) Sort(reverse bool)
- func (arr *List) Sorted(reverse bool) *List
- func (arr *List) String() string
- func (arr *List) Sum() interface{}
- func (arr *List) Type() ListType
- type ListType
- type Lists
Examples ¶
Constants ¶
This section is empty.
Variables ¶
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
Add : adds two list together and returns new list and possible error
func (*List) Combinations ¶ added in v1.1.6
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
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
Contains : returns true if element exists, returns false otherwise ```golang list := golist.NewList([]string{"Hello", "World"}) fmt.Println(list.Contains("okay")) // false ```
func (*List) ConvertToSliceFloat32 ¶ added in v1.4.4
ConvertToSliceFloat32 converts golist to []float32
func (*List) ConvertToSliceFloat64 ¶ added in v1.4.4
ConvertToSliceFloat64 converts golist to []float64
func (*List) ConvertToSliceInt ¶ added in v1.4.4
ConvertToSliceInt converts golist to []int
func (*List) ConvertToSliceInt32 ¶ added in v1.4.4
ConvertToSliceInt32 converts golist to []int32
func (*List) ConvertToSliceInt64 ¶ added in v1.4.4
ConvertToSliceInt64 converts golist to []int64
func (*List) ConvertToSliceString ¶ added in v1.4.4
ConvertToSliceString converts golist to []string
func (*List) Count ¶
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
Difference returns the elements in `list` that aren't in `other`.
func (*List) DifferenceBoth ¶ added in v1.4.4
DifferenceBoth returns the elements that aren't in both lists.
func (*List) Extend ¶
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
GCF : returns greatest common factor of the list. returns error if type of list is string or any of the unsupported types
func (*List) HCF ¶ added in v1.1.5
HCF : returns greatest common factor of the list. returns error if type of list is string or any of the unsupported types
func (*List) Insert ¶
Insert : inserts an element at a given location. returns error if index is out of range
func (*List) IsEqual ¶ added in v1.1.6
IsEqual : checks if two lists are equal and returns a boolean
func (*List) LCM ¶ added in v1.1.5
LCM : returns Lowest common multiple of a list. returns error if list type is string
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
ListDivide divides list with other list
func (*List) ListDivideNo ¶ added in v1.4.0
ListDivideNo Divide all elements in list with a number
func (*List) ListMultiply ¶ added in v1.4.0
ListMultiply returns the product of contents of two lists
func (*List) ListMultiplyNo ¶ added in v1.4.0
ListMultiplyNo multiply a given number with all elements in list
func (*List) ListSubtract ¶ added in v1.4.0
ListSubtract subtracts the contents of two lists
func (*List) ListSubtractNo ¶ added in v1.4.0
ListSubtractNo subtracts a given number from all elements in list
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 ¶
Remove : removes a given element from list. returns error if element do not exist
func (*List) Replace ¶ added in v1.1.4
Replace : replaces an element in the lsit using the elements index. returns error if index do not exist.
func (*List) Sort ¶
Sort : sorts elements in the list in ascending order or descending order in place.
func (*List) Sorted ¶ added in v1.1.4
Sorted : sorts elements in the list in ascending order or descending order and returns new 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
Source Files ¶
- ListDivide.go
- ListMultiply.go
- ListSubtract.go
- ListSum.go
- MinMax.go
- add.go
- clear.go
- combination.go
- contains.go
- copy.go
- count.go
- diff.go
- errors.go
- extend.go
- gcf.go
- get.go
- insert.go
- isequal.go
- join.go
- lcm.go
- length.go
- list.go
- pop.go
- random.go
- remove.go
- replace.go
- reverse.go
- set.go
- slice.go
- sliceConversion.go
- sort.go
- string.go
- sum.go
- type.go