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

chore: sort all functions #27

Merged
merged 5 commits into from
Sep 19, 2023
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
10 changes: 5 additions & 5 deletions channels/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import (
"github.com/life4/genesis/constraints"
)

// Any returns true if f returns true for any element in channel.
func Any[T any](c <-chan T, f func(el T) bool) bool {
return AnyC(context.Background(), c, f)
}

// All is an alias for [AllC] without a context.
func All[T any](c <-chan T, f func(el T) bool) bool {
return AllC(context.Background(), c, f)
}

// Any returns true if f returns true for any element in channel.
func Any[T any](c <-chan T, f func(el T) bool) bool {
return AnyC(context.Background(), c, f)
}

// BufferSize returns how many messages a channel can hold before being blocked.
//
// When you push that many messages in the channel, the next attempt
Expand Down
24 changes: 12 additions & 12 deletions channels/channel_ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,38 @@ import (
"github.com/life4/genesis/constraints"
)

// Any returns true if f returns true for any element in channel.
func AnyC[T any](ctx context.Context, c <-chan T, f func(el T) bool) bool {
// All returns true if f returns true for all elements in channel.
func AllC[T any](ctx context.Context, c <-chan T, f func(el T) bool) bool {
for {
select {
case el, ok := <-c:
if !ok {
return false
}
if f(el) {
return true
}
if !f(el) {
return false

}
case <-ctx.Done():
return false
return true
}
}
}

// All returns true if f returns true for all elements in channel.
func AllC[T any](ctx context.Context, c <-chan T, f func(el T) bool) bool {
// Any returns true if f returns true for any element in channel.
func AnyC[T any](ctx context.Context, c <-chan T, f func(el T) bool) bool {
for {
select {
case el, ok := <-c:
if !ok {
return true
}
if !f(el) {
return false
}
if f(el) {
return true

}
case <-ctx.Done():
return true
return false
}
}
}
Expand Down
124 changes: 62 additions & 62 deletions channels/channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,27 @@ import (
"github.com/matryer/is"
)

func TestToSlice(t *testing.T) {
func TestAll(t *testing.T) {
is := is.New(t)
f := func(given []int) {
f := func(given []int, expected bool) {
even := func(t int) bool { return t%2 == 0 }
c := make(chan int, 1)
go func() {
for _, el := range given {
c <- el
}
close(c)
}()
actual := channels.ToSlice(c)
is.Equal(given, actual)
actual := channels.All(c, even)
is.Equal(expected, actual)
}
f([]int{})
f([]int{1})
f([]int{1, 2, 3, 1, 2})
f([]int{}, true)
f([]int{1}, false)
f([]int{2}, true)
f([]int{1, 2}, false)
f([]int{2, 4}, true)
f([]int{2, 4, 6, 8, 10, 12}, true)
f([]int{2, 4, 6, 8, 11, 12}, false)
}

func TestAny(t *testing.T) {
Expand All @@ -50,68 +55,13 @@ func TestAny(t *testing.T) {
f([]int{1, 3, 5, 7, 10, 11}, true)
}

func TestAll(t *testing.T) {
is := is.New(t)
f := func(given []int, expected bool) {
even := func(t int) bool { return t%2 == 0 }
c := make(chan int, 1)
go func() {
for _, el := range given {
c <- el
}
close(c)
}()
actual := channels.All(c, even)
is.Equal(expected, actual)
}
f([]int{}, true)
f([]int{1}, false)
f([]int{2}, true)
f([]int{1, 2}, false)
f([]int{2, 4}, true)
f([]int{2, 4, 6, 8, 10, 12}, true)
f([]int{2, 4, 6, 8, 11, 12}, false)
}

func TestBufferSize(t *testing.T) {
is := is.New(t)
is.Equal(channels.BufferSize(make(chan int, 3)), 3)
is.Equal(channels.BufferSize(make(chan int)), 0)
is.Equal(channels.BufferSize[int](nil), 0)
}

func TestClose(t *testing.T) {
is := is.New(t)
is.True(!channels.Close[int](nil))
c := make(chan int)
is.True(channels.Close(c))
is.True(!channels.Close(c))
}

func TestEach(t *testing.T) {
is := is.New(t)
f := func(given []int) {
c := make(chan int, 1)
go func() {
for _, el := range given {
c <- el
}
close(c)
}()
result := make(chan int, len(given))
mapper := func(t int) { result <- t }
channels.Each(c, mapper)
close(result)
actual := channels.ToSlice(result)
is.Equal(given, actual)
}

f([]int{})
f([]int{1})
f([]int{1, 2, 3})
f([]int{1, 2, 3, 4, 5, 6, 7})
}

func TestChunkEvery(t *testing.T) {
is := is.New(t)
f := func(size int, given []int, expected [][]int) {
Expand All @@ -136,6 +86,14 @@ func TestChunkEvery(t *testing.T) {
f(2, []int{1, 2, 3, 4, 5}, [][]int{{1, 2}, {3, 4}, {5}})
}

func TestClose(t *testing.T) {
is := is.New(t)
is.True(!channels.Close[int](nil))
c := make(chan int)
is.True(channels.Close(c))
is.True(!channels.Close(c))
}

func TestCount(t *testing.T) {
is := is.New(t)
f := func(element int, given []int, expected int) {
Expand Down Expand Up @@ -181,6 +139,30 @@ func TestDrop(t *testing.T) {
f(1, []int{1, 2, 3, 4, 5, 6}, []int{2, 3, 4, 5, 6})
}

func TestEach(t *testing.T) {
is := is.New(t)
f := func(given []int) {
c := make(chan int, 1)
go func() {
for _, el := range given {
c <- el
}
close(c)
}()
result := make(chan int, len(given))
mapper := func(t int) { result <- t }
channels.Each(c, mapper)
close(result)
actual := channels.ToSlice(result)
is.Equal(given, actual)
}

f([]int{})
f([]int{1})
f([]int{1, 2, 3})
f([]int{1, 2, 3, 4, 5, 6, 7})
}

func TestFilter(t *testing.T) {
is := is.New(t)
f := func(given []int, expected []int) {
Expand Down Expand Up @@ -460,6 +442,24 @@ func TestTee(t *testing.T) {
f(10, []int{1, 2, 3, 1, 2})
}

func TestToSlice(t *testing.T) {
is := is.New(t)
f := func(given []int) {
c := make(chan int, 1)
go func() {
for _, el := range given {
c <- el
}
close(c)
}()
actual := channels.ToSlice(c)
is.Equal(given, actual)
}
f([]int{})
f([]int{1})
f([]int{1, 2, 3, 1, 2})
}

func TestWithBuffer(t *testing.T) {
is := is.New(t)
c1 := make(chan int)
Expand Down
80 changes: 40 additions & 40 deletions lambdas/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,6 @@ func EqualTo[T comparable](a T) func(T) bool {
}
}

// LessThan returns lambda that checks if an item is less than the given value.
func LessThan[T constraints.Ordered](a T) func(T) bool {
return func(b T) bool {
return b < a
}
}

// Not negates the result of a lambda
func Not[T constraints.Ordered](f func(T) bool) func(T) bool {
return func(item T) bool {
return !f(item)
}
}

// IsZero checks if the given number is zero
func IsZero[T Number](n T) bool {
return n == 0
}

// IsNotZero checks if the given number is not zero
func IsNotZero[T Number](n T) bool {
return n != 0
}

// IsEmpty checks if the given slice is empty
func IsEmpty[T any](items []T) bool {
return len(items) == 0
}

// Empty checks if the given slice is not empty
func IsNotEmpty[T any](items []T) bool {
return len(items) != 0
}

// IsDefault checks if the given value is the default for this type.
//
// A few examples:
Expand All @@ -55,6 +21,19 @@ func IsDefault[T comparable](value T) bool {
return value == def
}

// IsEmpty checks if the given slice is empty
func IsEmpty[T any](items []T) bool {
return len(items) == 0
}

func IsNaN[T comparable](value T) bool {
return value != value
}

func IsNil[T any](value *T) bool {
return value == nil
}

// IsNotDefault checks if the given value is not the default for this type.
//
// A few examples:
Expand All @@ -67,18 +46,39 @@ func IsNotDefault[T comparable](value T) bool {
return value != def
}

func IsNaN[T comparable](value T) bool {
return value != value
// Empty checks if the given slice is not empty
func IsNotEmpty[T any](items []T) bool {
return len(items) != 0
}

func IsNotNaN[T comparable](value T) bool {
return value == value
}

func IsNil[T any](value *T) bool {
return value == nil
}

func IsNotNil[T any](value *T) bool {
return value != nil
}

// IsNotZero checks if the given number is not zero
func IsNotZero[T Number](n T) bool {
return n != 0
}

// IsZero checks if the given number is zero
func IsZero[T Number](n T) bool {
return n == 0
}

// LessThan returns lambda that checks if an item is less than the given value.
func LessThan[T constraints.Ordered](a T) func(T) bool {
return func(b T) bool {
return b < a
}
}

// Not negates the result of a lambda
func Not[T constraints.Ordered](f func(T) bool) func(T) bool {
return func(item T) bool {
return !f(item)
}
}
Loading