Documentation ¶
Index ¶
- Constants
- func False(t Entry) bool
- func True(t Entry) bool
- type BiFunction
- type Consumer
- type Entry
- type EntryBool
- type EntryMap
- type EntrySlice
- type Function
- type Functor
- type Maybe
- type Predicate
- type Stream
- func (s Stream) Filter(predicate Predicate) Stream
- func (s Stream) ForEach(consumer Consumer)
- func (s Stream) GroupBy(classifier Function) EntryMap
- func (s Stream) Intersperse(e Entry) Stream
- func (s Stream) LeftReduce(f2 BiFunction) Entry
- func (s Stream) Map(mapper Function) Stream
- func (s Stream) Reduce(f2 BiFunction) Entry
- type Tuple
- type Tuple0
- type Tuple1
- type Tuple2
Examples ¶
Constants ¶
const PanicNoSuchElement = "No such element"
PanicNoSuchElement signifies that the iterator does not have the requested element.
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). 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 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 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 Maybe ¶
type Maybe struct {
// contains filtered or unexported fields
}
A Maybe is a maybe monad.
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.
res := ƒ.Predicate(ƒ.False).Not()(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 fuego.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).Not()(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 parallel parallel operations.
func NewStreamFromSlice ¶
NewStreamFromSlice creates a new Stream from a Go slice.
func (Stream) Filter ¶
Filter returns a stream consisting of the elements of this stream that match the given predicate.
func (Stream) GroupBy ¶
GroupBy groups the elements of this Stream by classifying them.
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). GroupBy(func(i ƒ.Entry) ƒ.Entry { return i.(ƒ.Tuple2).E1.(EntryInt) & 1 }). Stream(). 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.
func (Stream) LeftReduce ¶
func (s Stream) LeftReduce(f2 BiFunction) Entry
LeftReduce accumulates the elements of this Set by applying the given function.
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 Set
func (Stream) Reduce ¶
func (s Stream) Reduce(f2 BiFunction) Entry
Reduce is an alias for LeftReduce.
type Tuple ¶
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.