Skip to content

Commit

Permalink
Merge pull request #9 from koss-null/v1.0.2
Browse files Browse the repository at this point in the history
V1.0.2
  • Loading branch information
koss-null committed Jul 29, 2023
2 parents 277ba32 + 1a00125 commit 1aafde3
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 3 deletions.
Binary file modified coverage_badge.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions internal/internalpipe/constructor.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ func FuncP[T any](fn func(i int) (*T, bool)) Pipe[T] {
}
}

func Cycle[T any](a []T) Pipe[T] {
if len(a) == 0 {
return Func(func(_ int) (x T, exist bool) {
return
})
}

return Func(func(i int) (T, bool) {
return a[i%len(a)], true
})
}

func Range[T constraints.Integer | constraints.Float](start, finish, step T) Pipe[T] {
if start >= finish {
return Pipe[T]{
Expand Down
47 changes: 47 additions & 0 deletions internal/internalpipe/constructor_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package internalpipe

import (
"strings"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -27,6 +28,52 @@ func Test_FuncP(t *testing.T) {
require.Equal(t, p.GoroutinesCnt, defaultParallelWrks)
}

func Not[T any](fn func(x T) bool) func(T) bool {
return func(x T) bool {
return !fn(x)
}
}

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

isVowel := func(s *string) bool {
st := struct{}{}
_, ok := map[string]struct{}{"a": st, "e": st, "i": st, "o": st, "u": st, "y": st}[*s]
return ok
}

t.Run("happy", func(t *testing.T) {
p := Cycle([]string{"a", "b", "c", "d"})
require.Equal(
t,
[]string{"A", "A", "A", "A", "A", "A", "A", "A", "A", "A"},
p.Filter(isVowel).Map(strings.ToUpper).Take(10).Do(),
)
})

t.Run("empty res", func(t *testing.T) {
p := Cycle([]string{"a", "b", "c", "d"})
require.Equal(
t,
[]string{},
p.Filter(isVowel).Filter(Not(isVowel)).Gen(10).Do(),
)
})

t.Run("empty cycle", func(t *testing.T) {
p := Cycle([]string{})
require.Equal(
t,
[]string{},
p.Filter(isVowel).
Map(strings.TrimSpace).
Gen(10).
Do(),
)
})
}

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

Expand Down
4 changes: 1 addition & 3 deletions pkg/pipe/constructors.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ func FuncP[T any](fn func(i int) (*T, bool)) PiperNoLen[T] {
// Use the 'Take' or 'Gen' functions to set the number of output values to generate,
// or use the 'Until' function to enforce a limit based on a predicate function.
func Cycle[T any](a []T) PiperNoLen[T] {
return Fn(func(i int) T {
return a[i%len(a)]
})
return &PipeNL[T]{internalpipe.Cycle(a)}
}

// Range creates a lazy sequence of type 'T', which consists of values
Expand Down
25 changes: 25 additions & 0 deletions pkg/pipies/not.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package pipies

// This madness is the only way to implement Not function for a function with arbitary amount of arguments.
// The amount of t at the end of Not(t) function equals to the amount of arguments the initial function takes

// Not returns a new function that negates the result of the input function.
func Not[T any](fn func(x T) bool) func(T) bool {
return func(x T) bool {
return !fn(x)
}
}

// Nott returns a new function that negates the result of the input function.
func Nott[T1, T2 any](fn func(x T1, y T2) bool) func(T1, T2) bool {
return func(x T1, y T2) bool {
return !fn(x, y)
}
}

// Nottt returns a new function that negates the result of the input function.
func Nottt[T1, T2, T3 any](fn func(x T1, y T2, z T3) bool) func(T1, T2, T3) bool {
return func(x T1, y T2, z T3) bool {
return !fn(x, y, z)
}
}
10 changes: 10 additions & 0 deletions pkg/pipies/pipies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,13 @@ func Test_Accum(t *testing.T) {
require.Equal(t, Sum(pointer.To(10), pointer.To(20)), 30)
require.Equal(t, Sum(pointer.To(10.0), pointer.To(20.0)), 30.0)
}

func Test_Not(t *testing.T) {
require.Equal(t, Not(func(a bool) bool { return a })(true), false)
require.Equal(t, Nott(func(a, b bool) bool { return a && b })(true, true), false)
require.Equal(t, Nottt(func(a, b, c bool) bool { return a && b && c })(true, true, true), false)

require.Equal(t, Not(func(a bool) bool { return a })(false), true)
require.Equal(t, Nott(func(a, b bool) bool { return a && b })(false, false), true)
require.Equal(t, Nottt(func(a, b, c bool) bool { return a && b && c })(false, false, false), true)
}

0 comments on commit 1aafde3

Please sign in to comment.