Documentation
¶
Overview ¶
Package queue implements a persistent FIFO queue.
Index ¶
- type Queue
- func (q *Queue) Conj(elem interface{}) interface{}
- func (q *Queue) Equal(other interface{}) bool
- func (q *Queue) First() interface{}
- func (q *Queue) Length() int
- func (q *Queue) Pop() *Queue
- func (q *Queue) Push(elem interface{}) *Queue
- func (q *Queue) Range(do interface{})
- func (q *Queue) Reduce(fn interface{}, init interface{}) interface{}
- func (q *Queue) Seq() seq.Sequence
- func (q *Queue) String() string
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Queue ¶
type Queue struct {
// contains filtered or unexported fields
}
Queue represents a persistent immutable queue structure.
func From ¶
func From(value interface{}) *Queue
From returns a queue created from one of several go types:
*Queue:
The queue unmodified
[]interface{}:
A queue with the elements of the slice passed to New.
[]int:
A queue with the elements of the slice is created.
seq.Seqable:
A queue populated with the sequence returned by Seq.
seq.Sequence:
A queue populated with the elements of the sequence. Care should be taken to provide finite sequences or the queue will grow without bound.
Other:
Returns Empty()
Example (Int) ¶
q := From(1) fmt.Println(q) // Output [ ]
Output:
Example (SliceOfInt) ¶
q := From([]int{1, 2, 3, 4}) fmt.Println(q) // Output [ 1 2 3 4 ]
Output:
Example (SliceOfInterface) ¶
q := From([]interface{}{1, 2, 3, 4}) fmt.Println(q) // Output [ 1 2 3 4 ]
Output:
func New ¶
func New(elems ...interface{}) *Queue
New returns a queue populated with elems.
Example ¶
q := New(1, 2, 3, 4) fmt.Println(q) // Output [ 1 2 3 4 ]
Output:
func (*Queue) Conj ¶
func (q *Queue) Conj(elem interface{}) interface{}
Conj returns a Queue with the element added to the end. Conj implements a generic mechanism for building collections.
func (*Queue) Equal ¶
Equal returns whether the other value passed in is a queue and the values of that queue are equal to its values.
func (*Queue) First ¶
func (q *Queue) First() interface{}
First returns the first element of the queue.
Example ¶
q := New(1, 2, 3, 4) v := q.First() fmt.Println(v)
Output: 1
func (*Queue) Pop ¶
Pop returns a queue with the first element removed.
Example ¶
q := New(1, 2, 3, 4) q = q.Pop() fmt.Println(q)
Output: [ 2 3 4 ]
func (*Queue) Push ¶
Push returns a Queue with the element added to the end.
Example ¶
q := New(1, 2, 3, 4) q = q.Push(5) fmt.Println(q)
Output: [ 1 2 3 4 5 ]
func (*Queue) Range ¶
func (q *Queue) Range(do interface{})
Range calls the passed in function on each element of the queue. The function passed in may be of many types:
func(value interface{}) bool:
Takes a value of any type and returns if the loop should continue. Useful to avoid reflection where not needed and to support heterogenous queues.
func(value interface{})
Takes a value of any type. Useful to avoid reflection where not needed and to support heterogenous queues.
func(value T) bool:
Takes a value of the type of element stored in the queue and returns if the loop should continue. Useful for homogeneous queues. Is called with reflection and will panic if the type is incorrect.
func(value T)
Takes a value of the type of element stored in the queue and returns if the loop should continue. Useful for homogeneous queues. Is called with reflection and will panic if the type is incorrect.
Range will panic if passed anything that doesn't match one of these signatures
Example (Interface) ¶
q := New(1, 2, 3, 4) q.Range(func(v interface{}) { fmt.Println(v) })
Output: 1 2 3 4
Example (InterfaceContinue) ¶
q := New(1, 2, 3, 4) q.Range(func(v interface{}) bool { if v == 3 { return false } fmt.Println(v) return true })
Output: 1 2
Example (Type) ¶
q := New(1, 2, 3, 4) q.Range(func(v int) { fmt.Println(v) })
Output: 1 2 3 4
Example (TypeContinue) ¶
q := New(1, 2, 3, 4) q.Range(func(v int) bool { if v == 3 { return false } fmt.Println(v) return true })
Output: 1 2
func (*Queue) Reduce ¶
func (q *Queue) Reduce(fn interface{}, init interface{}) interface{}
Reduce is a fast mechanism for reducing a Queue. Reduce can take the following types as the fn:
func(init interface{}, value interface{}) interface{} func(init iT, v vT) oT
Reduce will panic if given any other function type.