Skip to content

Commit

Permalink
Update package to v2
Browse files Browse the repository at this point in the history
  • Loading branch information
jpfourny committed Oct 19, 2024
1 parent f2cff28 commit f412b85
Show file tree
Hide file tree
Showing 59 changed files with 312 additions and 156 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

A curated collection of utility functions and types absent from the Go standard library that make life easier.

See documentation: https://pkg.go.dev/github.com/jpfourny/papaya
See documentation: https://pkg.go.dev/github.com/jpfourny/papaya/v2

See tests and examples for usage.

## Installation

`go get github.com/jpfourny/papaya`
`go get github.com/jpfourny/papaya/v2`
2 changes: 1 addition & 1 deletion examples/people.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package examples
import (
"fmt"

"github.com/jpfourny/papaya/pkg/cmp"
"github.com/jpfourny/papaya/v2/pkg/cmp"
)

type Person struct {
Expand Down
6 changes: 3 additions & 3 deletions examples/stream/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package stream

import (
"fmt"
"github.com/jpfourny/papaya/pkg/stream/mapper"
"github.com/jpfourny/papaya/v2/pkg/stream/mapper"
"testing"

"github.com/jpfourny/papaya/pkg/pair"
"github.com/jpfourny/papaya/pkg/stream"
"github.com/jpfourny/papaya/v2/pkg/pair"
"github.com/jpfourny/papaya/v2/pkg/stream"
)

func TestMapIntToString(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions examples/stream/sort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"fmt"
"testing"

"github.com/jpfourny/papaya/examples"
"github.com/jpfourny/papaya/pkg/cmp"
"github.com/jpfourny/papaya/pkg/stream"
"github.com/jpfourny/papaya/v2/examples"
"github.com/jpfourny/papaya/v2/pkg/cmp"
"github.com/jpfourny/papaya/v2/pkg/stream"
)

func TestSort(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/jpfourny/papaya
module github.com/jpfourny/papaya/v2

go 1.23
4 changes: 2 additions & 2 deletions internal/kvstore/kvstore.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package kvstore

import (
"github.com/jpfourny/papaya/pkg/cmp"
"github.com/jpfourny/papaya/pkg/opt"
"github.com/jpfourny/papaya/v2/pkg/cmp"
"github.com/jpfourny/papaya/v2/pkg/opt"
"slices"
)

Expand Down
6 changes: 3 additions & 3 deletions internal/kvstore/kvstore_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package kvstore

import (
"github.com/jpfourny/papaya/internal/assert"
"github.com/jpfourny/papaya/pkg/cmp"
"github.com/jpfourny/papaya/pkg/opt"
"github.com/jpfourny/papaya/v2/internal/assert"
"github.com/jpfourny/papaya/v2/pkg/cmp"
"github.com/jpfourny/papaya/v2/pkg/opt"
"testing"
)

Expand Down
4 changes: 2 additions & 2 deletions pkg/cmp/comparer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
stdcmp "cmp"
"time"

"github.com/jpfourny/papaya/pkg/constraint"
"github.com/jpfourny/papaya/pkg/pair"
"github.com/jpfourny/papaya/v2/pkg/constraint"
"github.com/jpfourny/papaya/v2/pkg/pair"
)

// Comparer is a function that compares two values of the same type E and returns an integer.
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmp/comparer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"testing"
"time"

"github.com/jpfourny/papaya/pkg/pair"
"github.com/jpfourny/papaya/pkg/ptr"
"github.com/jpfourny/papaya/v2/pkg/pair"
"github.com/jpfourny/papaya/v2/pkg/ptr"
)

