Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add channel support #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ func Slice[T any](slice []T) Iterator[T]

`Slice` returns an Iterator that yields elements from a slice.

```go
func Chan[T any](ch <-chan T) Iterator[T]
```

`Chan` returns an Iterator that yields elements from a channel.

```go
func String(input string) Iterator[rune]
```
Expand Down Expand Up @@ -151,6 +157,12 @@ func ToSlice[T any](it Iterator[T]) []T

`ToSlice` consumes an Iterator creating a slice from the yielded values.

```go
func ToChan[T any](it Iterator[T]) <-chan T
```

`ToChan` consumes an Iterator writing the yielded values to a channel.

```go
func ToString(it Iterator[rune]) string
```
Expand Down
39 changes: 38 additions & 1 deletion iterator.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package iter

import "unicode/utf8"
import (
"unicode/utf8"
)

// Iterator[T] represents an iterator yielding elements of type T.
type Iterator[T any] interface {
Expand Down Expand Up @@ -77,6 +79,25 @@ func (it *sliceIter[T]) Next() Option[T] {
return Some[T](first)
}

type chanIter[T any] struct {
ch <-chan T
}

// Chan returns an Iterator that yields elements written to a channel.
func Chan[T any](ch <-chan T) Iterator[T] {
return &chanIter[T]{
ch: ch,
}
}

func (c *chanIter[T]) Next() Option[T] {
v, ok := <-c.ch
if !ok {
return None[T]()
}
return Some(v)
}

// ToSlice consumes an Iterator creating a slice from the yielded values.
func ToSlice[T any](it Iterator[T]) []T {
result := []T{}
Expand All @@ -91,6 +112,22 @@ func ToString(it Iterator[rune]) string {
return string(ToSlice(it))
}

// ToChan consumes an Iterator writing yielded values to the returned channel.
func ToChan[T any](it Iterator[T]) <-chan T {
ch := make(chan T)
go func() {
defer func() {
if r := recover(); r != nil {
}
}()
ForEach(it, func(v T) {
ch <- v
})
close(ch)
}()
return ch
}

type mapIter[T, R any] struct {
inner Iterator[T]
fn func(T) R
Expand Down
23 changes: 23 additions & 0 deletions iterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,22 @@ func TestSlice(t *testing.T) {
equals(t, it.Next().IsNone(), true)
}

func TestChan(t *testing.T) {
ch := make(chan int)
arr := []int{1, 2, 3}
go func() {
for _, v := range arr {
ch <- v
}
close(ch)
}()
it := Chan(ch)
equals(t, it.Next().Unwrap(), 1)
equals(t, it.Next().Unwrap(), 2)
equals(t, it.Next().Unwrap(), 3)
equals(t, it.Next().IsNone(), true)
}

func TestRepeat(t *testing.T) {
it := Repeat(5)
equals(t, it.Next().Unwrap(), 5)
Expand Down Expand Up @@ -75,6 +91,13 @@ func TestToSlice(t *testing.T) {
equals(t, slice2, []int{})
}

func TestToChan(t *testing.T) {
slice1 := ToSlice(Chan(ToChan(Take(Repeat(5), 3))))
equals(t, slice1, []int{5, 5, 5})
slice2 := ToSlice(Chan(ToChan(Empty[int]())))
equals(t, slice2, []int{})
}

func TestDrop(t *testing.T) {
slice1 := ToSlice(Drop(Slice([]int{1, 2, 3, 4, 5}), 3))
equals(t, slice1, []int{4, 5})
Expand Down