diff --git a/containers/enumerable.go b/containers/enumerable.go index 89ca07e..5824b23 100644 --- a/containers/enumerable.go +++ b/containers/enumerable.go @@ -36,10 +36,12 @@ type EnumerableWithIndex interface { // Invokes the given function once for each element and returns a // container containing the values returned by the given function. - Map(func(index int, value interface{}) interface{}) Container + // TODO need help on how to enforce this in containers (don't want to type assert when chaining) + // Map(func(index int, value interface{}) interface{}) Container // Returns a new container containing all elements for which the given function returns a true value. - Select(func(index int, value interface{}) bool) Container + // TODO need help on how to enforce this in containers (don't want to type assert when chaining) + // Select(func(index int, value interface{}) bool) Container // Passes each element of the container to the given function and // returns true if the function ever returns true for any element. @@ -62,10 +64,12 @@ type EnumerableWithKey interface { // Invokes the given function once for each element and returns a container // containing the values returned by the given function as key/value pairs. - Map(func(key interface{}, value interface{}) (interface{}, interface{})) Container + // TODO need help on how to enforce this in containers (don't want to type assert when chaining) + // Map(func(key interface{}, value interface{}) (interface{}, interface{})) Container // Returns a new container containing all elements for which the given function returns a true value. - Select(func(key interface{}, value interface{}) bool) Container + // TODO need help on how to enforce this in containers (don't want to type assert when chaining) + // Select(func(key interface{}, value interface{}) bool) Container // Passes each element of the container to the given function and // returns true if the function ever returns true for any element. diff --git a/lists/arraylist/arraylist.go b/lists/arraylist/arraylist.go index d577385..cdcd2d9 100644 --- a/lists/arraylist/arraylist.go +++ b/lists/arraylist/arraylist.go @@ -218,7 +218,7 @@ func (list *List) Each(f func(index int, value interface{})) { // Invokes the given function once for each element and returns a // container containing the values returned by the given function. -func (list *List) Map(f func(index int, value interface{}) interface{}) containers.Container { +func (list *List) Map(f func(index int, value interface{}) interface{}) *List { newList := &List{} iterator := list.Iterator() for iterator.Next() { @@ -228,7 +228,7 @@ func (list *List) Map(f func(index int, value interface{}) interface{}) containe } // Returns a new container containing all elements for which the given function returns a true value. -func (list *List) Select(f func(index int, value interface{}) bool) containers.Container { +func (list *List) Select(f func(index int, value interface{}) bool) *List { newList := &List{} iterator := list.Iterator() for iterator.Next() { diff --git a/lists/arraylist/arraylist_test.go b/lists/arraylist/arraylist_test.go index 68c499a..c3f0c58 100644 --- a/lists/arraylist/arraylist_test.go +++ b/lists/arraylist/arraylist_test.go @@ -162,7 +162,7 @@ func TestArrayListEnumerableAndIterator(t *testing.T) { // Map mappedList := list.Map(func(index int, value interface{}) interface{} { return "mapped: " + value.(string) - }).(*List) + }) if actualValue, _ := mappedList.Get(0); actualValue != "mapped: a" { t.Errorf("Got %v expected %v", actualValue, "mapped: a") } @@ -179,7 +179,7 @@ func TestArrayListEnumerableAndIterator(t *testing.T) { // Select selectedList := list.Select(func(index int, value interface{}) bool { return value.(string) >= "a" && value.(string) <= "b" - }).(*List) + }) if actualValue, _ := selectedList.Get(0); actualValue != "a" { t.Errorf("Got %v expected %v", actualValue, "value: a") } diff --git a/lists/doublylinkedlist/doublylinkedlist.go b/lists/doublylinkedlist/doublylinkedlist.go index 2fa22d4..73f7e59 100644 --- a/lists/doublylinkedlist/doublylinkedlist.go +++ b/lists/doublylinkedlist/doublylinkedlist.go @@ -350,7 +350,7 @@ func (list *List) Each(f func(index int, value interface{})) { // Invokes the given function once for each element and returns a // container containing the values returned by the given function. -func (list *List) Map(f func(index int, value interface{}) interface{}) containers.Container { +func (list *List) Map(f func(index int, value interface{}) interface{}) *List { newList := &List{} iterator := list.Iterator() for iterator.Next() { @@ -360,7 +360,7 @@ func (list *List) Map(f func(index int, value interface{}) interface{}) containe } // Returns a new container containing all elements for which the given function returns a true value. -func (list *List) Select(f func(index int, value interface{}) bool) containers.Container { +func (list *List) Select(f func(index int, value interface{}) bool) *List { newList := &List{} iterator := list.Iterator() for iterator.Next() { diff --git a/lists/doublylinkedlist/doublylinkedlist_test.go b/lists/doublylinkedlist/doublylinkedlist_test.go index 6d580c8..5e8331d 100644 --- a/lists/doublylinkedlist/doublylinkedlist_test.go +++ b/lists/doublylinkedlist/doublylinkedlist_test.go @@ -164,7 +164,7 @@ func TestDoublyLinkedListEnumerableAndIterator(t *testing.T) { // Map mappedList := list.Map(func(index int, value interface{}) interface{} { return "mapped: " + value.(string) - }).(*List) + }) if actualValue, _ := mappedList.Get(0); actualValue != "mapped: a" { t.Errorf("Got %v expected %v", actualValue, "mapped: a") } @@ -181,7 +181,7 @@ func TestDoublyLinkedListEnumerableAndIterator(t *testing.T) { // Select selectedList := list.Select(func(index int, value interface{}) bool { return value.(string) >= "a" && value.(string) <= "b" - }).(*List) + }) if actualValue, _ := selectedList.Get(0); actualValue != "a" { t.Errorf("Got %v expected %v", actualValue, "value: a") } diff --git a/lists/singlylinkedlist/singlylinkedlist.go b/lists/singlylinkedlist/singlylinkedlist.go index 52e834e..e71a36f 100644 --- a/lists/singlylinkedlist/singlylinkedlist.go +++ b/lists/singlylinkedlist/singlylinkedlist.go @@ -320,7 +320,7 @@ func (list *List) Each(f func(index int, value interface{})) { // Invokes the given function once for each element and returns a // container containing the values returned by the given function. -func (list *List) Map(f func(index int, value interface{}) interface{}) containers.Container { +func (list *List) Map(f func(index int, value interface{}) interface{}) *List { newList := &List{} iterator := list.Iterator() for iterator.Next() { @@ -330,7 +330,7 @@ func (list *List) Map(f func(index int, value interface{}) interface{}) containe } // Returns a new container containing all elements for which the given function returns a true value. -func (list *List) Select(f func(index int, value interface{}) bool) containers.Container { +func (list *List) Select(f func(index int, value interface{}) bool) *List { newList := &List{} iterator := list.Iterator() for iterator.Next() { diff --git a/lists/singlylinkedlist/singlylinkedlist_test.go b/lists/singlylinkedlist/singlylinkedlist_test.go index c5ce378..d5879c3 100644 --- a/lists/singlylinkedlist/singlylinkedlist_test.go +++ b/lists/singlylinkedlist/singlylinkedlist_test.go @@ -164,7 +164,7 @@ func TestSinglyLinkedListEnumerableAndIterator(t *testing.T) { // Map mappedList := list.Map(func(index int, value interface{}) interface{} { return "mapped: " + value.(string) - }).(*List) + }) if actualValue, _ := mappedList.Get(0); actualValue != "mapped: a" { t.Errorf("Got %v expected %v", actualValue, "mapped: a") } @@ -181,7 +181,7 @@ func TestSinglyLinkedListEnumerableAndIterator(t *testing.T) { // Select selectedList := list.Select(func(index int, value interface{}) bool { return value.(string) >= "a" && value.(string) <= "b" - }).(*List) + }) if actualValue, _ := selectedList.Get(0); actualValue != "a" { t.Errorf("Got %v expected %v", actualValue, "value: a") } diff --git a/maps/treemap/treemap.go b/maps/treemap/treemap.go index c8aee76..b18f293 100644 --- a/maps/treemap/treemap.go +++ b/maps/treemap/treemap.go @@ -163,7 +163,7 @@ func (m *Map) Each(f func(key interface{}, value interface{})) { // Invokes the given function once for each element and returns a container // containing the values returned by the given function as key/value pairs. -func (m *Map) Map(f func(key1 interface{}, value1 interface{}) (interface{}, interface{})) containers.Container { +func (m *Map) Map(f func(key1 interface{}, value1 interface{}) (interface{}, interface{})) *Map { newMap := &Map{tree: rbt.NewWith(m.tree.Comparator)} iterator := m.Iterator() for iterator.Next() { @@ -174,7 +174,7 @@ func (m *Map) Map(f func(key1 interface{}, value1 interface{}) (interface{}, int } // Returns a new container containing all elements for which the given function returns a true value. -func (m *Map) Select(f func(key interface{}, value interface{}) bool) containers.Container { +func (m *Map) Select(f func(key interface{}, value interface{}) bool) *Map { newMap := &Map{tree: rbt.NewWith(m.tree.Comparator)} iterator := m.Iterator() for iterator.Next() { diff --git a/maps/treemap/treemap_test.go b/maps/treemap/treemap_test.go index 3ad688c..4335b7f 100644 --- a/maps/treemap/treemap_test.go +++ b/maps/treemap/treemap_test.go @@ -213,7 +213,7 @@ func TestTreeMapEnumerableAndIterator(t *testing.T) { // Map mappedMap := m.Map(func(key1 interface{}, value1 interface{}) (key2 interface{}, value2 interface{}) { return key1, "mapped: " + key1.(string) - }).(*Map) + }) if actualValue, _ := mappedMap.Get("a"); actualValue != "mapped: a" { t.Errorf("Got %v expected %v", actualValue, "mapped: a") } @@ -230,7 +230,7 @@ func TestTreeMapEnumerableAndIterator(t *testing.T) { // Select selectedMap := m.Select(func(key interface{}, value interface{}) bool { return key.(string) >= "a" && key.(string) <= "b" - }).(*Map) + }) if actualValue, _ := selectedMap.Get("a"); actualValue != 1 { t.Errorf("Got %v expected %v", actualValue, "value: a") } diff --git a/sets/treeset/treeset.go b/sets/treeset/treeset.go index b9ca141..750e264 100644 --- a/sets/treeset/treeset.go +++ b/sets/treeset/treeset.go @@ -144,7 +144,7 @@ func (set *Set) Each(f func(index int, value interface{})) { // Invokes the given function once for each element and returns a // container containing the values returned by the given function. -func (set *Set) Map(f func(index int, value interface{}) interface{}) containers.Container { +func (set *Set) Map(f func(index int, value interface{}) interface{}) *Set { newSet := &Set{tree: rbt.NewWith(set.tree.Comparator)} iterator := set.Iterator() for iterator.Next() { @@ -154,7 +154,7 @@ func (set *Set) Map(f func(index int, value interface{}) interface{}) containers } // Returns a new container containing all elements for which the given function returns a true value. -func (set *Set) Select(f func(index int, value interface{}) bool) containers.Container { +func (set *Set) Select(f func(index int, value interface{}) bool) *Set { newSet := &Set{tree: rbt.NewWith(set.tree.Comparator)} iterator := set.Iterator() for iterator.Next() { diff --git a/sets/treeset/treeset_test.go b/sets/treeset/treeset_test.go index e725d82..db30724 100644 --- a/sets/treeset/treeset_test.go +++ b/sets/treeset/treeset_test.go @@ -111,7 +111,7 @@ func TestTreeSetEnumerableAndIterator(t *testing.T) { // Map mappedSet := set.Map(func(index int, value interface{}) interface{} { return "mapped: " + value.(string) - }).(*Set) + }) if actualValue, expectedValue := mappedSet.Contains("mapped: a", "mapped: b", "mapped: c"), true; actualValue != expectedValue { t.Errorf("Got %v expected %v", actualValue, expectedValue) } @@ -125,7 +125,7 @@ func TestTreeSetEnumerableAndIterator(t *testing.T) { // Select selectedSet := set.Select(func(index int, value interface{}) bool { return value.(string) >= "a" && value.(string) <= "b" - }).(*Set) + }) if actualValue, expectedValue := selectedSet.Contains("a", "b"), true; actualValue != expectedValue { fmt.Println("A: ", mappedSet.Contains("b")) t.Errorf("Got %v (%v) expected %v (%v)", actualValue, selectedSet.Values(), expectedValue, "[a b]")