Documentation ¶
Index ¶
- Constants
- func False(t Entry) bool
- func True(t Entry) bool
- type BiFunction
- type Consumer
- type Entry
- type EntryBool
- type EntryFloat
- type EntryInt
- type EntryMap
- type EntrySlice
- type EntryString
- type FloatStream
- type Function
- type Functor
- type IntStream
- type Maybe
- type Predicate
- type Stream
- func (s Stream) AllMatch(p Predicate) bool
- func (s Stream) AnyMatch(p Predicate) bool
- func (s Stream) Count() int
- func (s Stream) Drop(n uint64) Stream
- func (s Stream) DropUntil(p Predicate) Stream
- func (s Stream) DropWhile(p Predicate) Stream
- func (s Stream) EndsWith(slice []Entry) bool
- func (s Stream) Filter(predicate Predicate) Stream
- func (s Stream) ForEach(consumer Consumer)
- func (s Stream) GroupBy(classifier Function) EntryMap
- func (s Stream) Head() Entry
- func (s Stream) HeadN(n uint64) []Entry
- func (s Stream) Intersperse(e Entry) Stream
- func (s Stream) Last() Entry
- func (s Stream) LastN(n uint64) []Entry
- func (s Stream) LeftReduce(f2 BiFunction) Entry
- func (s Stream) Map(mapper Function) Stream
- func (s Stream) MapToInt(toInt ToIntFunction) IntStream
- func (s Stream) NoneMatch(p Predicate) bool
- func (s Stream) Reduce(f2 BiFunction) Entry
- func (s Stream) StartsWith(slice []Entry) bool
- func (s Stream) Take(n uint64) Stream
- func (s Stream) TakeUntil(p Predicate) Stream
- func (s Stream) TakeWhile(p Predicate) Stream
- type ToIntFunction
- type Tuple
- type Tuple0
- type Tuple1
- type Tuple2
Examples ¶
Constants ¶
const PanicMissingChannel = "stream creation requires a channel"
PanicMissingChannel signifies that the Stream is missing a channel.
const PanicNoSuchElement = "no such element"
PanicNoSuchElement signifies that the requested element is not present.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type BiFunction ¶
BiFunction that accepts two arguments and produces a result.
Example ¶
ExampleBiFunction shows how to use BiFunction's. There are more interesting examples through the code. Search for `BiFunction` or the BiFunction signature.
data := []ƒ.Entry{ EntryString("four"), EntryString("twelve"), EntryString("one"), EntryString("six"), EntryString("three")} res := ƒ.NewStreamFromSlice(data, 0). Reduce(concatenateStringsBiFunc) fmt.Printf("res = %+v\n", res)
Output: res = four-twelve-one-six-three
type Consumer ¶
type Consumer func(i Entry)
Consumer that accepts one argument and does not return any value.
type Entry ¶
type Entry interface { Hash() uint32 // TODO: remove Hash() since the project no longer includes collections? Hashes suffer from collision. Equal(Entry) bool }
Entry is the simplest behaviour that functional types must adhere to.
type EntryBool ¶
type EntryBool bool
EntryBool is an Entry for 'bool'.
type EntryFloat ¶
type EntryFloat float32
EntryFloat is an Entry for 'float32'.
func (EntryFloat) Equal ¶
func (f EntryFloat) Equal(e Entry) bool
Equal returns true if 'e' and 'f' are equal.
type EntryInt ¶
type EntryInt int
EntryInt is an Entry for 'int'.
type EntryMap ¶
type EntryMap map[Entry]EntrySlice
EntryMap is an Entry for 'map[Entry]EntrySlice'.
type EntrySlice ¶
type EntrySlice []Entry
EntrySlice is an Entry for '[]Entry'.
func (EntrySlice) Equal ¶
func (es EntrySlice) Equal(e Entry) bool
Equal returns true if this type is equal to 'e'.
type EntryString ¶
type EntryString string
EntryString is an Entry for 'string'.
func (EntryString) Equal ¶
func (es EntryString) Equal(e Entry) bool
Equal returns true if 'e' and 'i' are equal.
func (EntryString) Len ¶
func (es EntryString) Len() EntryInt
Len transform the string to lower case.
func (EntryString) ToLower ¶
func (es EntryString) ToLower() EntryString
ToLower transform the string to lower case.
func (EntryString) ToUpper ¶
func (es EntryString) ToUpper() EntryString
ToUpper transform the string to upper case.
type FloatStream ¶
type FloatStream struct {
Stream
}
FloatStream is a sequence of EntryFloat elements supporting sequential and (in the future?) parallel operations.
func NewFloatStream ¶
func NewFloatStream(c chan EntryFloat) FloatStream
NewFloatStream creates a new FloatStream. This function leaves the provided channel is the same state of openness.
func NewFloatStreamFromSlice ¶
func NewFloatStreamFromSlice(is []EntryFloat, bufsize int) FloatStream
NewFloatStreamFromSlice creates a new FloatStream from a Go slice of EntryFloat. The stream will be closed once all the slice data has been published.
func (FloatStream) Average ¶
func (is FloatStream) Average() EntryFloat
Average returns the average of the numbers in the stream. Panics if the channel is nil or the stream is empty. This is a special case of a reduction. This is a terminal operation and hence expects the producer to close the stream in order to complete (or it will block).
func (FloatStream) Max ¶
func (is FloatStream) Max() EntryFloat
Max returns the largest number in the stream. Panics if the channel is nil or the stream is empty. This is a special case of a reduction and is equivalent to:
is.Reduce(max) // where max is a BiFunction that returns // the largest of two integers.
This is a terminal operation and hence expects the producer to close the stream in order to complete (or it will block).
func (FloatStream) Min ¶
func (is FloatStream) Min() EntryFloat
Min returns the smallest number in the stream. Panics if the channel is nil or the stream is empty. This is a special case of a reduction and is equivalent to:
is.Reduce(min) // where min is a BiFunction that returns // the smallest of two integers.
This is a terminal operation and hence expects the producer to close the stream in order to complete (or it will block).
func (FloatStream) Sum ¶
func (is FloatStream) Sum() EntryFloat
Sum adds the numbers in the stream. Panics if the channel is nil or the stream is empty. This is a special case of a reduction and is equivalent to:
is.Reduce(sum) // where max is a BiFunction that adds // two integers.
This is a terminal operation and hence expects the producer to close the stream in order to complete (or it will block).
type Function ¶
Function that accepts one argument and produces a result.
Example ¶
ExampleFunction shows how to use Function's. There are more interesting examples through the code. Search for `Function` or the Function signature.
timesTwoFunction := timesTwo() res := timesTwoFunction(ƒ.EntryInt(7)) fmt.Printf("res = %+v\n", res)
Output: res = 14
type Functor ¶
type Functor struct { }
A Functor is a functor TODO: does it make sense to implement this in Golang?
type IntStream ¶
type IntStream struct {
Stream
}
IntStream is a sequence of EntryInt elements supporting sequential and (in the future?) parallel operations.
func NewIntStream ¶
NewIntStream creates a new IntStream. This function leaves the provided channel is the same state of openness.
func NewIntStreamFromSlice ¶
NewIntStreamFromSlice creates a new IntStream from a Go slice of EntryInt. The stream will be closed once all the slice data has been published.
func (IntStream) Average ¶
Average returns the average of the numbers in the stream. Panics if the channel is nil or the stream is empty. This is a special case of a reduction. This is a terminal operation and hence expects the producer to close the stream in order to complete (or it will block).
func (IntStream) Max ¶
Max returns the largest number in the stream. Panics if the channel is nil or the stream is empty. This is a special case of a reduction and is equivalent to:
is.Reduce(max) // where max is a BiFunction that returns // the largest of two integers.
This is a terminal operation and hence expects the producer to close the stream in order to complete (or it will block).
func (IntStream) Min ¶
Min returns the smallest number in the stream. Panics if the channel is nil or the stream is empty. This is a special case of a reduction and is equivalent to:
is.Reduce(min) // where min is a BiFunction that returns // the smallest of two integers.
This is a terminal operation and hence expects the producer to close the stream in order to complete (or it will block).
func (IntStream) Sum ¶
Sum adds the numbers in the stream. Panics if the channel is nil or the stream is empty. This is a special case of a reduction and is equivalent to:
is.Reduce(sum) // where max is a BiFunction that adds // two integers.
This is a terminal operation and hence expects the producer to close the stream in order to complete (or it will block).
type Maybe ¶
type Maybe struct {
// contains filtered or unexported fields
}
A Maybe is a maybe monad.
Example ¶
ExampleMaybe shows ways to use a Maybe.
m1 := ƒ.MaybeOf(EntryString("Hello World")) fmt.Printf("m1.Get=%v\n", m1.Get()) fmt.Printf("m1.GetOrElse=%v\n", m1.GetOrElse(EntryString("Bonjour le monde"))) m2 := ƒ.MaybeOf(nil) if assert.PanicsWithValue(nil, ƒ.PanicNoSuchElement, func() { fmt.Printf("m2.Get=%v\n", m2.Get()) }) { fmt.Println("m2.Get() panics with ƒ.PanicNoSuchElement") } fmt.Printf("m2.GetOrElse=%v\n", m2.GetOrElse(EntryString("Bonjour le monde")))
Output: m1.Get=Hello World m1.GetOrElse=Hello World m2.Get() panics with ƒ.PanicNoSuchElement m2.GetOrElse=Bonjour le monde
func MaybeOf ¶
MaybeOf creates a new Maybe with the given value. If the value is nil then return None otherwise Some(value). Note: MaybeOf(nil) == None() whereas MaybeSome(nil) == MaybeSome(nil).
func MaybeSome ¶
MaybeSome creates a new Maybe with the given value. Note: MaybeOf(nil) == None() whereas MaybeSome(nil) == MaybeSome(nil).
type Predicate ¶
Predicate represents a predicate (boolean-valued function) of one argument.
Example ¶
ExamplePredicate shows how to use and combine Predicates.
package main import ( "fmt" ƒ "github.com/seborama/fuego" ) func main() { res := ƒ.Predicate(ƒ.False).Negate()(ƒ.EntryInt(1)) fmt.Printf("Not False == %+v\n", res) res = ƒ.Predicate(ƒ.True).And(ƒ.False)(ƒ.EntryInt(1)) fmt.Printf("True and False == %+v\n", res) res = ƒ.Predicate(ƒ.True).Or(ƒ.False)(ƒ.EntryInt(1)) fmt.Printf("True or False == %+v\n", res) // You can use associativity too - part 1 of 2: // False And False Or True == true res = ƒ.Predicate(ƒ.False).And(ƒ.False).Or(ƒ.True)(ƒ.EntryInt(1)) fmt.Printf("False And False Or True == %+v\n", res) // You can use associativity too - part 2 of 2: // False And (False Or True) == false res = ƒ.Predicate(ƒ.False).And(ƒ.Predicate(ƒ.False).Or(ƒ.True))(ƒ.EntryInt(1)) fmt.Printf("False And (False Or True) == %+v\n", res) }
Output: Not False == true True and False == false True or False == true False And False Or True == true False And (False Or True) == false
Example (FunctionPredicate) ¶
ExamplePredicate_custom1 shows how to create a custom Predicate using the utility function ƒ.FunctionPredicate().
isEvenNumberPredicate := ƒ.FunctionPredicate(isEvenNumberFunction) res := isEvenNumberPredicate.And(ƒ.True)(ƒ.EntryInt(23)) fmt.Printf("res = %v", res)
Output: res = false
Example (Predicate) ¶
ExamplePredicate_custom2 shows how to create a custom Predicate from scratch. Notice how we get all Predicate helpers (And, Or, Not, etc) for "free".
res := intGreaterThanPredicate(50).And(ƒ.True).Negate()(ƒ.EntryInt(23)) fmt.Printf("res = %v", res)
Output: res = true
func FunctionPredicate ¶
FunctionPredicate creates a Predicate from a Function.
func (Predicate) And ¶
And is a composed predicate that represents a short-circuiting logical AND of this predicate and another.
type Stream ¶
type Stream struct {
// contains filtered or unexported fields
}
Stream is a sequence of elements supporting sequential and (in the future?) parallel operations.
func NewStream ¶
NewStream creates a new Stream. This function leaves the provided channel is the same state of openness.
func NewStreamFromSlice ¶
NewStreamFromSlice creates a new Stream from a Go slice. The slice data is published to the stream after which the stream is closed.
func (Stream) AllMatch ¶
AllMatch returns whether all of the elements in the stream satisfy the predicate. This is a continuous terminal operation and hence expects the producer to close the stream in order to complete (or it will block).
func (Stream) AnyMatch ¶
AnyMatch returns whether any of the elements in the stream satisfies the predicate. This is a continuous terminal operation and hence expects the producer to close the stream in order to complete (or it will block).
func (Stream) Count ¶
Count the number of elements in the stream. This is a special case of a reduction and is equivalent to:
s.MapToInt(func(Entry) { return EntryInt(1) }).Sum()
This is a continuous terminal operation and hence expects the producer to close the stream in order to complete (or it will block).
func (Stream) DropUntil ¶
DropUntil drops the first elements of this stream until the predicate is satisfied and returns a new stream. This function streams continuously until the in-stream is closed at which point the out-stream will be closed too.
func (Stream) DropWhile ¶
DropWhile drops the first elements of this stream while the predicate is satisfied and returns a new stream. This function streams continuously until the in-stream is closed at which point the out-stream will be closed too.
func (Stream) Filter ¶
Filter returns a stream consisting of the elements of this stream that match the given predicate. This function streams continuously until the in-stream is closed at which point the out-stream will be closed too.
func (Stream) ForEach ¶
ForEach executes the given function for each entry in this stream. This is a continuous terminal operation and hence expects the producer to close the stream in order to complete (or it will block).
func (Stream) GroupBy ¶
GroupBy groups the elements of this Stream by classifying them. This is a continuous terminal operation and hence expects the producer to close the stream in order to complete (or it will block).
Example ¶
ExampleStream_GroupBy shows a use of Stream's with GroupBy.
data := []ƒ.Entry{ ƒ.Tuple2{E1: ƒ.EntryInt(1), E2: EntryString("one")}, ƒ.Tuple2{E1: ƒ.EntryInt(2), E2: EntryString("two")}, ƒ.Tuple2{E1: ƒ.EntryInt(3), E2: EntryString("three")}, ƒ.Tuple2{E1: ƒ.EntryInt(4), E2: EntryString("four")}, ƒ.Tuple2{E1: ƒ.EntryInt(5), E2: EntryString("five")}, ƒ.Tuple2{E1: ƒ.EntryInt(6), E2: EntryString("six")}, ƒ.Tuple2{E1: ƒ.EntryInt(7), E2: EntryString("seven")}, ƒ.Tuple2{E1: ƒ.EntryInt(8), E2: EntryString("eight")}, ƒ.Tuple2{E1: ƒ.EntryInt(9), E2: EntryString("nine")}} resMap := map[ƒ.Entry]interface{}{} ƒ.NewStreamFromSlice(data, 0). GroupBy(func(i ƒ.Entry) ƒ.Entry { return i.(ƒ.Tuple2).E1.(ƒ.EntryInt) & 1 }). Stream(0). ForEach(func(e ƒ.Entry) { resMap[e.(ƒ.Tuple2).E1] = e.(ƒ.Tuple2).E2 }) for i := 0; i < len(resMap); i++ { fmt.Printf("%d => %v\n", i, resMap[ƒ.EntryInt(i)]) }
Output: 0 => [{2 two} {4 four} {6 six} {8 eight}] 1 => [{1 one} {3 three} {5 five} {7 seven} {9 nine}]
func (Stream) Intersperse ¶
Intersperse inserts an element between all elements of this Stream. This function streams continuously until the in-stream is closed at which point the out-stream will be closed too.
func (Stream) LeftReduce ¶
func (s Stream) LeftReduce(f2 BiFunction) Entry
LeftReduce accumulates the elements of this Stream by applying the given function. This is a continuous terminal operation and hence expects the producer to close the stream in order to complete (or it will block).
func (Stream) Map ¶
Map returns a slice of channel of Set consisting of the results of applying the given function to the elements of this stream. This function streams continuously until the in-stream is closed at which point the out-stream will be closed too.
func (Stream) MapToInt ¶
func (s Stream) MapToInt(toInt ToIntFunction) IntStream
MapToInt produces an EntryInt stream. This function streams continuously until the in-stream is closed at which point the out-stream will be closed too.
func (Stream) NoneMatch ¶
NoneMatch returns whether none of the elements in the stream satisfies the predicate. It is the opposite of AnyMatch. This is a continuous terminal operation and hence expects the producer to close the stream in order to complete (or it will block).
func (Stream) Reduce ¶
func (s Stream) Reduce(f2 BiFunction) Entry
Reduce is an alias for LeftReduce. See LeftReduce for more info.
func (Stream) StartsWith ¶
StartsWith returns true when this stream starts with the elements in the supplied slice.
func (Stream) Take ¶
Take returns a stream of the first 'n' elements of this stream. This function streams continuously until the 'n' elements are picked or the in-stream is closed at which point the out-stream will be closed too.
type ToIntFunction ¶
ToIntFunction that accepts one argument and produces an EntryInt result.
type Tuple ¶
type Tuple interface { Entry }
A Tuple is a container of value(s). A special case is Tuple0 which does not hold any value.
type Tuple0 ¶
type Tuple0 struct{}
Tuple0 is a tuple with 0 element.
type Tuple1 ¶
type Tuple1 struct {
E1 Entry
}
Tuple1 is a tuple with 1 element.