diff --git a/doc.go b/doc.go index 9c71d58..1902f7f 100644 --- a/doc.go +++ b/doc.go @@ -1,6 +1,2 @@ -// go-slices provides generic functions to common collection operations +// go-slices provides generic functions for working with slices package goslices - -import ( - _ "github.com/skippyza/go-slices/slices" -) diff --git a/slices/errors.go b/errors.go similarity index 79% rename from slices/errors.go rename to errors.go index fb834c6..051f8f2 100644 --- a/slices/errors.go +++ b/errors.go @@ -1,4 +1,4 @@ -package slices +package goslices import "errors" diff --git a/slices/example_test.go b/example_test.go similarity index 61% rename from slices/example_test.go rename to example_test.go index 5f36caa..41eea53 100644 --- a/slices/example_test.go +++ b/example_test.go @@ -1,18 +1,18 @@ -package slices_test +package goslices_test import ( "fmt" "strconv" - "github.com/skippyza/go-slices/slices" + goslices "github.com/skippyza/go-slices" ) func ExampleAll() { isEven := func(i int) bool { return i%2 == 0 } - result := slices.All([]int{2, 4, 6, 8, 10}, isEven) + result := goslices.All([]int{2, 4, 6, 8, 10}, isEven) fmt.Println(result) - result = slices.All([]int{1, 2, 4, 6, 8, 9, 10}, isEven) + result = goslices.All([]int{1, 2, 4, 6, 8, 9, 10}, isEven) fmt.Println(result) // Output: @@ -23,9 +23,9 @@ func ExampleAll() { func ExampleAny() { isEven := func(i int) bool { return i%2 == 0 } - result := slices.Any([]int{1, 2, 3, 4, 5}, isEven) + result := goslices.Any([]int{1, 2, 3, 4, 5}, isEven) fmt.Println(result) - result = slices.Any([]int{1, 3, 5}, isEven) + result = goslices.Any([]int{1, 3, 5}, isEven) fmt.Println(result) // Output: @@ -36,7 +36,7 @@ func ExampleAny() { func ExampleEach() { printLine := func(s string) { fmt.Println(s) } - slices.Each([]string{"test 1", "test 2", "test 3"}, printLine) + goslices.Each([]string{"test 1", "test 2", "test 3"}, printLine) // Output: // test 1 @@ -46,7 +46,7 @@ func ExampleEach() { func ExampleFilter() { isEven := func(i int) bool { return i%2 == 0 } - result := slices.Filter([]int{1, 2, 3, 4, 5}, isEven) + result := goslices.Filter([]int{1, 2, 3, 4, 5}, isEven) fmt.Println(result) // Output: @@ -56,9 +56,9 @@ func ExampleFilter() { func ExampleFind() { isEven := func(i int) bool { return i%2 == 0 } - result, err := slices.Find([]int{1, 3, 4, 6, 7}, isEven) + result, err := goslices.Find([]int{1, 3, 4, 6, 7}, isEven) fmt.Println(result, err) - result, err = slices.Find([]int{1, 3, 5, 7}, isEven) + result, err = goslices.Find([]int{1, 3, 5, 7}, isEven) fmt.Println(result, err) // Output: @@ -69,7 +69,7 @@ func ExampleFind() { func ExampleMap() { withPrefix := func(i int) string { return "pre-" + strconv.Itoa(i) } - result := slices.Map([]int{1, 2, 3}, withPrefix) + result := goslices.Map([]int{1, 2, 3}, withPrefix) fmt.Println(result) // Output: @@ -79,7 +79,7 @@ func ExampleMap() { func ExampleReduce() { sum := func(last, cur int) int { return last + cur } - result := slices.Reduce([]int{2, 4, 6, 8}, sum, 0) + result := goslices.Reduce([]int{2, 4, 6, 8}, sum, 0) fmt.Println(result) // Output: diff --git a/slices/slice.go b/slice.go similarity index 98% rename from slices/slice.go rename to slice.go index eea3ee7..a28d589 100644 --- a/slices/slice.go +++ b/slice.go @@ -1,4 +1,4 @@ -package slices +package goslices func All[S ~[]T, T any](arr S, fn PredicateFn[T]) bool { for _, el := range arr { diff --git a/slices/slice_test.go b/slice_test.go similarity index 69% rename from slices/slice_test.go rename to slice_test.go index 4914849..32d7999 100644 --- a/slices/slice_test.go +++ b/slice_test.go @@ -1,10 +1,10 @@ -package slices_test +package goslices_test import ( "strconv" "testing" - "github.com/skippyza/go-slices/slices" + goslices "github.com/skippyza/go-slices" "github.com/stretchr/testify/assert" ) @@ -17,25 +17,25 @@ var ( func TestAll(t *testing.T) { arr := []int{1, 2, 3, 4} - result := slices.All(arr, isPositive) + result := goslices.All(arr, isPositive) assert.Equal(t, true, result) arr = []int{1, 2, 3, -1} - result = slices.All(arr, isPositive) + result = goslices.All(arr, isPositive) assert.Equal(t, false, result) } func TestAny(t *testing.T) { arr := []int{1, 2, 3, 4} - result := slices.Any(arr, isPositive) + result := goslices.Any(arr, isPositive) assert.Equal(t, true, result) arr = []int{1, 2, 3, -1} - result = slices.Any(arr, isPositive) + result = goslices.Any(arr, isPositive) assert.Equal(t, true, result) arr = []int{-1, -2, -3, -4} - result = slices.Any(arr, isPositive) + result = goslices.Any(arr, isPositive) assert.Equal(t, false, result) } @@ -44,35 +44,35 @@ func TestEach(t *testing.T) { counter := func(in int) { count++ } arr := []int{1, 2, 3, 4} - slices.Each(arr, counter) + goslices.Each(arr, counter) assert.Equal(t, 4, count) } func TestFilter(t *testing.T) { arr := []int{1, 2, 3, 4} - result := slices.Filter(arr, isEven) + result := goslices.Filter(arr, isEven) assert.Equal(t, []int{2, 4}, result) } func TestFind(t *testing.T) { arr := []int{1, 2, 3, 4} - result, err := slices.Find(arr, isEven) + result, err := goslices.Find(arr, isEven) assert.NoError(t, err) assert.Equal(t, 2, result) arr = []int{1, 3, 5, 7} - _, err = slices.Find(arr, isEven) - assert.ErrorIs(t, err, slices.ErrNotFound) + _, err = goslices.Find(arr, isEven) + assert.ErrorIs(t, err, goslices.ErrNotFound) } func TestMap(t *testing.T) { arr := []int{1, 2, 3, 4} - result := slices.Map(arr, doubleFn) + result := goslices.Map(arr, doubleFn) assert.Equal(t, []int{2, 4, 6, 8}, result) } func TestReduce(t *testing.T) { arr := []int{1, 2, 3, 4} - result := slices.Reduce(arr, appendToString, "") + result := goslices.Reduce(arr, appendToString, "") assert.Equal(t, "1234", result) } diff --git a/slices/doc.go b/slices/doc.go deleted file mode 100644 index a49b099..0000000 --- a/slices/doc.go +++ /dev/null @@ -1,2 +0,0 @@ -// Package slices provides generic functions for working with slices -package slices diff --git a/slices/types.go b/types.go similarity index 87% rename from slices/types.go rename to types.go index 69a6a62..f361495 100644 --- a/slices/types.go +++ b/types.go @@ -1,4 +1,4 @@ -package slices +package goslices type PredicateFn[T any] func(T) bool type MapFn[T any, U any] func(T) U