Skip to content

Commit

Permalink
Moving library to root package
Browse files Browse the repository at this point in the history
  • Loading branch information
SkippyZA committed Sep 13, 2024
1 parent 181aca4 commit 3707514
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 36 deletions.
6 changes: 1 addition & 5 deletions doc.go
Original file line number Diff line number Diff line change
@@ -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"
)
2 changes: 1 addition & 1 deletion slices/errors.go → errors.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package slices
package goslices

import "errors"

Expand Down
24 changes: 12 additions & 12 deletions slices/example_test.go → example_test.go
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion slices/slice.go → slice.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package slices
package goslices

func All[S ~[]T, T any](arr S, fn PredicateFn[T]) bool {
for _, el := range arr {
Expand Down
28 changes: 14 additions & 14 deletions slices/slice_test.go → slice_test.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand All @@ -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)
}

Expand All @@ -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)
}
2 changes: 0 additions & 2 deletions slices/doc.go

This file was deleted.

2 changes: 1 addition & 1 deletion slices/types.go → types.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package slices
package goslices

type PredicateFn[T any] func(T) bool
type MapFn[T any, U any] func(T) U
Expand Down

0 comments on commit 3707514

Please sign in to comment.