- split enumerables into enumerables with keys and with indexes (same was done for iterators)

pull/12/head
Emir Pasic 8 years ago
parent 7346ca6337
commit 3b6a40775a

@ -29,7 +29,34 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package containers
type Enumerable interface {
// Enumerable function for ordered containers whose values can be fetched by an index.
type EnumerableWithIndex interface {
// Calls the given function once for each element, passing that element's index(key) and value.
Each(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.
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
// Passes each element of the collection to the given function and
// returns true if the function ever returns true for any element.
Any(func(index int, value interface{}) bool) bool
// Passes each element of the collection to the given function and
// returns true if the function returns true for all elements.
All(func(index int, value interface{}) bool) bool
// Passes each element of the collection to the given function and returns
// the first for which the function is true or nil,nil otherwise if no element
// matches the criteria.
Find(func(index int, value interface{}) bool) (index int, value interface{})
}
// Enumerable function for ordered containers whose values whose elements are key value pairs.
type EnumerableWithKey interface {
// Calls the given function once for each element, passing that element's index(key) and value.
Each(func(index interface{}, value interface{}))

@ -40,7 +40,7 @@ import (
func assertInterfaceImplementation() {
var _ lists.List = (*List)(nil)
var _ containers.Enumerable = (*List)(nil)
var _ containers.EnumerableWithIndex = (*List)(nil)
var _ containers.IteratorWithIndex = (*Iterator)(nil)
}
@ -200,14 +200,14 @@ func (iterator *Iterator) Index() int {
return iterator.index
}
func (list *List) Each(f func(index interface{}, value interface{})) {
func (list *List) Each(f func(index int, value interface{})) {
iterator := list.Iterator()
for iterator.Next() {
f(iterator.Index(), iterator.Value())
}
}
func (list *List) Map(f func(index interface{}, value interface{}) interface{}) containers.Container {
func (list *List) Map(f func(index int, value interface{}) interface{}) containers.Container {
newList := &List{}
iterator := list.Iterator()
for iterator.Next() {
@ -216,7 +216,7 @@ func (list *List) Map(f func(index interface{}, value interface{}) interface{})
return newList
}
func (list *List) Select(f func(index interface{}, value interface{}) bool) containers.Container {
func (list *List) Select(f func(index int, value interface{}) bool) containers.Container {
newList := &List{}
iterator := list.Iterator()
for iterator.Next() {
@ -227,7 +227,7 @@ func (list *List) Select(f func(index interface{}, value interface{}) bool) cont
return newList
}
func (list *List) Any(f func(index interface{}, value interface{}) bool) bool {
func (list *List) Any(f func(index int, value interface{}) bool) bool {
iterator := list.Iterator()
for iterator.Next() {
if f(iterator.Index(), iterator.Value()) {
@ -237,7 +237,7 @@ func (list *List) Any(f func(index interface{}, value interface{}) bool) bool {
return false
}
func (list *List) All(f func(index interface{}, value interface{}) bool) bool {
func (list *List) All(f func(index int, value interface{}) bool) bool {
iterator := list.Iterator()
for iterator.Next() {
if !f(iterator.Index(), iterator.Value()) {
@ -247,14 +247,14 @@ func (list *List) All(f func(index interface{}, value interface{}) bool) bool {
return true
}
func (list *List) Find(f func(index interface{}, value interface{}) bool) (index interface{}, value interface{}) {
func (list *List) Find(f func(index int, value interface{}) bool) (index int, value interface{}) {
iterator := list.Iterator()
for iterator.Next() {
if f(iterator.Index(), iterator.Value()) {
return iterator.Index(), iterator.Value()
}
}
return nil, nil
return -1, nil
}
func (list *List) String() string {

@ -140,7 +140,7 @@ func TestArrayListEnumerableAndIterator(t *testing.T) {
list.Add("a", "b", "c")
// Each
list.Each(func(index interface{}, value interface{}) {
list.Each(func(index int, value interface{}) {
switch index {
case 0:
if actualValue, expectedValue := value, "a"; actualValue != expectedValue {
@ -160,7 +160,7 @@ func TestArrayListEnumerableAndIterator(t *testing.T) {
})
// Map
mappedList := list.Map(func(index interface{}, value interface{}) interface{} {
mappedList := list.Map(func(index int, value interface{}) interface{} {
return "mapped: " + value.(string)
}).(*List)
if actualValue, _ := mappedList.Get(0); actualValue != "mapped: a" {
@ -177,7 +177,7 @@ func TestArrayListEnumerableAndIterator(t *testing.T) {
}
// Select
selectedList := list.Select(func(index interface{}, value interface{}) bool {
selectedList := list.Select(func(index int, value interface{}) bool {
return value.(string) >= "a" && value.(string) <= "b"
}).(*List)
if actualValue, _ := selectedList.Get(0); actualValue != "a" {
@ -191,13 +191,13 @@ func TestArrayListEnumerableAndIterator(t *testing.T) {
}
// Any
any := list.Any(func(index interface{}, value interface{}) bool {
any := list.Any(func(index int, value interface{}) bool {
return value.(string) == "c"
})
if any != true {
t.Errorf("Got %v expected %v", any, true)
}
any = list.Any(func(index interface{}, value interface{}) bool {
any = list.Any(func(index int, value interface{}) bool {
return value.(string) == "x"
})
if any != false {
@ -205,13 +205,13 @@ func TestArrayListEnumerableAndIterator(t *testing.T) {
}
// All
all := list.All(func(index interface{}, value interface{}) bool {
all := list.All(func(index int, value interface{}) bool {
return value.(string) >= "a" && value.(string) <= "c"
})
if all != true {
t.Errorf("Got %v expected %v", all, true)
}
all = list.All(func(index interface{}, value interface{}) bool {
all = list.All(func(index int, value interface{}) bool {
return value.(string) >= "a" && value.(string) <= "b"
})
if all != false {
@ -219,16 +219,16 @@ func TestArrayListEnumerableAndIterator(t *testing.T) {
}
// Find
foundIndex, foundValue := list.Find(func(index interface{}, value interface{}) bool {
foundIndex, foundValue := list.Find(func(index int, value interface{}) bool {
return value.(string) == "c"
})
if foundValue != "c" || foundIndex != 2 {
t.Errorf("Got %v at %v expected %v at %v", foundValue, foundIndex, "c", 2)
}
foundIndex, foundValue = list.Find(func(index interface{}, value interface{}) bool {
foundIndex, foundValue = list.Find(func(index int, value interface{}) bool {
return value.(string) == "x"
})
if foundValue != nil || foundIndex != nil {
if foundValue != nil || foundIndex != -1 {
t.Errorf("Got %v at %v expected %v at %v", foundValue, foundIndex, nil, nil)
}

@ -40,7 +40,7 @@ import (
func assertInterfaceImplementation() {
var _ lists.List = (*List)(nil)
var _ containers.Enumerable = (*List)(nil)
var _ containers.EnumerableWithIndex = (*List)(nil)
var _ containers.IteratorWithIndex = (*Iterator)(nil)
}
@ -332,14 +332,14 @@ func (iterator *Iterator) Index() int {
return iterator.index
}
func (list *List) Each(f func(index interface{}, value interface{})) {
func (list *List) Each(f func(index int, value interface{})) {
iterator := list.Iterator()
for iterator.Next() {
f(iterator.Index(), iterator.Value())
}
}
func (list *List) Map(f func(index interface{}, value interface{}) interface{}) containers.Container {
func (list *List) Map(f func(index int, value interface{}) interface{}) containers.Container {
newList := &List{}
iterator := list.Iterator()
for iterator.Next() {
@ -348,7 +348,7 @@ func (list *List) Map(f func(index interface{}, value interface{}) interface{})
return newList
}
func (list *List) Select(f func(index interface{}, value interface{}) bool) containers.Container {
func (list *List) Select(f func(index int, value interface{}) bool) containers.Container {
newList := &List{}
iterator := list.Iterator()
for iterator.Next() {
@ -359,7 +359,7 @@ func (list *List) Select(f func(index interface{}, value interface{}) bool) cont
return newList
}
func (list *List) Any(f func(index interface{}, value interface{}) bool) bool {
func (list *List) Any(f func(index int, value interface{}) bool) bool {
iterator := list.Iterator()
for iterator.Next() {
if f(iterator.Index(), iterator.Value()) {
@ -369,7 +369,7 @@ func (list *List) Any(f func(index interface{}, value interface{}) bool) bool {
return false
}
func (list *List) All(f func(index interface{}, value interface{}) bool) bool {
func (list *List) All(f func(index int, value interface{}) bool) bool {
iterator := list.Iterator()
for iterator.Next() {
if !f(iterator.Index(), iterator.Value()) {
@ -379,14 +379,14 @@ func (list *List) All(f func(index interface{}, value interface{}) bool) bool {
return true
}
func (list *List) Find(f func(index interface{}, value interface{}) bool) (index interface{}, value interface{}) {
func (list *List) Find(f func(index int, value interface{}) bool) (index int, value interface{}) {
iterator := list.Iterator()
for iterator.Next() {
if f(iterator.Index(), iterator.Value()) {
return iterator.Index(), iterator.Value()
}
}
return nil, nil
return -1, nil
}
func (list *List) String() string {

@ -142,7 +142,7 @@ func TestDoublyLinkedListEnumerableAndIterator(t *testing.T) {
list.Add("a", "b", "c")
// Each
list.Each(func(index interface{}, value interface{}) {
list.Each(func(index int, value interface{}) {
switch index {
case 0:
if actualValue, expectedValue := value, "a"; actualValue != expectedValue {
@ -162,7 +162,7 @@ func TestDoublyLinkedListEnumerableAndIterator(t *testing.T) {
})
// Map
mappedList := list.Map(func(index interface{}, value interface{}) interface{} {
mappedList := list.Map(func(index int, value interface{}) interface{} {
return "mapped: " + value.(string)
}).(*List)
if actualValue, _ := mappedList.Get(0); actualValue != "mapped: a" {
@ -179,7 +179,7 @@ func TestDoublyLinkedListEnumerableAndIterator(t *testing.T) {
}
// Select
selectedList := list.Select(func(index interface{}, value interface{}) bool {
selectedList := list.Select(func(index int, value interface{}) bool {
return value.(string) >= "a" && value.(string) <= "b"
}).(*List)
if actualValue, _ := selectedList.Get(0); actualValue != "a" {
@ -193,13 +193,13 @@ func TestDoublyLinkedListEnumerableAndIterator(t *testing.T) {
}
// Any
any := list.Any(func(index interface{}, value interface{}) bool {
any := list.Any(func(index int, value interface{}) bool {
return value.(string) == "c"
})
if any != true {
t.Errorf("Got %v expected %v", any, true)
}
any = list.Any(func(index interface{}, value interface{}) bool {
any = list.Any(func(index int, value interface{}) bool {
return value.(string) == "x"
})
if any != false {
@ -207,13 +207,13 @@ func TestDoublyLinkedListEnumerableAndIterator(t *testing.T) {
}
// All
all := list.All(func(index interface{}, value interface{}) bool {
all := list.All(func(index int, value interface{}) bool {
return value.(string) >= "a" && value.(string) <= "c"
})
if all != true {
t.Errorf("Got %v expected %v", all, true)
}
all = list.All(func(index interface{}, value interface{}) bool {
all = list.All(func(index int, value interface{}) bool {
return value.(string) >= "a" && value.(string) <= "b"
})
if all != false {
@ -221,16 +221,16 @@ func TestDoublyLinkedListEnumerableAndIterator(t *testing.T) {
}
// Find
foundIndex, foundValue := list.Find(func(index interface{}, value interface{}) bool {
foundIndex, foundValue := list.Find(func(index int, value interface{}) bool {
return value.(string) == "c"
})
if foundValue != "c" || foundIndex != 2 {
t.Errorf("Got %v at %v expected %v at %v", foundValue, foundIndex, "c", 2)
}
foundIndex, foundValue = list.Find(func(index interface{}, value interface{}) bool {
foundIndex, foundValue = list.Find(func(index int, value interface{}) bool {
return value.(string) == "x"
})
if foundValue != nil || foundIndex != nil {
if foundValue != nil || foundIndex != -1 {
t.Errorf("Got %v at %v expected %v at %v", foundValue, foundIndex, nil, nil)
}

@ -40,7 +40,7 @@ import (
func assertInterfaceImplementation() {
var _ lists.List = (*List)(nil)
var _ containers.Enumerable = (*List)(nil)
var _ containers.EnumerableWithIndex = (*List)(nil)
var _ containers.IteratorWithIndex = (*Iterator)(nil)
}
@ -302,14 +302,14 @@ func (iterator *Iterator) Index() int {
return iterator.index
}
func (list *List) Each(f func(index interface{}, value interface{})) {
func (list *List) Each(f func(index int, value interface{})) {
iterator := list.Iterator()
for iterator.Next() {
f(iterator.Index(), iterator.Value())
}
}
func (list *List) Map(f func(index interface{}, value interface{}) interface{}) containers.Container {
func (list *List) Map(f func(index int, value interface{}) interface{}) containers.Container {
newList := &List{}
iterator := list.Iterator()
for iterator.Next() {
@ -318,7 +318,7 @@ func (list *List) Map(f func(index interface{}, value interface{}) interface{})
return newList
}
func (list *List) Select(f func(index interface{}, value interface{}) bool) containers.Container {
func (list *List) Select(f func(index int, value interface{}) bool) containers.Container {
newList := &List{}
iterator := list.Iterator()
for iterator.Next() {
@ -329,7 +329,7 @@ func (list *List) Select(f func(index interface{}, value interface{}) bool) cont
return newList
}
func (list *List) Any(f func(index interface{}, value interface{}) bool) bool {
func (list *List) Any(f func(index int, value interface{}) bool) bool {
iterator := list.Iterator()
for iterator.Next() {
if f(iterator.Index(), iterator.Value()) {
@ -339,7 +339,7 @@ func (list *List) Any(f func(index interface{}, value interface{}) bool) bool {
return false
}
func (list *List) All(f func(index interface{}, value interface{}) bool) bool {
func (list *List) All(f func(index int, value interface{}) bool) bool {
iterator := list.Iterator()
for iterator.Next() {
if !f(iterator.Index(), iterator.Value()) {
@ -349,14 +349,14 @@ func (list *List) All(f func(index interface{}, value interface{}) bool) bool {
return true
}
func (list *List) Find(f func(index interface{}, value interface{}) bool) (index interface{}, value interface{}) {
func (list *List) Find(f func(index int, value interface{}) bool) (index int, value interface{}) {
iterator := list.Iterator()
for iterator.Next() {
if f(iterator.Index(), iterator.Value()) {
return iterator.Index(), iterator.Value()
}
}
return nil, nil
return -1, nil
}
func (list *List) String() string {

@ -142,7 +142,7 @@ func TestSinglyLinkedListEnumerableAndIterator(t *testing.T) {
list.Add("a", "b", "c")
// Each
list.Each(func(index interface{}, value interface{}) {
list.Each(func(index int, value interface{}) {
switch index {
case 0:
if actualValue, expectedValue := value, "a"; actualValue != expectedValue {
@ -162,7 +162,7 @@ func TestSinglyLinkedListEnumerableAndIterator(t *testing.T) {
})
// Map
mappedList := list.Map(func(index interface{}, value interface{}) interface{} {
mappedList := list.Map(func(index int, value interface{}) interface{} {
return "mapped: " + value.(string)
}).(*List)
if actualValue, _ := mappedList.Get(0); actualValue != "mapped: a" {
@ -179,7 +179,7 @@ func TestSinglyLinkedListEnumerableAndIterator(t *testing.T) {
}
// Select
selectedList := list.Select(func(index interface{}, value interface{}) bool {
selectedList := list.Select(func(index int, value interface{}) bool {
return value.(string) >= "a" && value.(string) <= "b"
}).(*List)
if actualValue, _ := selectedList.Get(0); actualValue != "a" {
@ -193,13 +193,13 @@ func TestSinglyLinkedListEnumerableAndIterator(t *testing.T) {
}
// Any
any := list.Any(func(index interface{}, value interface{}) bool {
any := list.Any(func(index int, value interface{}) bool {
return value.(string) == "c"
})
if any != true {
t.Errorf("Got %v expected %v", any, true)
}
any = list.Any(func(index interface{}, value interface{}) bool {
any = list.Any(func(index int, value interface{}) bool {
return value.(string) == "x"
})
if any != false {
@ -207,13 +207,13 @@ func TestSinglyLinkedListEnumerableAndIterator(t *testing.T) {
}
// All
all := list.All(func(index interface{}, value interface{}) bool {
all := list.All(func(index int, value interface{}) bool {
return value.(string) >= "a" && value.(string) <= "c"
})
if all != true {
t.Errorf("Got %v expected %v", all, true)
}
all = list.All(func(index interface{}, value interface{}) bool {
all = list.All(func(index int, value interface{}) bool {
return value.(string) >= "a" && value.(string) <= "b"
})
if all != false {
@ -221,16 +221,16 @@ func TestSinglyLinkedListEnumerableAndIterator(t *testing.T) {
}
// Find
foundIndex, foundValue := list.Find(func(index interface{}, value interface{}) bool {
foundIndex, foundValue := list.Find(func(index int, value interface{}) bool {
return value.(string) == "c"
})
if foundValue != "c" || foundIndex != 2 {
t.Errorf("Got %v at %v expected %v at %v", foundValue, foundIndex, "c", 2)
}
foundIndex, foundValue = list.Find(func(index interface{}, value interface{}) bool {
foundIndex, foundValue = list.Find(func(index int, value interface{}) bool {
return value.(string) == "x"
})
if foundValue != nil || foundIndex != nil {
if foundValue != nil || foundIndex != -1 {
t.Errorf("Got %v at %v expected %v at %v", foundValue, foundIndex, nil, nil)
}

@ -33,7 +33,7 @@ import (
func assertInterfaceImplementation() {
var _ sets.Set = (*Set)(nil)
var _ containers.Enumerable = (*Set)(nil)
var _ containers.EnumerableWithIndex = (*Set)(nil)
var _ containers.IteratorWithIndex = (*Iterator)(nil)
}
@ -126,14 +126,14 @@ func (iterator *Iterator) Index() int {
return iterator.index
}
func (set *Set) Each(f func(index interface{}, value interface{})) {
func (set *Set) Each(f func(index int, value interface{})) {
iterator := set.Iterator()
for iterator.Next() {
f(iterator.Index(), iterator.Value())
}
}
func (set *Set) Map(f func(index interface{}, value interface{}) interface{}) containers.Container {
func (set *Set) Map(f func(index int, value interface{}) interface{}) containers.Container {
newSet := &Set{tree: rbt.NewWith(set.tree.Comparator)}
iterator := set.Iterator()
for iterator.Next() {
@ -142,7 +142,7 @@ func (set *Set) Map(f func(index interface{}, value interface{}) interface{}) co
return newSet
}
func (set *Set) Select(f func(index interface{}, value interface{}) bool) containers.Container {
func (set *Set) Select(f func(index int, value interface{}) bool) containers.Container {
newSet := &Set{tree: rbt.NewWith(set.tree.Comparator)}
iterator := set.Iterator()
for iterator.Next() {
@ -153,7 +153,7 @@ func (set *Set) Select(f func(index interface{}, value interface{}) bool) contai
return newSet
}
func (set *Set) Any(f func(index interface{}, value interface{}) bool) bool {
func (set *Set) Any(f func(index int, value interface{}) bool) bool {
iterator := set.Iterator()
for iterator.Next() {
if f(iterator.Index(), iterator.Value()) {
@ -163,7 +163,7 @@ func (set *Set) Any(f func(index interface{}, value interface{}) bool) bool {
return false
}
func (set *Set) All(f func(index interface{}, value interface{}) bool) bool {
func (set *Set) All(f func(index int, value interface{}) bool) bool {
iterator := set.Iterator()
for iterator.Next() {
if !f(iterator.Index(), iterator.Value()) {
@ -173,14 +173,14 @@ func (set *Set) All(f func(index interface{}, value interface{}) bool) bool {
return true
}
func (set *Set) Find(f func(index interface{}, value interface{}) bool) (index interface{}, value interface{}) {
func (set *Set) Find(f func(index int, value interface{}) bool) (index int, value interface{}) {
iterator := set.Iterator()
for iterator.Next() {
if f(iterator.Index(), iterator.Value()) {
return iterator.Index(), iterator.Value()
}
}
return nil, nil
return -1, nil
}
func (set *Set) String() string {

@ -89,7 +89,7 @@ func TestTreeSetEnumerableAndIterator(t *testing.T) {
set.Add("c", "a", "b")
// Each
set.Each(func(index interface{}, value interface{}) {
set.Each(func(index int, value interface{}) {
switch index {
case 0:
if actualValue, expectedValue := value, "a"; actualValue != expectedValue {
@ -109,7 +109,7 @@ func TestTreeSetEnumerableAndIterator(t *testing.T) {
})
// Map
mappedSet := set.Map(func(index interface{}, value interface{}) interface{} {
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 {
@ -123,7 +123,7 @@ func TestTreeSetEnumerableAndIterator(t *testing.T) {
}
// Select
selectedSet := set.Select(func(index interface{}, value interface{}) bool {
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 {
@ -138,13 +138,13 @@ func TestTreeSetEnumerableAndIterator(t *testing.T) {
}
// Any
any := set.Any(func(index interface{}, value interface{}) bool {
any := set.Any(func(index int, value interface{}) bool {
return value.(string) == "c"
})
if any != true {
t.Errorf("Got %v expected %v", any, true)
}
any = set.Any(func(index interface{}, value interface{}) bool {
any = set.Any(func(index int, value interface{}) bool {
return value.(string) == "x"
})
if any != false {
@ -152,13 +152,13 @@ func TestTreeSetEnumerableAndIterator(t *testing.T) {
}
// All
all := set.All(func(index interface{}, value interface{}) bool {
all := set.All(func(index int, value interface{}) bool {
return value.(string) >= "a" && value.(string) <= "c"
})
if all != true {
t.Errorf("Got %v expected %v", all, true)
}
all = set.All(func(index interface{}, value interface{}) bool {
all = set.All(func(index int, value interface{}) bool {
return value.(string) >= "a" && value.(string) <= "b"
})
if all != false {
@ -166,16 +166,16 @@ func TestTreeSetEnumerableAndIterator(t *testing.T) {
}
// Find
foundIndex, foundValue := set.Find(func(index interface{}, value interface{}) bool {
foundIndex, foundValue := set.Find(func(index int, value interface{}) bool {
return value.(string) == "c"
})
if foundValue != "c" || foundIndex != 2 {
t.Errorf("Got %v at %v expected %v at %v", foundValue, foundIndex, "c", 2)
}
foundIndex, foundValue = set.Find(func(index interface{}, value interface{}) bool {
foundIndex, foundValue = set.Find(func(index int, value interface{}) bool {
return value.(string) == "x"
})
if foundValue != nil || foundIndex != nil {
if foundValue != nil || foundIndex != -1 {
t.Errorf("Got %v at %v expected %v at %v", foundValue, foundIndex, nil, nil)
}

Loading…
Cancel
Save