-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
205 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Package constr provides a set of constraints for Go generics. | ||
package constr | ||
|
||
// Integer represents a type that is a signed or unsigned integer. | ||
type Integer interface { | ||
Signed | Unsigned | ||
} | ||
|
||
// Signed represents a type that is a signed integer. | ||
type Signed interface { | ||
~int | ~int8 | ~int16 | ~int32 | ~int64 | ||
} | ||
|
||
// Unsigned represents a type that is an unsigned integer. | ||
type Unsigned interface { | ||
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ||
} | ||
|
||
// Float represents a type that is a floating-point number. | ||
type Float interface { | ||
~float32 | ~float64 | ||
} | ||
|
||
// Numeric represents a type that is a number. | ||
type Numeric interface { | ||
Integer | Float | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package seq | ||
|
||
import "iter" | ||
|
||
// Select applies the given predicate function to each element of the source | ||
// sequence and returns a new sequence with the elements for which the predicate | ||
// returned true. | ||
func Select[T any](src iter.Seq[T], pred func(T) bool) iter.Seq[T] { | ||
return func(yield func(T) bool) { | ||
for item := range src { | ||
if !pred(item) { | ||
continue | ||
} | ||
if !yield(item) { | ||
return | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package seq_test | ||
|
||
import ( | ||
"slices" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
|
||
"github.com/mokiat/gog/seq" | ||
) | ||
|
||
var _ = Describe("Filter", func() { | ||
|
||
Describe("Select", func() { | ||
isEven := func(value int) bool { | ||
return value%2 == 0 | ||
} | ||
|
||
It("returns only the elements that match the predicate", func() { | ||
source := slices.Values([]int{1, 2, 3, 4, 5}) | ||
target := seq.Select(source, isEven) | ||
items := slices.Collect(target) | ||
Expect(items).To(Equal([]int{2, 4})) | ||
}) | ||
}) | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters