Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1296,7 +1296,6 @@ func ExampleQuery_Take() {
fmt.Printf("The top three grades are: %v", topThreeGrades)
// Output:
// The top three grades are: [98 92 85]

}

// The following code example demonstrates how to use TakeWhile
Expand Down Expand Up @@ -1354,6 +1353,22 @@ func ExampleQuery_ToChannel() {
// 10
}

// The following code example demonstrates how to use ToChannelT
// to send a slice to a typed channel.
func ExampleQuery_ToChannelT() {
c := make(chan string)

go Repeat("ten", 3).ToChannelT(c)

for i := range c {
fmt.Println(i)
}
// Output:
// ten
// ten
// ten
}

// The following code example demonstrates how to use ToMap to populate a map.
func ExampleQuery_ToMap() {
type Product struct {
Expand Down
16 changes: 16 additions & 0 deletions result.go
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,22 @@ func (q Query) ToChannel(result chan<- interface{}) {
close(result)
}

// ToChannelT is the typed version of ToChannel.
//
// - result is of type "chan TSource"
//
// NOTE: ToChannel has better performance than ToChannelT.
func (q Query) ToChannelT(result interface{}) {
r := reflect.ValueOf(result)
next := q.Iterate()

for item, ok := next(); ok; item, ok = next() {
r.Send(reflect.ValueOf(item))
}

r.Close()
}

// ToMap iterates over a collection and populates result map with elements.
// Collection elements have to be of KeyValue type to use this method. To
// populate a map with elements of different type use ToMapBy method. ToMap
Expand Down
16 changes: 16 additions & 0 deletions result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,22 @@ func TestToChannel(t *testing.T) {
}
}

func TestToChannelT(t *testing.T) {
c := make(chan string)
input := []string{"1", "2", "3", "4", "5"}

go From(input).ToChannelT(c)

result := []string{}
for value := range c {
result = append(result, value)
}

if !reflect.DeepEqual(result, input) {
t.Errorf("From(%v).ToChannelT()=%v expected %v", input, result, input)
}
}

func TestToMap(t *testing.T) {
input := make(map[int]bool)
input[1] = true
Expand Down