Documentation
¶
Index ¶
- Variables
- func All(subject Enumerable, p Predicate) bool
- func Any(iterator Enumerable) bool
- func Anyp(iterator Enumerable, p Predicate) bool
- func Count(iter Enumerable, p Predicate) int
- func CountAll(iter Enumerable) int
- func ElementAt(iter Enumerable, n uint) interface{}
- func First(subject Enumerable) (retval interface{}, err error)
- func IsErrorMultipleElements(err error) bool
- func IsErrorNoElements(err error) bool
- func Last(iter Enumerable) interface{}
- func Single(iter Enumerable) (retval interface{}, err error)
- func Singlep(iter Enumerable, pred Predicate) (retval interface{}, err error)
- func ToSlice(iter Enumerable) []interface{}
- func UCount(iter Enumerable, p Predicate) uint
- func UCountAll(iter Enumerable) uint
- type Comparator
- type Enumerable
- func AsEnumerable(entries ...interface{}) Enumerable
- func Merge(channels ...Enumerable) Enumerable
- func ParallelSelect(original Enumerable, operation Transform) Enumerable
- func Reverse(original Enumerable) Enumerable
- func Select(subject Enumerable, transform Transform) Enumerable
- func SelectMany(subject Enumerable, toMany Unfolder) Enumerable
- func Skip(subject Enumerable, n uint) Enumerable
- func Take(subject Enumerable, n uint) Enumerable
- func TakeWhile(subject Enumerable, criteria func(interface{}, uint) bool) Enumerable
- func Where(original Enumerable, p Predicate) Enumerable
- type Enumerator
- func (iter Enumerator) All(p Predicate) bool
- func (iter Enumerator) AsEnumerable() Enumerable
- func (iter Enumerator) Count(p Predicate) int
- func (iter Enumerator) CountAll() int
- func (iter Enumerator) Discard()
- func (iter Enumerator) ElementAt(n uint) interface{}
- func (iter Enumerator) Last() (retval interface{})
- func (iter Enumerator) Merge(others ...Enumerator) Enumerator
- func (iter Enumerator) ParallelSelect(operation Transform) Enumerator
- func (iter Enumerator) Reverse() Enumerator
- func (iter Enumerator) Select(transform Transform) Enumerator
- func (iter Enumerator) SelectMany(lister Unfolder) Enumerator
- func (iter Enumerator) Skip(n uint) Enumerator
- func (iter Enumerator) Take(n uint) Enumerator
- func (iter Enumerator) TakeWhile(criteria func(interface{}, uint) bool) Enumerator
- func (iter Enumerator) Tee() (Enumerator, Enumerator)
- func (iter Enumerator) ToSlice() []interface{}
- func (iter Enumerator) UCount(p Predicate) uint
- func (iter Enumerator) UCountAll() uint
- func (iter Enumerator) Where(predicate Predicate) Enumerator
- type LinkedList
- func (list *LinkedList) AddBack(entry interface{})
- func (list *LinkedList) AddFront(entry interface{})
- func (list *LinkedList) Enumerate(cancel <-chan struct{}) Enumerator
- func (list *LinkedList) Get(pos uint) (interface{}, bool)
- func (list *LinkedList) IsEmpty() bool
- func (list *LinkedList) Length() uint
- func (list *LinkedList) PeekBack() (interface{}, bool)
- func (list *LinkedList) PeekFront() (interface{}, bool)
- func (list *LinkedList) RemoveBack() (interface{}, bool)
- func (list *LinkedList) RemoveFront() (interface{}, bool)
- func (list *LinkedList) Sort(comparator Comparator) error
- func (list *LinkedList) Sorta() error
- func (list *LinkedList) Sorti() (err error)
- func (list *LinkedList) String() string
- func (list *LinkedList) Swap(x, y uint) error
- func (list *LinkedList) ToSlice() []interface{}
- type List
- func (l *List) Add(entries ...interface{})
- func (l *List) AddAt(pos uint, entries ...interface{})
- func (l *List) Enumerate(cancel <-chan struct{}) Enumerator
- func (l *List) Get(pos uint) (interface{}, bool)
- func (l *List) IsEmpty() bool
- func (l *List) Length() uint
- func (l *List) Remove(pos uint) (interface{}, bool)
- func (l *List) Set(pos uint, val interface{}) bool
- func (l *List) String() string
- func (l *List) Swap(x, y uint) bool
- type Predicate
- type Queue
- type Stack
- type Transform
- type Unfolder
Examples ¶
- AsEnumerable
- Enumerator.Count
- Enumerator.CountAll
- Enumerator.ElementAt
- Enumerator.Last
- Enumerator.Reverse
- Enumerator.Select
- Enumerator.SelectMany
- Enumerator.Skip
- Enumerator.Take
- Enumerator.TakeWhile
- Enumerator.Tee
- Enumerator.UCount
- Enumerator.UCountAll
- Enumerator.Where
- First
- Last
- LinkedList.AddBack
- LinkedList.AddFront
- LinkedList.Enumerate
- LinkedList.Get
- LinkedList.Sort
- LinkedList.Sorta
- LinkedList.Sorti
- LinkedList.String
- LinkedList.Swap
- List.AddAt
- Merge
- NewLinkedList
- NewQueue
- NewStack
- Queue.Add
- Queue.IsEmpty
- Queue.Next
- Select
- Skip
- Take
- TakeWhile
- UCount
- UCountAll
- Where
Constants ¶
This section is empty.
Variables ¶
var (
ErrUnexpectedType = errors.New("value was of an unexpected type")
)
A collection of errors that may be thrown by functions in this file.
Functions ¶
func All ¶ added in v0.3.0
func All(subject Enumerable, p Predicate) bool
All tests whether or not all items present in an Enumerable meet a criteria.
func Any ¶ added in v0.2.0
func Any(iterator Enumerable) bool
Any tests an Enumerable to see if there are any elements present.
func Anyp ¶ added in v0.2.0
func Anyp(iterator Enumerable, p Predicate) bool
Anyp tests an Enumerable to see if there are any elements present that meet a criteria.
func Count ¶ added in v0.2.0
func Count(iter Enumerable, p Predicate) int
Count iterates over a list and keeps a running tally of the number of elements satisfy a predicate.
func CountAll ¶ added in v0.2.0
func CountAll(iter Enumerable) int
CountAll iterates over a list and keeps a running tally of how many it's seen.
func ElementAt ¶ added in v0.2.0
func ElementAt(iter Enumerable, n uint) interface{}
ElementAt retreives an item at a particular position in an Enumerator.
func First ¶ added in v0.3.0
func First(subject Enumerable) (retval interface{}, err error)
First retrieves just the first item in the list, or returns an error if there are no elements in the array.
Example ¶
empty := NewQueue() notEmpty := NewQueue(1, 2, 3, 4) fmt.Println(First(empty)) fmt.Println(First(notEmpty))
Output: <nil> Enumerator encountered no elements 1 <nil>
func IsErrorMultipleElements ¶ added in v0.3.2
IsErrorMultipleElements determines whether or not the given error is the result of multiple values being returned when one or zero were expected.
func IsErrorNoElements ¶ added in v0.3.2
IsErrorNoElements determines whethr or not the given error is the result of no values being returned when one or more were expected.
func Last ¶ added in v0.2.0
func Last(iter Enumerable) interface{}
Last retreives the item logically behind all other elements in the list.
Example ¶
subject := NewList(1, 2, 3, 4) fmt.Println(Last(subject))
Output: 4
func Single ¶ added in v0.2.0
func Single(iter Enumerable) (retval interface{}, err error)
Single retreives the only element from a list, or returns nil and an error.
func Singlep ¶ added in v0.3.2
func Singlep(iter Enumerable, pred Predicate) (retval interface{}, err error)
Singlep retrieces the only element from a list that matches a criteria. If no match is found, or two or more are found, `Singlep` returns nil and an error.
func ToSlice ¶ added in v0.2.0
func ToSlice(iter Enumerable) []interface{}
ToSlice places all iterated over values in a Slice for easy consumption.
func UCount ¶ added in v0.2.0
func UCount(iter Enumerable, p Predicate) uint
UCount iterates over a list and keeps a running tally of the number of elements satisfy a predicate.
Example ¶
subject := NewStack(9, 'a', "str1")
result := UCount(subject, func(a interface{}) bool {
_, ok := a.(string)
return ok
})
fmt.Println(result)
Output: 1
func UCountAll ¶ added in v0.2.0
func UCountAll(iter Enumerable) uint
UCountAll iterates over a list and keeps a running tally of how many it's seen.
Example ¶
subject := NewStack(8, 9, 10, 11) fmt.Println(UCountAll(subject))
Output: 4
Types ¶
type Comparator ¶
Comparator is a function which evaluates two values to determine their relation to one another. - Zero is returned when `a` and `b` are equal. - Positive numbers are returned when `a` is greater than `b`. - Negative numbers are returned when `a` is less than `b`.
type Enumerable ¶
type Enumerable interface {
Enumerate(cancel <-chan struct{}) Enumerator
}
Enumerable offers a means of easily converting into a channel. It is most useful for types where mutability is not in question.
var Empty Enumerable = &emptyEnumerable{}
Empty is an Enumerable that has no elements, and will never have any elements.
var Fibonacci Enumerable = fibonacciGenerator{}
Fibonacci is an Enumerable which will dynamically generate the fibonacci sequence.
func AsEnumerable ¶ added in v0.2.0
func AsEnumerable(entries ...interface{}) Enumerable
AsEnumerable allows for easy conversion of a slice to a re-usable Enumerable object.
Example ¶
// When a single value is provided, and it is an array or slice, each value in the array or slice is treated as an enumerable value.
original := []int{1, 2, 3, 4, 5}
wrapped := AsEnumerable(original)
for entry := range wrapped.Enumerate(nil) {
fmt.Print(entry)
}
fmt.Println()
// When multiple values are provided, regardless of their type, they are each treated as enumerable values.
wrapped = AsEnumerable("red", "orange", "yellow", "green", "blue", "indigo", "violet")
for entry := range wrapped.Enumerate(nil) {
fmt.Println(entry)
}
Output: 12345 red orange yellow green blue indigo violet
func Merge ¶
func Merge(channels ...Enumerable) Enumerable
Merge takes the results as it receives them from several channels and directs them into a single channel.
Example ¶
a := AsEnumerable(1, 2, 4)
b := AsEnumerable(8, 16, 32)
c := Merge(a, b)
sum := 0
for x := range c.Enumerate(nil) {
sum += x.(int)
}
fmt.Println(sum)
product := 1
for y := range a.Enumerate(nil) {
product *= y.(int)
}
fmt.Println(product)
Output: 63 8
func ParallelSelect ¶ added in v0.3.0
func ParallelSelect(original Enumerable, operation Transform) Enumerable
ParallelSelect creates an Enumerable which will use all logically available CPUs to execute a Transform.
func Reverse ¶ added in v0.3.1
func Reverse(original Enumerable) Enumerable
Reverse will enumerate all values of an enumerable, store them in a Stack, then replay them all.
func Select ¶ added in v0.3.0
func Select(subject Enumerable, transform Transform) Enumerable
Select creates a reusable stream of transformed values.
Example ¶
const offset = 'a' - 1
subject := AsEnumerable('a', 'b', 'c')
subject = Select(subject, func(a interface{}) interface{} {
return a.(rune) - offset
})
fmt.Println(ToSlice(subject))
Output: [1 2 3]
func SelectMany ¶ added in v0.3.0
func SelectMany(subject Enumerable, toMany Unfolder) Enumerable
SelectMany allows for unfolding of values.
func Skip ¶ added in v0.3.0
func Skip(subject Enumerable, n uint) Enumerable
Skip creates a reusable stream which will skip the first `n` elements before iterating over the rest of the elements in an Enumerable.
Example ¶
done := make(chan struct{})
defer close(done)
trimmed := Take(Skip(Fibonacci, 1), 3)
for entry := range trimmed.Enumerate(done) {
fmt.Println(entry)
}
Output: 1 1 2
func Take ¶ added in v0.3.0
func Take(subject Enumerable, n uint) Enumerable
Take retreives just the first `n` elements from an Enumerable.
Example ¶
done := make(chan struct{})
defer close(done)
taken := Take(Fibonacci, 4)
for entry := range taken.Enumerate(done) {
fmt.Println(entry)
}
Output: 0 1 1 2
func TakeWhile ¶ added in v0.3.0
func TakeWhile(subject Enumerable, criteria func(interface{}, uint) bool) Enumerable
TakeWhile creates a reusable stream which will halt once some criteria is no longer met.
Example ¶
taken := TakeWhile(Fibonacci, func(x interface{}, n uint) bool {
return x.(int) < 10
})
for entry := range taken.Enumerate(nil) {
fmt.Println(entry)
}
Output: 0 1 1 2 3 5 8
func Where ¶ added in v0.3.0
func Where(original Enumerable, p Predicate) Enumerable
Where creates a reusable means of filtering a stream.
Example ¶
results := Where(AsEnumerable(1, 2, 3, 4, 5), func(a interface{}) bool {
return a.(int) < 3
})
fmt.Println(ToSlice(results))
Output: [1 2]
type Enumerator ¶
type Enumerator <-chan interface{}
Enumerator exposes a new syntax for querying familiar data structures.
func (Enumerator) All ¶
func (iter Enumerator) All(p Predicate) bool
All tests whether or not all items present meet a criteria.
func (Enumerator) AsEnumerable ¶ added in v0.3.0
func (iter Enumerator) AsEnumerable() Enumerable
AsEnumerable stores the results of an Enumerator so the results can be enumerated over repeatedly.
func (Enumerator) Count ¶
func (iter Enumerator) Count(p Predicate) int
Count iterates over a list and keeps a running tally of the number of elements satisfy a predicate.
Example ¶
subject := AsEnumerable("str1", "str1", "str2")
count1 := subject.Enumerate(nil).Count(func(a interface{}) bool {
return a == "str1"
})
fmt.Println(count1)
Output: 2
func (Enumerator) CountAll ¶
func (iter Enumerator) CountAll() int
CountAll iterates over a list and keeps a running tally of how many it's seen.
Example ¶
subject := AsEnumerable('a', 'b', 'c', 'd', 'e')
fmt.Println(subject.Enumerate(nil).CountAll())
// Ouput: 5
func (Enumerator) Discard ¶ added in v0.3.0
func (iter Enumerator) Discard()
Discard reads an enumerator to the end but does nothing with it. This method should be used in circumstances when it doesn't make sense to explicitly cancel the Enumeration.
func (Enumerator) ElementAt ¶
func (iter Enumerator) ElementAt(n uint) interface{}
ElementAt retreives an item at a particular position in an Enumerator.
Example ¶
done := make(chan struct{})
defer close(done)
fmt.Print(Fibonacci.Enumerate(done).ElementAt(4))
Output: 3
func (Enumerator) Last ¶
func (iter Enumerator) Last() (retval interface{})
Last retreives the item logically behind all other elements in the list.
Example ¶
subject := AsEnumerable(1, 2, 3) fmt.Print(subject.Enumerate(nil).Last())
Output: 3
func (Enumerator) Merge ¶
func (iter Enumerator) Merge(others ...Enumerator) Enumerator
Merge takes the results of this Enumerator and others, and funnels them into a single Enumerator. The order of in which they will be combined is non-deterministic.
func (Enumerator) ParallelSelect ¶ added in v0.2.0
func (iter Enumerator) ParallelSelect(operation Transform) Enumerator
ParallelSelect will execute a Transform across all logical CPUs available to the current process.
func (Enumerator) Reverse ¶
func (iter Enumerator) Reverse() Enumerator
Reverse returns items in the opposite order it encountered them in.
Example ¶
a := AsEnumerable(1, 2, 3).Enumerate(nil) a = a.Reverse() fmt.Println(a.ToSlice())
Output: [3 2 1]
func (Enumerator) Select ¶
func (iter Enumerator) Select(transform Transform) Enumerator
Select iterates over a list and returns a transformed item.
Example ¶
subject := AsEnumerable('a', 'b', 'c').Enumerate(nil)
const offset = 'a' - 1
results := subject.Select(func(a interface{}) interface{} {
return a.(rune) - offset
})
fmt.Println(results.ToSlice())
Output: [1 2 3]
func (Enumerator) SelectMany ¶ added in v0.2.0
func (iter Enumerator) SelectMany(lister Unfolder) Enumerator
SelectMany allows for flattening of data structures.
Example ¶
type BrewHouse struct {
Name string
Beers Enumerable
}
breweries := AsEnumerable(
BrewHouse{
"Mac & Jacks",
AsEnumerable(
"African Amber",
"Ibis IPA",
),
},
BrewHouse{
"Post Doc",
AsEnumerable(
"Prereq Pale",
),
},
BrewHouse{
"Resonate",
AsEnumerable(
"Comfortably Numb IPA",
"Lithium Altbier",
),
},
BrewHouse{
"Triplehorn",
AsEnumerable(
"Samson",
"Pepper Belly",
),
},
)
beers := breweries.Enumerate(nil).SelectMany(func(brewer interface{}) Enumerator {
return brewer.(BrewHouse).Beers.Enumerate(nil)
})
for beer := range beers {
fmt.Println(beer)
}
Output: African Amber Ibis IPA Prereq Pale Comfortably Numb IPA Lithium Altbier Samson Pepper Belly
func (Enumerator) Skip ¶
func (iter Enumerator) Skip(n uint) Enumerator
Skip retreives all elements after the first 'n' elements.
Example ¶
subject := AsEnumerable(1, 2, 3, 4, 5, 6, 7)
skipped := subject.Enumerate(nil).Skip(5)
for entry := range skipped {
fmt.Println(entry)
}
Output: 6 7
func (Enumerator) Take ¶
func (iter Enumerator) Take(n uint) Enumerator
Take retreives just the first 'n' elements from an Enumerator.
Example ¶
done := make(chan struct{})
defer close(done)
taken := Fibonacci.Enumerate(done).Skip(4).Take(2)
for entry := range taken {
fmt.Println(entry)
}
Output: 3 5
func (Enumerator) TakeWhile ¶
func (iter Enumerator) TakeWhile(criteria func(interface{}, uint) bool) Enumerator
TakeWhile continues returning items as long as 'criteria' holds true.
Example ¶
taken := Fibonacci.Enumerate(nil).TakeWhile(func(x interface{}, n uint) bool {
return x.(int) < 6
})
for entry := range taken {
fmt.Println(entry)
}
Output: 0 1 1 2 3 5
func (Enumerator) Tee ¶
func (iter Enumerator) Tee() (Enumerator, Enumerator)
Tee creates two Enumerators which will have identical contents as one another.
Example ¶
base := AsEnumerable(1, 2, 4)
left, right := base.Enumerate(nil).Tee()
var wg sync.WaitGroup
wg.Add(2)
product := 1
go func() {
for x := range left {
product *= x.(int)
}
wg.Done()
}()
sum := 0
go func() {
for x := range right {
sum += x.(int)
}
wg.Done()
}()
wg.Wait()
fmt.Printf("Sum: %d\n", sum)
fmt.Printf("Product: %d\n", product)
Output: Sum: 7 Product: 8
func (Enumerator) ToSlice ¶
func (iter Enumerator) ToSlice() []interface{}
ToSlice places all iterated over values in a Slice for easy consumption.
func (Enumerator) UCount ¶
func (iter Enumerator) UCount(p Predicate) uint
UCount iterates over a list and keeps a running tally of the number of elements satisfy a predicate.
Example ¶
subject := AsEnumerable("str1", "str1", "str2")
count1 := subject.Enumerate(nil).UCount(func(a interface{}) bool {
return a == "str1"
})
fmt.Println(count1)
Output: 2
func (Enumerator) UCountAll ¶
func (iter Enumerator) UCountAll() uint
UCountAll iterates over a list and keeps a running tally of how many it's seen.
Example ¶
subject := AsEnumerable('a', 2, "str1")
fmt.Println(subject.Enumerate(nil).UCountAll())
Output: 3
func (Enumerator) Where ¶
func (iter Enumerator) Where(predicate Predicate) Enumerator
Where iterates over a list and returns only the elements that satisfy a predicate.
Example ¶
done := make(chan struct{})
defer close(done)
results := Fibonacci.Enumerate(done).Where(func(a interface{}) bool {
return a.(int) > 8
}).Take(3)
fmt.Println(results.ToSlice())
Output: [13 21 34]
type LinkedList ¶
type LinkedList struct {
// contains filtered or unexported fields
}
LinkedList encapsulates a list where each entry is aware of only the next entry in the list.
func NewLinkedList ¶
func NewLinkedList(entries ...interface{}) *LinkedList
NewLinkedList instantiates a new LinkedList with the entries provided.
Example ¶
subject1 := NewLinkedList('a', 'b', 'c', 'd', 'e')
fmt.Println(subject1.Length())
slice := []interface{}{1, 2, 3, 4, 5, 6}
subject2 := NewLinkedList(slice...)
fmt.Println(subject2.Length())
Output: 5 6
func (*LinkedList) AddBack ¶
func (list *LinkedList) AddBack(entry interface{})
AddBack creates an entry in the LinkedList that is logically at the back of the list.
Example ¶
subject := NewLinkedList(2, 3, 5) subject.AddBack(8) result, _ := subject.PeekBack() fmt.Println(result) fmt.Println(subject.Length())
Output: 8 4
func (*LinkedList) AddFront ¶
func (list *LinkedList) AddFront(entry interface{})
AddFront creates an entry in the LinkedList that is logically at the front of the list.
Example ¶
subject := NewLinkedList(2, 3) subject.AddFront(1) result, _ := subject.PeekFront() fmt.Println(result)
Output: 1
func (*LinkedList) Enumerate ¶
func (list *LinkedList) Enumerate(cancel <-chan struct{}) Enumerator
Enumerate creates a new instance of Enumerable which can be executed on.
Example ¶
subject := NewLinkedList(2, 3, 5, 8)
results := subject.Enumerate(nil).Select(func(a interface{}) interface{} {
return -1 * a.(int)
})
for entry := range results {
fmt.Println(entry)
}
Output: -2 -3 -5 -8
func (*LinkedList) Get ¶
func (list *LinkedList) Get(pos uint) (interface{}, bool)
Get finds the value from the LinkedList. pos is expressed as a zero-based index begining from the 'front' of the list.
Example ¶
subject := NewLinkedList(2, 3, 5, 8) val, _ := subject.Get(2) fmt.Println(val)
Output: 5
func (*LinkedList) IsEmpty ¶
func (list *LinkedList) IsEmpty() bool
IsEmpty tests the list to determine if it is populate or not.
func (*LinkedList) Length ¶
func (list *LinkedList) Length() uint
Length returns the number of elements present in the LinkedList.
func (*LinkedList) PeekBack ¶
func (list *LinkedList) PeekBack() (interface{}, bool)
PeekBack returns the entry logicall stored at the back of the list without removing it.
func (*LinkedList) PeekFront ¶
func (list *LinkedList) PeekFront() (interface{}, bool)
PeekFront returns the entry logically stored at the front of this list without removing it.
func (*LinkedList) RemoveBack ¶
func (list *LinkedList) RemoveBack() (interface{}, bool)
RemoveBack returns the entry logically stored at the back of this list and removes it.
func (*LinkedList) RemoveFront ¶
func (list *LinkedList) RemoveFront() (interface{}, bool)
RemoveFront returns the entry logically stored at the front of this list and removes it.
func (*LinkedList) Sort ¶
func (list *LinkedList) Sort(comparator Comparator) error
Sort rearranges the positions of the entries in this list so that they are ascending.
Example ¶
// Sorti sorts into ascending order, this example demonstrates sorting
// into descending order.
subject := NewLinkedList(2, 4, 3, 5, 7, 7)
subject.Sort(func(a, b interface{}) (int, error) {
castA, ok := a.(int)
if !ok {
return 0, ErrUnexpectedType
}
castB, ok := b.(int)
if !ok {
return 0, ErrUnexpectedType
}
return castB - castA, nil
})
fmt.Println(subject)
Output: [7 7 5 4 3 2]
func (*LinkedList) Sorta ¶
func (list *LinkedList) Sorta() error
Sorta rearranges the position of string entries in this list so that they are ascending.
Example ¶
subject := NewLinkedList("charlie", "alfa", "bravo", "delta")
subject.Sorta()
for _, entry := range subject.ToSlice() {
fmt.Println(entry.(string))
}
Output: alfa bravo charlie delta
func (*LinkedList) Sorti ¶
func (list *LinkedList) Sorti() (err error)
Sorti rearranges the position of integer entries in this list so that they are ascending.
Example ¶
subject := NewLinkedList(7, 3, 2, 2, 3, 6) subject.Sorti() fmt.Println(subject)
Output: [2 2 3 3 6 7]
func (*LinkedList) String ¶
func (list *LinkedList) String() string
String prints upto the first fifteen elements of the list in string format.
Example ¶
subject1 := NewLinkedList()
for i := 0; i < 20; i++ {
subject1.AddBack(i)
}
fmt.Println(subject1)
subject2 := NewLinkedList(1, 2, 3)
fmt.Println(subject2)
Output: [0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...] [1 2 3]
func (*LinkedList) Swap ¶
func (list *LinkedList) Swap(x, y uint) error
Swap switches the positions in which two values are stored in this list. x and y represent the indexes of the items that should be swapped.
Example ¶
subject := NewLinkedList(2, 3, 5, 8, 13) subject.Swap(1, 3) fmt.Println(subject)
Output: [2 8 5 3 13]
func (*LinkedList) ToSlice ¶
func (list *LinkedList) ToSlice() []interface{}
ToSlice converts the contents of the LinkedList into a slice.
type List ¶
type List struct {
// contains filtered or unexported fields
}
List is a dynamically sized list akin to List in the .NET world, ArrayList in the Java world, or vector in the C++ world.
func NewList ¶
func NewList(entries ...interface{}) *List
NewList creates a new list which contains the elements provided.
func (*List) Add ¶
func (l *List) Add(entries ...interface{})
Add appends an entry to the logical end of the List.
func (*List) AddAt ¶
AddAt injects values beginning at `pos`. If multiple values are provided in `entries` they are placed in the same order they are provided.
Example ¶
subject := NewList(0, 1, 4, 5, 6) subject.AddAt(2, 2, 3) fmt.Println(subject)
Output: [0 1 2 3 4 5 6]
func (*List) Enumerate ¶
func (l *List) Enumerate(cancel <-chan struct{}) Enumerator
Enumerate lists each element present in the collection
func (*List) Get ¶
Get retreives the value stored in a particular position of the list. If no item exists at the given position, the second parameter will be returned as false.
type Predicate ¶
type Predicate func(interface{}) bool
Predicate defines an interface for funcs that make some logical test.
type Queue ¶
type Queue struct {
// contains filtered or unexported fields
}
Queue implements a basic FIFO structure.
func NewQueue ¶
func NewQueue(entries ...interface{}) *Queue
NewQueue instantiates a new FIFO structure.
Example ¶
empty := NewQueue() fmt.Println(empty.Length()) populated := NewQueue(1, 2, 3, 5, 8, 13) fmt.Println(populated.Length())
Output: 0 6
func (*Queue) Add ¶
func (q *Queue) Add(entry interface{})
Add places an item at the back of the Queue.
Example ¶
subject := &Queue{}
subject.Add(1)
subject.Add(2)
res, _ := subject.Peek()
fmt.Println(res)
Output: 1
func (*Queue) Enumerate ¶ added in v0.2.0
func (q *Queue) Enumerate(cancel <-chan struct{}) Enumerator
Enumerate peeks at each element of this queue without mutating it.
func (*Queue) IsEmpty ¶
IsEmpty tests the Queue to determine if it is populate or not.
Example ¶
empty := NewQueue() fmt.Println(empty.IsEmpty()) populated := NewQueue(1, 2, 3, 5, 8, 13) fmt.Println(populated.IsEmpty())
Output: true false
func (*Queue) Next ¶
Next removes and returns the next item in the Queue.
Example ¶
subject := NewQueue(1, 2, 3, 5, 8, 13)
for !subject.IsEmpty() {
val, _ := subject.Next()
fmt.Println(val)
}
Output: 1 2 3 5 8 13
type Stack ¶
type Stack struct {
// contains filtered or unexported fields
}
Stack implements a basic FILO structure.
func NewStack ¶
func NewStack(entries ...interface{}) *Stack
NewStack instantiates a new FILO structure.
Example ¶
subject := NewStack(1, 2, 3)
for !subject.IsEmpty() {
val, _ := subject.Pop()
fmt.Println(val)
}
Output: 3 2 1
func (*Stack) Enumerate ¶ added in v0.2.0
func (stack *Stack) Enumerate(cancel <-chan struct{}) Enumerator
Enumerate peeks at each element in the stack without mutating it.
type Transform ¶ added in v0.2.0
type Transform func(interface{}) interface{}
Transform defines a function which takes a value, and returns some value based on the original.
var Identity Transform = func(value interface{}) interface{} {
return value
}
Identity is a trivial Transform which applies no operation on the value.
type Unfolder ¶ added in v0.3.0
type Unfolder func(interface{}) Enumerator
Unfolder defines a function which takes a single value, and exposes many of them as an Enumerator