hoi

package
v0.0.0-...-1dc7587 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2016 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

The Higher Order Iterators (hoi) package is a collection of iterators that build on the common interfacs provided by package i and provde higher order functionality. Most functions in this package transform a iterator into a new iterator that transforms the original iteration according to some computation.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Append

func Append(itrs ...i.Forward) i.Forward

Build an iteteator that appends a list of iteratior. Looping over the Append iterator will loop over all of the provided iterators.

Example
// Three lists that we wish to append together
list1 := icon.List("one", "two", "three")
list2 := icon.List(1, 2, 3)
list3 := icon.List(1.1, 2.2, 3.3)

// Define Append iterator
appendItr := Append(list1, list2, list3)
for ; !appendItr.AtEnd(); appendItr.Next() {
	// Prints out the list: one, two, three, 1, 2, 3, 1.1, 2.2, 3.3,
	fmt.Printf("%v, ", appendItr.Value())
}
Output:

func BMap

func BMap(mapf MapFunc, itr i.Bounded) i.Bounded

A bounded map iterator that maps a underlying data stream to a new data stream according to the computation provied by the mapping function.

func BasMap

func BasMap(mapf MapFunc, itr i.BoundedAtStart) i.BoundedAtStart

A bounded at start map iterator that maps a underlying data stream to a new data stream according to the computation provied by the mapping function.

func BiMap

func BiMap(mapf MapFunc, itr i.BiDirectional) i.BiDirectional

A bidirectional map iterator that maps a underlying data stream to a new data stream according to the computation provied by the mapping function.

func Count

func Count(max int, itr i.Forward) i.Forward

Given the data stream [1,2,3,4,5,6,7,8,...] the Count(5, stream) iterator will result n the data stream [1,2,3,4,5]

Example
// The underlying data stream
list := icon.List(1, 2, 3, 4, 5, 6, 7, 8, 9)

// The Count iterator
itrCount := Count(5, list)
for ; !itrCount.AtEnd(); itrCount.Next() {
	// Prints out the list: 1,2,3,4,5,
	fmt.Printf("%v, ", itrCount.Value())
}
Output:

func Cycle

func Cycle(itr i.BoundedAtStart) i.Forward

The Cycle iterator will transform any BoundedAtStart iterator into a infinitie data stream that will cycle to the start of the original stream when it reaches the end.

Example
// The underlying data stream
list := icon.List(1, 2, 3)

// The Cycle iterator
itrCycle := Cycle(list)
for i := 0; i < 10; i++ {
	// Prints out the list: 1, 2, 3, 1, 2, 3, 1, 2, 3, 1,
	fmt.Printf("%v, ", itrCycle.Value())
	itrCycle.Next()
}
Output:

func Each

func Each(itr i.Forward, e EachFunc)

The Each function provides a loop around the iterator, calling EachFunc on each iterator. If the EachFunc returns false, the Each function quits. Otherwise the function loops to the next value in the stream, until the end is reached.

func Filter

func Filter(ff FilterFunc, itr i.Forward) i.Forward

Constructs a filtering iterator from a filtering function and a iterator providing access to the data stream that we wish to filter.

Example
// The underlying data stream
list := icon.List(1, 2, 3, 4, 5, 6, 7, 8, 9)

// The filtering function will filter out odd numbers
filterFunc := func(itr i.Iterator) bool {
	v, _ := itr.Value().(int)
	return math.Mod(float64(v), 2) == 0
}

// The Filter iterator
itrFilter := Filter(filterFunc, list)
for ; !itrFilter.AtEnd(); itrFilter.Next() {
	// Prints out the list: 2, 4, 6, 8
	fmt.Printf("%v, ", itrFilter.Value())
}
Output:

func Foldl

func Foldl(ff FoldFunc, init interface{}, itr i.Forward) i.Forward

Fold a data stream from the left. If the providing iterator gives access to the datastream [1,2,3,4,5] the Foldl iterator will apply the folding function to the data stream in the following way (provided the iteration is completed): f(f(f(f(f(inital value, 1), 2), 3), 4), 5)

