- rewrite enumerable operations using iterator (for easier copy/paste into other containers)

pull/12/head
Emir Pasic 9 years ago
parent 549ece1100
commit c685593e6e

@ -178,33 +178,54 @@ func (list *List) Insert(index int, values ...interface{}) {
} }
} }
func (list *List) Iterator() Iterator {
return Iterator{list: list, current: -1}
}
func (iterator *Iterator) Next() bool {
iterator.current += 1
return iterator.list.withinRange(iterator.current)
}
func (iterator *Iterator) Value() interface{} {
return iterator.list.elements[iterator.current]
}
func (iterator *Iterator) Index() interface{} {
return iterator.current
}
func (list *List) Each(f func(index interface{}, value interface{})) { func (list *List) Each(f func(index interface{}, value interface{})) {
for i := 0; i < list.size; i++ { iterator := list.Iterator()
f(i, list.elements[i]) 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 interface{}, value interface{}) interface{}) containers.Container {
newList := &List{} newList := &List{}
for i := 0; i < list.size; i++ { iterator := list.Iterator()
newList.Add(f(i, list.elements[i])) for iterator.Next() {
newList.Add(f(iterator.Index(), iterator.Value()))
} }
return newList return newList
} }
func (list *List) Select(f func(index interface{}, value interface{}) bool) containers.Container { func (list *List) Select(f func(index interface{}, value interface{}) bool) containers.Container {
newList := &List{} newList := &List{}
for i := 0; i < list.size; i++ { iterator := list.Iterator()
if f(i, list.elements[i]) { for iterator.Next() {
newList.Add(list.elements[i]) if f(iterator.Index(), iterator.Value()) {
newList.Add(iterator.Value())
} }
} }
return newList return newList
} }
func (list *List) Any(f func(index interface{}, value interface{}) bool) bool { func (list *List) Any(f func(index interface{}, value interface{}) bool) bool {
for i := 0; i < list.size; i++ { iterator := list.Iterator()
if f(i, list.elements[i]) { for iterator.Next() {
if f(iterator.Index(), iterator.Value()) {
return true return true
} }
} }
@ -212,8 +233,9 @@ func (list *List) Any(f func(index interface{}, value interface{}) bool) bool {
} }
func (list *List) All(f func(index interface{}, value interface{}) bool) bool { func (list *List) All(f func(index interface{}, value interface{}) bool) bool {
for i := 0; i < list.size; i++ { iterator := list.Iterator()
if !f(i, list.elements[i]) { for iterator.Next() {
if !f(iterator.Index(), iterator.Value()) {
return false return false
} }
} }
@ -221,9 +243,10 @@ func (list *List) All(f func(index interface{}, value interface{}) bool) bool {
} }
func (list *List) Find(f func(index interface{}, value interface{}) bool) (index interface{}, value interface{}) { func (list *List) Find(f func(index interface{}, value interface{}) bool) (index interface{}, value interface{}) {
for i := 0; i < list.size; i++ { iterator := list.Iterator()
if f(i, list.elements[i]) { for iterator.Next() {
return i, list.elements[i] if f(iterator.Index(), iterator.Value()) {
return iterator.Index(), iterator.Value()
} }
} }
return nil, nil return nil, nil
@ -234,23 +257,6 @@ type Iterator struct {
current int current int
} }
func (list *List) Iterator() Iterator {
return Iterator{list: list, current: -1}
}
func (iterator *Iterator) Next() bool {
iterator.current += 1
return iterator.list.withinRange(iterator.current)
}
func (iterator *Iterator) Value() interface{} {
return iterator.list.elements[iterator.current]
}
func (iterator *Iterator) Index() interface{} {
return iterator.current
}
func (list *List) String() string { func (list *List) String() string {
str := "ArrayList\n" str := "ArrayList\n"
values := []string{} values := []string{}

Loading…
Cancel
Save