type Person struct {
Expand Down
10 changes: 5 additions & 5 deletions pkg/env/env.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package env

import (
"github.com/jpfourny/papaya/pkg/constraint"
"github.com/jpfourny/papaya/pkg/opt"
"github.com/jpfourny/papaya/pkg/pair"
"github.com/jpfourny/papaya/pkg/stream"
"github.com/jpfourny/papaya/pkg/stream/mapper"
"github.com/jpfourny/papaya/v2/pkg/constraint"
"github.com/jpfourny/papaya/v2/pkg/opt"
"github.com/jpfourny/papaya/v2/pkg/pair"
"github.com/jpfourny/papaya/v2/pkg/stream"
"github.com/jpfourny/papaya/v2/pkg/stream/mapper"
"os"
"strings"
"time"
Expand Down
4 changes: 2 additions & 2 deletions pkg/env/env_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package env

import (
"github.com/jpfourny/papaya/pkg/pair"
"github.com/jpfourny/papaya/pkg/stream"
"github.com/jpfourny/papaya/v2/pkg/pair"
"github.com/jpfourny/papaya/v2/pkg/stream"
"os"
"testing"
"time"
Expand Down
2 changes: 1 addition & 1 deletion pkg/ptr/pointer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ptr

import "github.com/jpfourny/papaya/pkg/opt"
import "github.com/jpfourny/papaya/v2/pkg/opt"

// Nil returns a nil pointer to the type parameter V.
func Nil[V any]() *V {
Expand Down
2 changes: 1 addition & 1 deletion pkg/res/failure.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package res

import (
"fmt"
"github.com/jpfourny/papaya/pkg/opt"
"github.com/jpfourny/papaya/v2/pkg/opt"
)

// Failure represents a failed result.
Expand Down
2 changes: 1 addition & 1 deletion pkg/res/partial_success.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package res

import (
"fmt"
"github.com/jpfourny/papaya/pkg/opt"
"github.com/jpfourny/papaya/v2/pkg/opt"
)

// PartialSuccess represents a partially-successful result.
Expand Down
2 changes: 1 addition & 1 deletion pkg/res/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package res

import (
"fmt"
"github.com/jpfourny/papaya/pkg/opt"
"github.com/jpfourny/papaya/v2/pkg/opt"
)

// Result represents the result of an operation that may have a value and/or an error.
Expand Down
2 changes: 1 addition & 1 deletion pkg/res/success.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package res

import (
"fmt"
"github.com/jpfourny/papaya/pkg/opt"
"github.com/jpfourny/papaya/v2/pkg/opt"
)

// Success represents a successful result.
Expand Down
10 changes: 5 additions & 5 deletions pkg/stream/aggregate.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package stream

import (
"github.com/jpfourny/papaya/pkg/cmp"
"github.com/jpfourny/papaya/pkg/constraint"
"github.com/jpfourny/papaya/pkg/opt"
"github.com/jpfourny/papaya/pkg/stream/mapper"
"github.com/jpfourny/papaya/pkg/stream/reducer"
"github.com/jpfourny/papaya/v2/pkg/cmp"
"github.com/jpfourny/papaya/v2/pkg/constraint"
"github.com/jpfourny/papaya/v2/pkg/opt"
"github.com/jpfourny/papaya/v2/pkg/stream/mapper"
"github.com/jpfourny/papaya/v2/pkg/stream/reducer"
)

// Reducer represents a function that takes two inputs of type E and returns an output of type E.
Expand Down
4 changes: 2 additions & 2 deletions pkg/stream/aggregate_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package stream

import (
"github.com/jpfourny/papaya/pkg/cmp"
"github.com/jpfourny/papaya/v2/pkg/cmp"
"testing"

"github.com/jpfourny/papaya/pkg/opt"
"github.com/jpfourny/papaya/v2/pkg/opt"
)

func TestReduce(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/stream/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package stream

import (
"context"
"github.com/jpfourny/papaya/pkg/stream/mapper"
"github.com/jpfourny/papaya/v2/pkg/stream/mapper"

"github.com/jpfourny/papaya/pkg/pair"
"github.com/jpfourny/papaya/v2/pkg/pair"
)

// CollectSlice returns a slice containing all elements from the stream.
Expand Down
4 changes: 2 additions & 2 deletions pkg/stream/collect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"context"
"testing"

"github.com/jpfourny/papaya/internal/assert"
"github.com/jpfourny/papaya/pkg/pair"
"github.com/jpfourny/papaya/v2/internal/assert"
"github.com/jpfourny/papaya/v2/pkg/pair"
)

func TestCollectSlice(t *testing.T) {
Expand Down
10 changes: 5 additions & 5 deletions pkg/stream/combine.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package stream

import (
"context"
"github.com/jpfourny/papaya/pkg/constraint"
"github.com/jpfourny/papaya/pkg/opt"
"github.com/jpfourny/papaya/pkg/pair"
"github.com/jpfourny/papaya/pkg/stream/mapper"
"github.com/jpfourny/papaya/pkg/stream/pred"
"github.com/jpfourny/papaya/v2/pkg/constraint"
"github.com/jpfourny/papaya/v2/pkg/opt"
"github.com/jpfourny/papaya/v2/pkg/pair"
"github.com/jpfourny/papaya/v2/pkg/stream/mapper"
"github.com/jpfourny/papaya/v2/pkg/stream/pred"
)

// Combiner represents a function that combines two elements of type E1 and E2 into an element of type F.
Expand Down
6 changes: 3 additions & 3 deletions pkg/stream/combine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"fmt"
"testing"

"github.com/jpfourny/papaya/internal/assert"
"github.com/jpfourny/papaya/pkg/opt"
"github.com/jpfourny/papaya/pkg/pair"
"github.com/jpfourny/papaya/v2/internal/assert"
"github.com/jpfourny/papaya/v2/pkg/opt"
"github.com/jpfourny/papaya/v2/pkg/pair"
)

func TestCombine(t *testing.T) {
Expand Down
18 changes: 18 additions & 0 deletions pkg/stream/consumer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package stream

// Consumer represents a function that accepts a yielded element of type E and returns a boolean value.
// The boolean value indicates whether the consumer wishes to continue accepting elements.
// If the consumer returns false, the caller must stop yielding elements.
type Consumer[E any] func(yield E) (cont bool)

func stopSensingConsumer[E any](c Consumer[E]) (Consumer[E], *bool) {
stopped := new(bool)
c2 := func(e E) bool {
if c(e) {
return true
}
*stopped = true
return false
}
return c2, stopped
}
44 changes: 44 additions & 0 deletions pkg/stream/consumer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package stream

import (
"slices"
"testing"
)

func Test_stopSensingConsumer(t *testing.T) {
t.Run("stream-exhausted", func(t *testing.T) {
s := Of(1, 2, 3)
var saw []int
c1 := func(e int) bool {
saw = append(saw, e)
return true
}
c2, stopped := stopSensingConsumer(c1)
s(c2)
if *stopped {
t.Errorf("expected stopped to be false; got true")
}
want := []int{1, 2, 3}
if !slices.Equal(saw, want) {
t.Errorf("expected to see %v; got %v", want, saw)
}
})

t.Run("consumer-stopped", func(t *testing.T) {
s := Of(1, 2, 3)
var saw []int
c1 := func(e int) bool {
saw = append(saw, e)
return false
}
c2, stopped := stopSensingConsumer(c1)
s(c2)
if !*stopped {
t.Errorf("expected stopped to be true; got false")
}
want := []int{1}
if !slices.Equal(saw, want) {
t.Errorf("expected to see %v; got %v", want, saw)
}
})
}
6 changes: 3 additions & 3 deletions pkg/stream/filter.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package stream

import (
"github.com/jpfourny/papaya/internal/kvstore"
"github.com/jpfourny/papaya/pkg/cmp"
"github.com/jpfourny/papaya/pkg/pair"
"github.com/jpfourny/papaya/v2/internal/kvstore"
"github.com/jpfourny/papaya/v2/pkg/cmp"
"github.com/jpfourny/papaya/v2/pkg/pair"
)

// Predicate is a function that accepts a value of type E and returns a boolean.
Expand Down
6 changes: 3 additions & 3 deletions pkg/stream/filter_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package stream

import (
"github.com/jpfourny/papaya/pkg/cmp"
"github.com/jpfourny/papaya/pkg/pair"
"github.com/jpfourny/papaya/v2/pkg/cmp"
"github.com/jpfourny/papaya/v2/pkg/pair"
"testing"

"github.com/jpfourny/papaya/internal/assert"
"github.com/jpfourny/papaya/v2/internal/assert"
)

func TestFilter(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/stream/from.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package stream
import (
"context"

"github.com/jpfourny/papaya/pkg/pair"
"github.com/jpfourny/papaya/v2/pkg/pair"
)

// FromSlice creates a stream that iterates over the elements of the given slice.
Expand Down
4 changes: 2 additions & 2 deletions pkg/stream/from_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"sync"
"testing"

"github.com/jpfourny/papaya/internal/assert"
"github.com/jpfourny/papaya/pkg/pair"
"github.com/jpfourny/papaya/v2/internal/assert"
"github.com/jpfourny/papaya/v2/pkg/pair"
)

func TestFromSlice(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/stream/generate_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package stream

import (
"github.com/jpfourny/papaya/internal/assert"
"github.com/jpfourny/papaya/v2/internal/assert"
"math/rand"
"testing"
)
Expand Down
10 changes: 5 additions & 5 deletions pkg/stream/group.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package stream

import (
"github.com/jpfourny/papaya/internal/kvstore"
"github.com/jpfourny/papaya/pkg/cmp"
"github.com/jpfourny/papaya/pkg/constraint"
"github.com/jpfourny/papaya/pkg/pair"
"github.com/jpfourny/papaya/pkg/stream/mapper"
"github.com/jpfourny/papaya/v2/internal/kvstore"
"github.com/jpfourny/papaya/v2/pkg/cmp"
"github.com/jpfourny/papaya/v2/pkg/constraint"
"github.com/jpfourny/papaya/v2/pkg/pair"
"github.com/jpfourny/papaya/v2/pkg/stream/mapper"
)

// GroupByKey returns a stream that values key-value pairs by key.
Expand Down
Loading

0 comments on commit f412b85

Please sign in to comment.