Example
// The underying data stream
list := icon.List(1, 2, 3, 4, 5)

// The folding function
foldFunc := func(v interface{}, i i.Iterator) interface{} {
	// Fold the two values into the sum of the two values
	return v.(int) + i.Value().(int)
}

// The Foldl iteator
itrFoldl := Foldl(foldFunc, 1, list)
for ; !itrFoldl.AtEnd(); itrFoldl.Next() {
	// Prints: 2, 4, 7, 11, 16
	fmt.Printf("%v, ", itrFoldl.Value())
}
Output:

func Foldr

func Foldr(ff FoldFunc, init interface{}, itr i.Bounded) i.Forward

Fold a data stream from the right. If the providing iterator gives access to the datastream [1,2,3,4,5] the Foldr iterator will apply the folding function to the data stream in the following way (provided the iteration is completed): f(f(f(f(f(inital value, 5), 4), 3), 2), 1)

Example
// The underying data stream
list := icon.List(1, 2, 3, 4, 5)

// The folding function
foldFunc := func(v interface{}, i i.Iterator) interface{} {
	// Fold the two values into the sum of the two values
	return v.(int) + i.Value().(int)
}

// The Foldr iteator
itrFoldr := Foldr(foldFunc, 1, list)
for ; !itrFoldr.AtEnd(); itrFoldr.Next() {
	// Prints: 6, 10, 13, 15, 16
	fmt.Printf("%v, ", itrFoldr.Value())
}
Output:

func Map

func Map(mapf MapFunc, itr i.Forward) i.Forward

A forward map iterator that maps a underlying data stream to a new data stream according to the computation provied by the mapping function.

Example
// The underlying data stream
list := icon.List(1, 2, 3, 4, 5, 6, 7, 8, 9)

// The mapping function will map even numbers to its square
// and odd numbers to 1
mapFunc := func(itr i.Iterator) interface{} {
	v, _ := itr.Value().(int)
	if math.Mod(float64(v), 2) == 0 {
		return v * v
	}
	return 1
}

// The Map iterator
itrMap := Map(mapFunc, list)
for ; !itrMap.AtEnd(); itrMap.Next() {
	// Prints out the list: 1, 4, 1, 8, 1, 36, 1, 64, 1
	fmt.Printf("%v, ", itrMap.Value())
}
Output:

func RaMap

func RaMap(mapf MapFunc, itr i.RandomAccess) i.RandomAccess

A random access map iterator that maps a underlying data stream to a new data stream according to the computation provied by the mapping function.

func Repeat

func Repeat(count int, itr i.Forward) i.Forward

The Repeat iteator will iterate over a data stream and repeat each item in the list. E.g give the data stream [1,2,3] the Count(3, stream) will result in the data stream [1, 1, 1, 2, 2, 2, 3, 3, 3].

Example
// The uderlying data stream
list := icon.List(1, 2, 3, 4)

// The repeating iterator
itrRepeat := Repeat(2, list)

for ; !itrRepeat.AtEnd(); itrRepeat.Next() {
	// Prints out the list: 1, 1, 2, 2, 3, 3, 4, 4,
	fmt.Printf("%v, ", itrRepeat.Value())
}
Output:

func Reverse

func Reverse(itr i.Bounded) i.Forward

The Reverse iterator will construct an iterator that will loop over the data stream in reverse order. Given the data stream [1,2,3,4], the iterator will provide the data stream [4,3,2,1]

Example
// The data stream
list := icon.List(1, 2, 3, 4, 5)

// The reversing iterator
itrReverse := Reverse(list)
for ; !itrReverse.AtEnd(); itrReverse.Next() {
	// Prints out: 5, 4, 3, 2, 1,
	fmt.Printf("%v, ", itrReverse.Value())
}
Output:

func Sample

func Sample(itr i.RandomAccess) i.Forward

The Sample iterator will construct an infinite data stream from an underlying finite data stream, selecting elements from the underlying in a random fashion. The selection is a weak random selection, there is no guarantee that item A is not selected twice before item B is selected for the first time.

