-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #98 from Primetalk/feat-extract-set
fix: move set functions to a separate package
- Loading branch information
Showing
7 changed files
with
70 additions
and
38 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
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,17 @@ | ||
package set | ||
|
||
// Set is a map with a dummy value. | ||
type Set[A comparable] map[A]struct{} | ||
|
||
// Contains creates a predicate that will check if an element is in this set. | ||
func Contains[A comparable](set Set[A]) func(A) bool { | ||
return func(a A) (ok bool) { | ||
_, ok = set[a] | ||
return | ||
} | ||
} | ||
|
||
// SetSize returns the size of the set. | ||
func SetSize[A comparable](s Set[A]) int { | ||
return len(s) | ||
} |
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,26 @@ | ||
package set_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/primetalk/goio/set" | ||
"github.com/primetalk/goio/slice" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var nats10Values = []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} | ||
|
||
func TestSet(t *testing.T) { | ||
intsDuplicated := slice.FlatMap(nats10Values, func(i int) []int { | ||
return slice.Map(nats10Values, func(j int) int { return i + j }) | ||
}) | ||
intsSet := slice.ToSet(intsDuplicated) | ||
assert.Equal(t, 19, set.SetSize(intsSet)) | ||
} | ||
|
||
func TestFilter(t *testing.T) { | ||
s123 := slice.ToSet([]int{1, 2, 3}) | ||
assert.True(t, set.Contains(s123)(3), "3 \\in {1,2,3}") | ||
sl13 := slice.Filter([]int{1, 3, 5, 7}, set.Contains(s123)) | ||
assert.ElementsMatch(t, []int{1, 3}, sl13) | ||
} |
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