- remove map and select functions from enumerable interface, because this requires type assertions in chaining, which is really ugly and unnecessary. the only drawback is that one might forget to implement those functions and interface implementations asserts will not register that. (need help on this)

pull/12/head
Emir Pasic 8 years ago
parent 8aba2d70fe
commit 544abaeab1

@ -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.

@ -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() {

@ -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")
}

@ -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() {

@ -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")
}

@ -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() {

@ -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")
}

@ -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() {

@ -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")
}

@ -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() {

@ -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]")

Loading…
Cancel
Save