Example
// The underlying data stream
list := icon.List(1, 2, 3, 4, 5, 6, 7, 8, 9)

// The Sample iterator
itrSample := Sample(list)

fmt.Println(itrSample.Value()) // Prints a random element
fmt.Println(itrSample.Value()) // Prints the same element
itrSample.Next()
fmt.Println(itrSample.Value()) // Prints a random element, might be the same as before
Output:

func Shuffle

func Shuffle(itr i.RandomAccess) i.Forward

The Shuffle iterator will construct an finite data stream from an underlying finite data stream, selecting elements from the underlying in a random fashion. The selection is a strong random selection, each element will be selected only once.

Example
// The underlying data stream
list := icon.List(1, 2, 3, 4, 5, 6, 7, 8, 9)

// The Shuffle iterator
itrShuffle := Shuffle(list)

for ; !itrShuffle.AtEnd(); itrShuffle.Next() {
	// Prints out the underlying data stream in a random order
	fmt.Printf("%v, ", itrShuffle.Value())
}
Output:

func Slice

func Slice(start, end int, itr i.RandomAccess) i.RandomAccess

The Slice iterator will give access to a subset of an underlying data stream. Given the stream [1,2,3,4,5,6,7,8], the Slice(2,6,stream) iterator will give access to the stream [3,4,5,6].

Example
// The data stream
list := icon.List(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

// The Slice iterator
itrSlice := Slice(4, 8, list)

for ; !itrSlice.AtEnd(); itrSlice.Next() {
	// Produces the list 4, 5, 6, 7,
	fmt.Printf("%v, ", itrSlice.Value())
}
Output:

func Zip

func Zip(itrs ...i.Forward) i.Forward

The Zip iterator will zip together a collection of data streams, stopping after the shortest data stream is finished. Given e.g. the data streams [1,2,3], [5,6,7] and [10,11,12,13] Zip will provide access to the data sream [[1,5,10], [2,6,11], [3,7,12]].

Example
// The data streams
list1 := icon.List(1, 2, 3)
list2 := icon.List("one", "two", "three")
list3 := icon.List(10, 11, 12, 13)

// The Zip iterator
itrZip := Zip(list1, list2, list3)
for ; !itrZip.AtEnd(); itrZip.Next() {
	// Prints out: [[1,"one",10], [2,"two",11], [3,"three",12]]
	fmt.Printf("%v, ", itrZip.Value())
}
Output:

func ZipLongest

func ZipLongest(itrs ...i.Forward) i.Forward

The ZipLongest iterator will zip together a collection of data streams, not stopping until the longest data stream is finished. Given e.g. the data streams [1,2,3], [5,6,7] and [10,11,12,13] Zip will provide access to the data sream [[1,5,10], [2,6,11], [3,7,12], [nil, nil, 13]].

Example
// The data streams
list1 := icon.List(1, 2, 3)
list2 := icon.List("one", "two", "three")
list3 := icon.List(10, 11, 12, 13)

// The Zip iterator
itrZipLongest := ZipLongest(list1, list2, list3)
for ; !itrZipLongest.AtEnd(); itrZipLongest.Next() {
	// Prints out: [[1,"one",10], [2,"two",11], [3,"three",12], [nil,nil,13]]
	fmt.Printf("%v, ", itrZipLongest.Value())
}
Output:

Types

type EachFunc

type EachFunc func(itr i.Iterator) bool

The function to provide to the Each function. The function will recieve an iterator that points to some position in the data stream. If the function returns false, the loop breaks. Otherwise the loop continues until the end of the data stream is reached.

type FilterFunc

type FilterFunc func(i.Iterator) bool

The FilterFunc is a function that will be called on each item in the data stream by the Filter iterator. If the function returns true, the item will be included in the resulting stream, otherwise it will be omitted.

type FoldFunc

type FoldFunc func(interface{}, i.Iterator) interface{}

The FoldFunc will provide the folding action that the Foldl and Foldr iterators will apply to a data stream.

type MapFunc

type MapFunc func(i.Iterator) interface{}

A function that will provide the mapping computation for the Map iterator.

Jump to

Keyboard shortcuts

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