Skip to content

Commit

Permalink
Match naming conventions for Seq on Count (-> NaturalNumbers)
Browse files Browse the repository at this point in the history
  • Loading branch information
BooleanCat committed Jul 17, 2024
1 parent ac11494 commit e2bf45f
Show file tree
Hide file tree
Showing 12 changed files with 113 additions and 80 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ A library of iterators for use with `iter.Seq`. Requires Go 1.23+.
```go
// The first 5 natural numbers
numbers := slices.Collect(
it.Take(it.Count[int](), 5),
it.Take(it.NaturalNumbers[int](), 5),
)

// All even numbers
evens := it.Filter(it.Count[int](), filter.IsEven)
evens := it.Filter(it.NaturalNumbers[int](), filter.IsEven)

// String representations of integers
numbers := it.Map(it.Count[int](), strconv.Itoa)
numbers := it.Map(it.NaturalNumbers[int](), strconv.Itoa)
```

_[Read the docs](https://pkg.go.dev/github.com/BooleanCat/go-functional/v2)_ to see the full iterator library.
Expand All @@ -38,7 +38,7 @@ Let's take a look at an example:

```go
// The first 10 odd integers
itx.Count[int]().Filter(filter.IsOdd).Take(10).Collect()
itx.NaturalNumbers[int]().Filter(filter.IsOdd).Take(10).Collect()
```

Most iterators support chaining. A notable exception is `it.Map` which cannot
Expand Down
2 changes: 1 addition & 1 deletion it/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestChainMany(t *testing.T) {

numbers := slices.Collect(it.Chain(
slices.Values([]int{1, 2}),
it.Take(it.Drop(it.Count[int](), 3), 2),
it.Take(it.Drop(it.NaturalNumbers[int](), 3), 2),
slices.Values([]int{5, 6}),
))

Expand Down
17 changes: 0 additions & 17 deletions it/count.go

This file was deleted.

31 changes: 0 additions & 31 deletions it/count_test.go

This file was deleted.

28 changes: 28 additions & 0 deletions it/integers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package it

import "iter"

// Integers yields all integers in the range [start, stop) with the given step.
func Integers[V ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr | ~int | ~int8 | ~int16 | ~int32 | ~int64](start, stop, step V) iter.Seq[V] {
return func(yield func(V) bool) {
for i := start; i < stop; i += step {
if !yield(i) {
return
}
}
}
}

// NaturalNumbers yields all non-negative integers in ascending order.
func NaturalNumbers[V ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr | ~int | ~int8 | ~int16 | ~int32 | ~int64]() iter.Seq[V] {
return func(yield func(V) bool) {
var i V

for {
if !yield(i) {
return
}
i++
}
}
}
44 changes: 44 additions & 0 deletions it/integers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package it_test

import (
"fmt"
"testing"

"github.com/BooleanCat/go-functional/v2/it"
)

func ExampleNaturalNumbers() {
for i := range it.NaturalNumbers[int]() {
if i >= 3 {
break
}

fmt.Println(i)
}

// Output:
// 0
// 1
// 2
}

func ExampleIntegers() {
for i := range it.Integers[uint](0, 3, 1) {
fmt.Println(i)
}

// Output:
// 0
// 1
// 2
}

func TestIntegersYieldFalse(t *testing.T) {
t.Parallel()

numbers := it.Integers[uint](0, 3, 1)

numbers(func(value uint) bool {
return false
})
}
10 changes: 0 additions & 10 deletions it/itx/count.go

This file was deleted.

12 changes: 0 additions & 12 deletions it/itx/count_test.go

This file was deleted.

13 changes: 13 additions & 0 deletions it/itx/integers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package itx

import "github.com/BooleanCat/go-functional/v2/it"

// Integers yields all integers in the range [start, stop) with the given step.
func Integers[V ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr | ~int | ~int8 | ~int16 | ~int32 | ~int64](start, stop, step V) Iterator[V] {
return Iterator[V](it.Integers[V](start, stop, step))
}

// NaturalNumbers yields all non-negative integers in ascending order.
func NaturalNumbers[V ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr | ~int | ~int8 | ~int16 | ~int32 | ~int64]() Iterator[V] {
return Iterator[V](it.NaturalNumbers[V]())
}
17 changes: 17 additions & 0 deletions it/itx/integers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package itx_test

import (
"fmt"

"github.com/BooleanCat/go-functional/v2/it/itx"
)

func ExampleNaturalNumbers() {
fmt.Println(itx.NaturalNumbers[int]().Take(4).Collect())
// Output: [0 1 2 3]
}

func ExampleIntegers() {
fmt.Println(itx.Integers[uint](0, 3, 1).Collect())
// Output: [0 1 2]
}
2 changes: 1 addition & 1 deletion it/itx/iter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func ExampleIterator_Seq() {
fmt.Println(slices.Collect(itx.Count[int]().Take(3).Seq()))
fmt.Println(slices.Collect(itx.NaturalNumbers[int]().Take(3).Seq()))
// Output: [0 1 2]
}

Expand Down
9 changes: 5 additions & 4 deletions it/itx/zip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package itx_test
import (
"fmt"
"maps"
"strings"
"testing"

"github.com/BooleanCat/go-functional/v2/internal/assert"
Expand All @@ -29,10 +30,10 @@ func TestUnzipMethod(t *testing.T) {
}

func ExampleIterator2_Left() {
for key := range itx.From2(maps.All(map[int]string{1: "one"})).Left() {
fmt.Println(key)
}
// Output: 1
text := strings.NewReader("one\ntwo\nthree\n")

fmt.Println(itx.LinesString(text).Left().Collect())
// Output: [one two three]
}

func ExampleIterator2_Right() {
Expand Down

0 comments on commit e2bf45f

Please sign in to comment.