Documentation
¶
Overview ¶
A doubly-linked list implementation in Go
This library implements doubly-linked list in Go. Since Go doesn't support generics, this library utilize empty interface to mimic this functionality and element comparison tasks are delegated to users.
Example:
package main import ( "github.com/cwchentw/golist" "log" ) func main() { list := list.New() for i := 1; i <= 10; i++ { list.Push(i) } data, _ := list.At(3) if data != 4 { log.Fatalln("Data error") } }
This library implements higher order functions as well. As Go lacks generics, the library delegates related interface implementations to users.
Example:
import ( "errors" "github.com/cwchentw/golist" "log" ) func main() { list := list.New() for i := 1; i <= 10; i++ { list.Push(i) } evens, _ := list.Select(even) for e := range evens.Iter() { n, _ := e.(int) if n % 2 != 0 { log.Fatalln("Error Select result") } } } func even(a interface{}) (bool, error) { n, ok := a.(int) if ok != true { return false, errors.New("Failed type assertion on a") } return n % 2 == 0, nil }
Index ¶
- type List
- func (list *List) All(grep func(interface{}) (bool, error)) (bool, error)
- func (list *List) Any(grep func(interface{}) (bool, error)) (bool, error)
- func (list *List) At(index uint) (interface{}, error)
- func (list *List) Clone() *List
- func (list *List) Each() <-chan interface{}
- func (list *List) Equal(other *List, eq func(interface{}, interface{}) (bool, error)) (bool, error)
- func (list *List) Find(data interface{}, cmp func(interface{}, interface{}) (bool, error)) (uint, error)
- func (list *List) IsEmpty() bool
- func (list *List) Iter() <-chan interface{}
- func (list *List) Len() uint
- func (list *List) Map(m func(interface{}) (interface{}, error)) (*List, error)
- func (list *List) Pop() (interface{}, error)
- func (list *List) Push(data interface{})
- func (list *List) Reduce(r func(a interface{}, b interface{}) (interface{}, error)) (interface{}, error)
- func (list *List) Remove(data interface{}, cmp func(interface{}, interface{}) (bool, error)) error
- func (list *List) RemoveAt(index uint) (interface{}, error)
- func (list *List) Select(grep func(interface{}) (bool, error)) (*List, error)
- func (list *List) Shift() (interface{}, error)
- func (list *List) Sort(pred func(interface{}, interface{}) (bool, error)) (*List, error)
- func (list *List) Unshift(data interface{})
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type List ¶
type List struct {
// contains filtered or unexported fields
}
List holds the internal structure of a doubly-linked list
func (*List) All ¶
Check whether all items in the list fulfill the predicate. Implement grep function object to use this method.
func (*List) Any ¶
Check whether any item in the list fulfills the predicate. Implement grep function object to use this method.
func (*List) Equal ¶
Check whether two lists are equal. If both the lengths and data of the two list are identical, the two lists are equal. Implement eq function object to use this method.
func (*List) Find ¶
func (list *List) Find(data interface{}, cmp func(interface{}, interface{}) (bool, error)) (uint, error)
Find an item in the list. Return error if not found. Implement cmp function object to use this method.
func (*List) Iter ¶
func (list *List) Iter() <-chan interface{}
Iterate through the list and get the elements.
func (*List) Map ¶
Iterate through the list and transform each datum into new value. Implement m function object to use this method.
func (*List) Reduce ¶
func (list *List) Reduce(r func(a interface{}, b interface{}) (interface{}, error)) (interface{}, error)
Reduce the list into single value. Implement Reduce interface to use this method.
func (*List) Remove ¶
Remove an item from the list. Return error if not found. Implement cmp function object to use this method
func (*List) RemoveAt ¶
Remove the element at “index“ from the list. Returns errors if “index“ out of range.
func (*List) Select ¶
Select items from the list when item fulfill the predicate. Implement grep function object to use this method.