queue

package
v0.0.0-...-7241100 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2019 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package queue implements a persistent FIFO queue.

Index

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 Empty

func Empty() *Queue

Empty returns an empty queue.

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

func (q *Queue) Equal(other interface{}) bool

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) Length

func (q *Queue) Length() int

Length returns the number of elements currently in the queue.

func (*Queue) Pop

func (q *Queue) Pop() *Queue

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

func (q *Queue) Push(elem interface{}) *Queue

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.

func (*Queue) Seq

func (q *Queue) Seq() seq.Sequence

Seq returns the queue as a sequence.

Example (String)
fmt.Println(New(1, 2, 3, 4, 5, 6).Seq())
Output:

(1 2 3 4 5 6)

func (*Queue) String

func (q *Queue) String() string

String returns a representation of the queue as a string.

Example
fmt.Println(New(1, 2, 3, 4, 5, 6))
Output:

[ 1 2 3 4 5 6 ]

Jump to

Keyboard shortcuts

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