@@ -86,7 +86,7 @@ func TestEachIndexable(t *testing.T) {

ConfirmEachIndexable := func(s Indexable) {
ConfirmEach(s, func(v interface{}) {
if v != s.At(count) {
if v != s.AtOffset(count) {
t.Fatalf("element %v erroneously reported as %v", count, v)
}
count++
@@ -95,15 +95,15 @@ func TestEachIndexable(t *testing.T) {
ConfirmEach(s, func(i int, v interface{}) {
switch {
case i != count: t.Fatalf("index %v erroneously reported as %v", count, i)
case v != s.At(i): t.Fatalf("element %v erroneously reported as %v", count, v)
case v != s.AtOffset(i): t.Fatalf("element %v erroneously reported as %v", count, v)
}
count++
})

ConfirmEach(s, func(k, v interface{}) {
switch {
case k != count: t.Fatalf("index %v erroneously reported as %v", count, k)
case v != s.At(count): t.Fatalf("element %v erroneously reported as %v", count, v)
case v != s.AtOffset(count): t.Fatalf("element %v erroneously reported as %v", count, v)
}
count++
})
@@ -117,7 +117,7 @@ func TestEachIndexable(t *testing.T) {
})

ConfirmEach(s, func(v R.Value) {
if v.Interface() != s.At(count) {
if v.Interface() != s.AtOffset(count) {
t.Fatalf("element %v erroneously reported as %v", count, v.Interface())
}
count++
@@ -126,23 +126,23 @@ func TestEachIndexable(t *testing.T) {
ConfirmEach(s, func(i int, v R.Value) {
switch {
case i != count: t.Fatalf("index %v erroneously reported as %v", count, i)
case v.Interface() != s.At(i): t.Fatalf("element %v erroneously reported as %v", count, v)
case v.Interface() != s.AtOffset(i): t.Fatalf("element %v erroneously reported as %v", count, v)
}
count++
})

ConfirmEach(s, func(k interface{}, v R.Value) {
switch {
case k != count: t.Fatalf("index %v erroneously reported as %v", count, k)
case v.Interface() != s.At(count): t.Fatalf("element %v erroneously reported as %v", count, v)
case v.Interface() != s.AtOffset(count): t.Fatalf("element %v erroneously reported as %v", count, v)
}
count++
})

ConfirmEach(s, func(k, v R.Value) {
switch {
case k.Interface() != count: t.Fatalf("index %v erroneously reported as %v", count, k)
case v.Interface() != s.At(count): t.Fatalf("element %v erroneously reported as %v", count, v)
case v.Interface() != s.AtOffset(count): t.Fatalf("element %v erroneously reported as %v", count, v)
}
count++
})
@@ -164,7 +164,7 @@ func TestEachIndexable(t *testing.T) {
})

ConfirmEach(s, func(v int) {
if v != s.At(count) {
if v != s.AtOffset(count) {
t.Fatalf("element %v erroneously reported as %v", count, v)
}
count++
@@ -173,7 +173,7 @@ func TestEachIndexable(t *testing.T) {
ConfirmEach(s, func(i, v int) {
switch {
case i != count: t.Fatalf("index %v erroneously reported as %v", count, i)
case v != s.At(i): t.Fatalf("element %v erroneously reported as %v", count, v)
case v != s.AtOffset(i): t.Fatalf("element %v erroneously reported as %v", count, v)
}
count++
})
@@ -208,14 +208,14 @@ func TestEachMappable(t *testing.T) {
})

ConfirmEach(m, func(i int, v interface{}) {
if v != m.Lookup(i) {
if v != m.StoredAs(i) {
t.Fatalf("element %v erroneously reported as %v", i, v)
}
count++
})

ConfirmEach(m, func(k, v interface{}) {
if v != m.Lookup(k) {
if v != m.StoredAs(k) {
t.Fatalf("element %v erroneously reported as %v", k, v)
}
count++
@@ -234,21 +234,21 @@ func TestEachMappable(t *testing.T) {
})

ConfirmEach(m, func(i int, v R.Value) {
if v.Interface() != m.Lookup(i) {
if v.Interface() != m.StoredAs(i) {
t.Fatalf("element %v erroneously reported as %v", i, v)
}
count++
})

ConfirmEach(m, func(k interface{}, v R.Value) {
if v.Interface() != m.Lookup(k) {
if v.Interface() != m.StoredAs(k) {
t.Fatalf("element %v erroneously reported as %v", k, v)
}
count++
})

ConfirmEach(m, func(k, v R.Value) {
if v.Interface() != m.Lookup(k.Interface()) {
if v.Interface() != m.StoredAs(k.Interface()) {
t.Fatalf("element %v erroneously reported as %v", k.Interface(), v.Interface())
}
count++
@@ -275,7 +275,7 @@ func TestEachMappable(t *testing.T) {
})

ConfirmEach(m, func(i, v int) {
if v != m.Lookup(i) {
if v != m.StoredAs(i) {
t.Fatalf("element %v erroneously reported as %v", i, v)
}
count++
@@ -288,14 +288,14 @@ func TestEachMappable(t *testing.T) {

m := mappable_string_map{"0": 0, "1": 1, "2": 2, "3": 3}
ConfirmEach(m, func(k string, v interface{}) {
if v != m.Lookup(k) {
if v != m.StoredAs(k) {
t.Fatalf("element %v erroneously reported as %v", k, v)
}
count++
})

ConfirmEach(m, func(k string, v R.Value) {
if v.Interface() != m.Lookup(k) {
if v.Interface() != m.StoredAs(k) {
t.Fatalf("element %v erroneously reported as %v", k, v.Interface())
}
count++
@@ -528,48 +528,47 @@ func TestEachMap(t *testing.T) {
count++
})

ConfirmEach(MS, func(k, v interface{}) {
if v != MS[k.(string)] {
t.Fatalf("element %v erroneously reported as %v", k, v)
}
count++
})


ConfirmEach(MS, func(k string, v R.Value) {
if v.Interface() != MS[k] {
t.Fatalf("element %v erroneously reported as %v", k, v)
}
count++
})

ConfirmEach(MS, func(k interface{}, v R.Value) {
if v.Interface() != MS[k.(string)] {
t.Fatalf("element %v erroneously reported as %v", k, v)
}
count++
})
}

func TestEachChannel(t *testing.T) {
var count int
var index int

ConfirmEach := func(s []int, f interface{}) {
var v int
var count int

C := make(chan int)
Generate := func(s []int) (c chan int) {
c = make(chan int)
go func() {
for count, v = range s {
C <- v
for _, v := range s {
c <- v
count++
}
close(C)
close(c)
}()
return c
}

ConfirmEach := func(s []int, f interface{}) {
count = 0
index = 0
switch {
case !Each(Generate(s), f): t.Fatalf("failed to perform iteration %v over %v", f, s)
case count != len(s): t.Fatalf("total itmes produced should be %v but are %v", len(s), count)
case index != len(s): t.Fatalf("total iterations should be %v but are %v", len(s), index)
}
}

ConfirmVariadicEach := func(s []int, f interface{}) {
count = 0
index = 0
Each(C, f)
if count != len(s) - 1 {
t.Fatalf("total iterations should be %v but is %v", len(s) - 1, count)
switch {
case !Each(Generate(s), f): t.Fatalf("failed to perform iteration %v over %v", f, s)
case count != len(s): t.Fatalf("total items produced should be %v but are %v", len(s), count)
case index != 1: t.Fatalf("total iterations should be 1 but are %v", index)
}
}

@@ -584,7 +583,85 @@ func TestEachChannel(t *testing.T) {
ConfirmEach(S, func(i int, v interface{}) {
switch {
case i != index: t.Fatalf("index %v erroneously reported as %v", index, i)
case v != S[i]: t.Fatalf("value %v erroneously reported as %v", S[9], v)
case v != S[i]: t.Fatalf("value %v erroneously reported as %v", S[i], v)
}
index++
})

ConfirmEach(S, func(i, v interface{}) {
switch {
case i != index: t.Fatalf("index %v erroneously reported as %v", index, i)
case v != S[i.(int)]: t.Fatalf("value %v erroneously reported as %v", S[i.(int)], v)
}
index++
})

ConfirmVariadicEach(S, func(v ...interface{}) {
switch {
case index != 0: t.Fatalf("variadic function erroneously called %v times", index)
case len(v) != Len(S): t.Fatalf("variadic slice of keys for %v erroneously passed as %v", S, v)
}
index++
})

ConfirmEach(S, func(v R.Value) {
if v.Interface() != S[index] {
t.Fatalf("index %v erroneously reported as %v", index, v.Interface())
}
index++
})

ConfirmEach(S, func(i int, v R.Value) {
switch {
case i != index: t.Fatalf("index %v erroneously reported as %v", index, i)
case v.Interface() != S[i]: t.Fatalf("value %v erroneously reported as %v", S[i], v.Interface())
}
index++
})

ConfirmEach(S, func(i interface{}, v R.Value) {
switch {
case i != index: t.Fatalf("index %v erroneously reported as %v", index, i)
case v.Interface() != S[i.(int)]: t.Fatalf("value %v erroneously reported as %v", S[i.(int)], v.Interface())
}
index++
})

ConfirmEach(S, func(i, v R.Value) {
switch x := int(i.Int()); {
case x != index: t.Fatalf("index %v erroneously reported as %v", index, x)
case v.Interface() != S[x]: t.Fatalf("value %v erroneously reported as %v", S[x], v.Interface())
}
index++
})

ConfirmVariadicEach(S, func(v ...R.Value) {
switch {
case index != 0: t.Fatalf("variadic function erroneously called %v times", index)
case len(v) != Len(S): t.Fatalf("variadic slice of keys for %v erroneously passed as %v", S, v)
}
index++
})

ConfirmVariadicEach(S, func(v ...int) {
switch {
case index != 0: t.Fatalf("variadic function erroneously called %v times", index)
case len(v) != Len(S): t.Fatalf("variadic slice of keys for %v erroneously passed as %v", S, v)
}
index++
})

ConfirmEach(S, func(v int) {
if v != S[index] {
t.Fatalf("index %v erroneously reported as %v", index, v)
}
index++
})

ConfirmEach(S, func(i, v int) {
switch {
case i != index: t.Fatalf("index %v erroneously reported as %v", index, i)
case v != S[i]: t.Fatalf("value %v erroneously reported as %v", S[i], v)
}
index++
})
@@ -52,7 +52,7 @@ func (i indexable_slice) Len() int {
return len(i)
}

func (i indexable_slice) At(x int) interface{} {
func (i indexable_slice) AtOffset(x int) interface{} {
return i[x]
}

@@ -63,7 +63,7 @@ func (f indexable_function) Len() int {
return 10
}

func (f indexable_function) At(x int) interface{} {
func (f indexable_function) AtOffset(x int) interface{} {
return f(x)
}

@@ -74,7 +74,7 @@ func (m mappable_slice) Len() int {
return len(m)
}

func (m mappable_slice) Lookup(key interface{}) interface{} {
func (m mappable_slice) StoredAs(key interface{}) interface{} {
return m[key.(int)]
}

@@ -94,7 +94,7 @@ func (m mappable_map) Len() int {
return len(m)
}

func (m mappable_map) Lookup(key interface{}) interface{} {
func (m mappable_map) StoredAs(key interface{}) interface{} {
return m[key.(int)]
}

@@ -115,7 +115,7 @@ func (m mappable_string_map) Len() int {
return len(m)
}

func (m mappable_string_map) Lookup(key interface{}) interface{} {
func (m mappable_string_map) StoredAs(key interface{}) interface{} {
return m[key.(string)]
}

@@ -136,7 +136,7 @@ func (f mappable_function) Len() int {
return 10
}

func (f mappable_function) Lookup(x interface{}) interface{} {
func (f mappable_function) StoredAs(x interface{}) interface{} {
return f(x.(int))
}

@@ -6,15 +6,15 @@ func reduceIndexable(c Indexable, seed, f interface{}) (r interface{}, ok bool)
end := Len(c)
switch f := f.(type) {
case func(interface{}, interface{}) interface{}: for i := 0; i < end; i++ {
seed = f(seed, c.At(i))
seed = f(seed, c.AtOffset(i))
}
ok = true

default: if f := reflect.ValueOf(f); f.Kind() == reflect.Func {
switch f.Type().NumIn() {
case 2: p := make([]reflect.Value, 2, 2)
for i := 0; i < end; i++ {
p[0], p[1] = reflect.ValueOf(i), reflect.ValueOf(c.At(i))
p[0], p[1] = reflect.ValueOf(i), reflect.ValueOf(c.AtOffset(i))
f.Call(p)
}
ok = true
@@ -11,11 +11,11 @@ type Confined interface {
}

type Indexable interface {
At(index int) interface{}
AtOffset(index int) interface{}
}

type Mappable interface {
Lookup(key interface{}) interface{}
StoredAs(key interface{}) interface{}
Keys() interface{}
}

@@ -5,30 +5,30 @@ import R "reflect"
func whileIndexable(container Indexable, r bool, f interface{}) (count int) {
if end := Len(container); end > 0 {
switch f := f.(type) {
case func(interface{}) bool: if f(container.At(0)) == r {
case func(interface{}) bool: if f(container.AtOffset(0)) == r {
count = 1
for i := 1; i < end; i++ {
if f(container.At(i)) != r {
if f(container.AtOffset(i)) != r {
break
}
count++
}
}

case func(int, interface{}) bool: if f(0, container.At(0)) == r {
case func(int, interface{}) bool: if f(0, container.AtOffset(0)) == r {
count = 1
for i := 1; i < end; i++ {
if f(i, container.At(i)) != r {
if f(i, container.AtOffset(i)) != r {
break
}
count++
}
}

case func(interface{}, interface{}) bool: if f(0, container.At(0)) == r {
case func(interface{}, interface{}) bool: if f(0, container.AtOffset(0)) == r {
count = 1
for i := 1; i < end; i++ {
if f(i, container.At(i)) != r {
if f(i, container.AtOffset(i)) != r {
break
}
count++
@@ -37,7 +37,7 @@ func whileIndexable(container Indexable, r bool, f interface{}) (count int) {

case func(...interface{}) bool: p := make([]interface{}, end, end)
for i := 0; i < end; i++ {
p[i] = container.At(i)
p[i] = container.AtOffset(i)
}
if f(p...) == r {
count = 1
@@ -46,34 +46,34 @@ func whileIndexable(container Indexable, r bool, f interface{}) (count int) {

case func(bool, ...interface{}) int: p := make([]interface{}, end, end)
for i := 0; i < end; i++ {
p[i] = container.At(i)
p[i] = container.AtOffset(i)
}
count = f(r, p...)

case func(R.Value) bool: if f(R.ValueOf(container.At(0))) == r {
case func(R.Value) bool: if f(R.ValueOf(container.AtOffset(0))) == r {
count = 1
for i := 1; i < end; i++ {
if f(R.ValueOf(container.At(i))) != r {
if f(R.ValueOf(container.AtOffset(i))) != r {
break
}
count++
}
}

case func(int, R.Value) bool: if f(0, R.ValueOf(container.At(0))) == r {
case func(int, R.Value) bool: if f(0, R.ValueOf(container.AtOffset(0))) == r {
count = 1
for i := 1; i < end; i++ {
if f(i, R.ValueOf(container.At(i))) != r {
if f(i, R.ValueOf(container.AtOffset(i))) != r {
break
}
count++
}
}

case func(R.Value, R.Value) bool: if f(R.ValueOf(0), R.ValueOf(container.At(0))) == r {
case func(R.Value, R.Value) bool: if f(R.ValueOf(0), R.ValueOf(container.AtOffset(0))) == r {
count = 1
for i := 1; i < end; i++ {
if f(R.ValueOf(i), R.ValueOf(container.At(i))) != r {
if f(R.ValueOf(i), R.ValueOf(container.AtOffset(i))) != r {
break
}
count++
@@ -82,15 +82,15 @@ func whileIndexable(container Indexable, r bool, f interface{}) (count int) {

case func(...R.Value) bool: p := make([]R.Value, end, end)
for i := 0; i < end; i++ {
p[i] = R.ValueOf(container.At(i))
p[i] = R.ValueOf(container.AtOffset(i))
}
if f(p...) == r {
count = 1
}

case func(bool, ...R.Value) int: p := make([]R.Value, end, end)
for i := 0; i < end; i++ {
p[i] = R.ValueOf(container.At(i))
p[i] = R.ValueOf(container.AtOffset(i))
}
count = f(r, p...)

@@ -100,7 +100,7 @@ func whileIndexable(container Indexable, r bool, f interface{}) (count int) {
case 1: // f(...v) bool
p := make([]R.Value, end, end)
for i := 0; i < end; i++ {
p[i] = R.ValueOf(container.At(i))
p[i] = R.ValueOf(container.AtOffset(i))
}
if f.Call(p)[0].Bool() == r {
count = 1
@@ -110,7 +110,7 @@ func whileIndexable(container Indexable, r bool, f interface{}) (count int) {
p := make([]R.Value, 1, 4)
p[0] = R.ValueOf(r)
for i := 0; i < end; i++ {
p = append(p, R.ValueOf(container.At(i)))
p = append(p, R.ValueOf(container.AtOffset(i)))
}
count = int(f.Call(p)[0].Int())
}
@@ -119,7 +119,7 @@ func whileIndexable(container Indexable, r bool, f interface{}) (count int) {
case 1: // f(v) bool
p := make([]R.Value, 1, 1)
for ; count < end; count++ {
p[0] = R.ValueOf(container.At(count))
p[0] = R.ValueOf(container.AtOffset(count))
if f.Call(p)[0].Bool() != r {
break
}
@@ -129,7 +129,7 @@ func whileIndexable(container Indexable, r bool, f interface{}) (count int) {
p := make([]R.Value, 2, 2)
for ; count < end; count++ {
p[0] = R.ValueOf(count)
p[1] = R.ValueOf(container.At(count))
p[1] = R.ValueOf(container.AtOffset(count))
if f.Call(p)[0].Bool() != r {
break
}