Skip to content

Commit

Permalink
Documentation + FlattenEntrySliceToEntry
Browse files Browse the repository at this point in the history
  • Loading branch information
seborama committed Feb 21, 2019
1 parent 0a1b583 commit de7a926
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 8 deletions.
46 changes: 46 additions & 0 deletions README.md
Expand Up @@ -135,6 +135,16 @@ A `Function` is a normal Go function which signature is:
func(i Entry) Entry
```

#### StreamFunction

`StreamFunction` accepts one argument and produces a stream.

This effectively is a one to many operation, such as exploding the individual values of an EntrySlice into a Stream.

##### FlattenEntrySliceToEntry

FlattenEntrySliceToEntry is an example of `StreamFunction` which flattens an `EntrySlice` to a `Stream` of its elements.

#### BiFunction

A `BiFunction` is a normal Go function which signature is:
Expand Down Expand Up @@ -189,6 +199,8 @@ NewStream(c)

#### Filter

Filter returns a stream consisting of the elements of this stream that match the given predicate.

```go
// See helpers_test.go for "newEntryIntEqualsTo()"
s := ƒ.NewStreamFromSlice([]ƒ.Entry{
Expand Down Expand Up @@ -259,6 +271,36 @@ Please refer to [stream_test.go](stream_test.go) for an example that groups numb

Counts the number of elements in the Stream.

#### Map

Map returns a `Stream` consisting of the result of applying the given function to the elements of this stream.

#### FlatMap

FlatMap takes a `StreamFunction` to flatten the entries in this stream and produce a new stream.

Example:

```go
a := EntrySlice{EntryInt(1), EntryInt(2), EntryInt(3)}
b := EntrySlice{EntryInt(4), EntryInt(5)}
c := EntrySlice{EntryInt(6), EntryInt(7), EntryInt(8)}

sliceOfEntrySlicesOfEntryInts := EntrySlice{a, b, c}

fmt.Printf("Before flattening: %+v\n", sliceOfEntrySlicesOfEntryInts)

sliceOfEntryInts := NewStreamFromSlice(sliceOfEntrySlicesOfEntryInts, 0).
FlatMap(FlattenEntrySliceToEntry(0)).
Collect(ToEntrySlice())

fmt.Printf("After flattening: %+v\n", sliceOfEntryInts)

// Output:
// Before flattening: [[1 2 3] [4 5] [6 7 8]]
// After flattening: [1 2 3 4 5 6 7 8]
```

#### MapToInt

Maps this stream to an `IntStream`.
Expand Down Expand Up @@ -521,6 +563,10 @@ Example:
// map[1:[] 2:[BB CC] 3:[DDD]]
```

#### ToSlice

ToSlice extracts the elements of the stream into an EntrySlice.

### IntStream

A Stream of EntryInt.
Expand Down
2 changes: 1 addition & 1 deletion entryslice.go
Expand Up @@ -35,7 +35,7 @@ func (es EntrySlice) Equal(e Entry) bool {

// Append an Entry to this EntrySlice
func (es EntrySlice) Append(e Entry) EntrySlice {
return EntrySlice(append(es, e))
return append(es, e)
}

// Len returns the number of Entries in this EntrySlice.
Expand Down
4 changes: 1 addition & 3 deletions example_stream_test.go
Expand Up @@ -48,9 +48,7 @@ func ExampleStream_FlatMap() {
fmt.Printf("Before flattening: %+v\n", sliceOfEntrySlicesOfEntryInts)

sliceOfEntryInts := NewStreamFromSlice(sliceOfEntrySlicesOfEntryInts, 0).
FlatMap(func(e Entry) Stream {
return NewStreamFromSlice(e.(EntrySlice), 0)
}).
FlatMap(FlattenEntrySliceToEntry(0)).
Collect(ToEntrySlice())

fmt.Printf("After flattening: %+v\n", sliceOfEntryInts)
Expand Down
10 changes: 9 additions & 1 deletion function.go
Expand Up @@ -3,7 +3,7 @@ package fuego
// Function that accepts one argument and produces a result.
type Function func(e Entry) Entry

// StreamFunction that accepts on argument and produces a stream.
// StreamFunction that accepts one argument and produces a stream.
// This effectively is a one to many operation, such as exploding
// the individual values of an EntrySlice into a Stream.
type StreamFunction func(e Entry) Stream
Expand All @@ -22,3 +22,11 @@ type ToIntFunction func(e Entry) EntryInt

// ToFloatFunction that accepts one argument and produces an EntryFloat result.
type ToFloatFunction func(e Entry) EntryFloat

// FlattenEntrySliceToEntry is a StreamFunction that flattens
// an EntrySlice to a Stream of its elements.
func FlattenEntrySliceToEntry(bufsize int) StreamFunction {
return func(e Entry) Stream {
return NewStreamFromSlice(e.(EntrySlice), bufsize)
}
}
35 changes: 35 additions & 0 deletions function_test.go
@@ -0,0 +1,35 @@
package fuego

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestFlattenEntrySliceToEntry(t *testing.T) {
tests := []struct {
name string
input Entry
want EntrySlice
}{
{
name: "Should flatten EntrySlice to its elements",
input: EntrySlice{
EntryInt(1),
EntryInt(3),
EntryInt(5),
},
want: EntrySlice{
EntryInt(1),
EntryInt(3),
EntryInt(5),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := FlattenEntrySliceToEntry(0)(tt.input).ToSlice()
assert.EqualValues(t, tt.want, got)
})
}
}
4 changes: 1 addition & 3 deletions stream.go
Expand Up @@ -13,10 +13,8 @@ package fuego
// Limit(uint64) - Returns a Stream consisting of at most n elements.
// MapToString(ToStringFunction)
// FindAny / FindFirst?
// FlatMap
// FlatMapToXXX (Int, Uint, etc) => is this the same as FlatMap().MapToXXX()?
// **Sorted(Comparator)
// ToSlice
// Contains
// ContainsAll
// Tail
Expand Down Expand Up @@ -69,7 +67,7 @@ func NewStreamFromSlice(slice EntrySlice, bufsize int) Stream {

// TODO: implement NewStreamFromMap -> Stream of Keys / Stream of Values?

// Map returns a Stream consisting of the results of
// Map returns a Stream consisting of the result 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.
Expand Down

0 comments on commit de7a926

Please sign in to comment.