Permalink
Browse files

Reduce() for []error

  • Loading branch information...
1 parent 2ff5317 commit 858c05ab818ddf211cdc7405ca9fcb57bf400027 @feyeleanor committed Apr 17, 2012
Showing with 90 additions and 109 deletions.
  1. +2 −2 enumerator.go
  2. +50 −106 reduce_slices.go
  3. +38 −1 reduce_slices_test.go
View
@@ -275,9 +275,9 @@ func (enum *Enumerator) Reduce(seed, f interface{}) (r interface{}) {
r = reduceComplex64Slice(enum, seq)
case []complex128:
r = reduceComplex128Slice(enum, seq)
-/* case []error:
+ case []error:
r = reduceErrorSlice(enum, seq)
-*/ case []float32:
+ case []float32:
r = reduceFloat32Slice(enum, seq)
case []float64:
r = reduceFloat64Slice(enum, seq)
View
@@ -197,128 +197,72 @@ func reduceComplex128Slice(enum *Enumerator, seq []complex128) (r complex128) {
}
return
}
-/*
-func reduceErrorSlice(enum *Enumerator, seq []error) (i int) {
+
+func reduceErrorSlice(enum *Enumerator, seq []error) (r error) {
switch f := enum.f.(type) {
- case func(error):
- i = enum.reduce(func(cursor int) {
- f(seq[cursor])
- })
- case func(error) bool:
- i = enum.reduce(func(cursor int) bool {
- return f(seq[cursor])
- })
- case func(int, error):
- i = enum.reduce(func(cursor int) {
- f(cursor, seq[cursor])
- })
- case func(int, error) bool:
- i = enum.reduce(func(cursor int) bool {
- return f(cursor, seq[cursor])
- })
- case func(interface{}, error):
- i = enum.reduce(func(cursor int) {
- f(cursor, seq[cursor])
- })
- case func(interface{}, error) bool:
- i = enum.reduce(func(cursor int) bool {
- return f(cursor, seq[cursor])
- })
- case func(interface{}):
- i = enum.reduce(func(cursor int) {
- f(seq[cursor])
- })
- case func(interface{}) bool:
- i = enum.reduce(func(cursor int) bool {
- return f(seq[cursor])
- })
- case func(int, interface{}):
- i = enum.reduce(func(cursor int) {
- f(cursor, seq[cursor])
- })
- case func(int, interface{}) bool:
- i = enum.reduce(func(cursor int) bool {
- return f(cursor, seq[cursor])
- })
- case func(interface{}, interface{}):
- i = enum.reduce(func(cursor int) {
- f(cursor, seq[cursor])
- })
- case func(interface{}, interface{}) bool:
- i = enum.reduce(func(cursor int) bool {
- return f(cursor, seq[cursor])
- })
- case func(R.Value):
- i = enum.reduce(func(cursor int) {
- f(R.ValueOf(seq[cursor]))
- })
- case func(R.Value) bool:
- i = enum.reduce(func(cursor int) bool {
- return f(R.ValueOf(seq[cursor]))
- })
- case func(int, R.Value):
- i = enum.reduce(func(cursor int) {
- f(cursor, R.ValueOf(seq[cursor]))
- })
- case func(int, R.Value) bool:
- i = enum.reduce(func(cursor int) bool {
- return f(cursor, R.ValueOf(seq[cursor]))
- })
- case func(interface{}, R.Value):
- i = enum.reduce(func(cursor int) {
- f(cursor, R.ValueOf(seq[cursor]))
+ case func(error, error) error:
+ r = enum.seed.(error)
+ enum.reduce(func(cursor int) {
+ r = f(r, seq[cursor])
})
- case func(interface{}, R.Value) bool:
- i = enum.reduce(func(cursor int) bool {
- return f(cursor, R.ValueOf(seq[cursor]))
+ case func(error, int, error) error:
+ r = enum.seed.(error)
+ enum.reduce(func(cursor int) {
+ r = f(r, cursor, seq[cursor])
})
- case func(R.Value, R.Value):
- i = enum.reduce(func(cursor int) {
- f(R.ValueOf(cursor), R.ValueOf(seq[cursor]))
+ case func(error, interface{}, error) error:
+ r = enum.seed.(error)
+ enum.reduce(func(cursor int) {
+ r = f(r, cursor, seq[cursor])
})
- case func(R.Value, R.Value) bool:
- i = enum.reduce(func(cursor int) bool {
- return f(R.ValueOf(cursor), R.ValueOf(seq[cursor]))
+ case func(interface{}, interface{}) interface{}:
+ ri := enum.seed
+ enum.reduce(func(cursor int) {
+ ri = f(ri, seq[cursor])
})
- case chan error:
- i = enum.reduce(func(cursor int) {
- f <- seq[cursor]
+ r = ri.(error)
+ case func(interface{}, int, interface{}) interface{}:
+ ri := enum.seed
+ enum.reduce(func(cursor int) {
+ ri = f(ri, cursor, seq[cursor])
})
- case chan interface{}:
- i = enum.reduce(func(cursor int) {
- f <- seq[cursor]
+ r = ri.(error)
+ case func(interface{}, interface{}, interface{}) interface{}:
+ ri := enum.seed
+ enum.reduce(func(cursor int) {
+ ri = f(ri, cursor, seq[cursor])
})
- case chan R.Value:
- i = enum.reduce(func(cursor int) {
- f <- R.ValueOf(seq[cursor])
+ r = ri.(error)
+ case func(R.Value, R.Value) R.Value:
+ rv := R.ValueOf(enum.seed)
+ enum.reduce(func(cursor int) {
+ rv = f(rv, R.ValueOf(seq[cursor]))
})
- case []chan error:
- i = enum.reduce(func(cursor int) {
- v := seq[cursor]
- for _, c := range f {
- c <- v
- }
+ r = rv.Interface().(error)
+ case func(R.Value, int, R.Value) R.Value:
+ rv := R.ValueOf(enum.seed)
+ enum.reduce(func(cursor int) {
+ rv = f(rv, cursor, R.ValueOf(seq[cursor]))
})
- case []chan interface{}:
- i = enum.reduce(func(cursor int) {
- v := seq[cursor]
- for _, c := range f {
- c <- v
- }
+ r = rv.Interface().(error)
+ case func(R.Value, interface{}, R.Value) R.Value:
+ rv := R.ValueOf(enum.seed)
+ enum.reduce(func(cursor int) {
+ rv = f(rv, cursor, R.ValueOf(seq[cursor]))
})
- case []chan R.Value:
- i = enum.reduce(func(cursor int) {
- v := seq[cursor]
- for _, c := range f {
- c <- R.ValueOf(v)
- }
+ r = rv.Interface().(error)
+ case func(R.Value, R.Value, R.Value) R.Value:
+ rv := R.ValueOf(enum.seed)
+ enum.reduce(func(cursor int) {
+ rv = f(rv, R.ValueOf(cursor), R.ValueOf(seq[cursor]))
})
+ r = rv.Interface().(error)
default:
panic(UNHANDLED_ITERATOR)
}
return
}
-*/
+
func reduceFloat32Slice(enum *Enumerator, seq []float32) (r float32) {
switch f := enum.f.(type) {
case func(float32, float32) float32:
View
@@ -184,6 +184,43 @@ func TestReduceComplex128Slice(t *testing.T) {
ConfirmReduce([]complex128{1, 2, 3, 4}, 10, 20)
}
+func TestReduceErrorSlice(t *testing.T) {
+ iterators := []interface{}{
+ func(seed, v error) error { return seed.(Error) | v.(Error) },
+ func(seed error, index int, v error) error { return seed.(Error) | v.(Error) },
+ func(seed error, index interface{}, v error) error { return seed.(Error) | v.(Error) },
+ func(seed, v interface{}) interface{} { return seed.(Error) | v.(Error) },
+ func(seed interface{}, index int, v interface{}) interface{} { return seed.(Error) | v.(Error) },
+ func(seed, index, v interface{}) interface{} { return seed.(Error) | v.(Error) },
+ func(seed, v R.Value) R.Value { return R.ValueOf(seed.Interface().(Error) | v.Interface().(Error)) },
+ func(seed R.Value, index int, v R.Value) R.Value { return R.ValueOf(seed.Interface().(Error) | v.Interface().(Error)) },
+ func(seed R.Value, index interface{}, v R.Value) R.Value { return R.ValueOf(seed.Interface().(Error) | v.Interface().(Error)) },
+ func(seed R.Value, index, v R.Value) R.Value { return R.ValueOf(seed.Interface().(Error) | v.Interface().(Error)) },
+ }
+
+ ConfirmReduce := func(o, seed, result interface{}) {
+ defer reportReductionError(t, o, seed)
+ for n, f := range iterators {
+ if v := Reduce(o, seed, f); v != result {
+ t.Fatalf("Reduce(%v, %v, iterators[%v]): should have returned %v but instead returned %v", o, seed, n, result, v)
+ }
+ }
+ }
+
+ ConfirmReduce([]error{}, Error(0), Error(0))
+ ConfirmReduce([]error{}, Error(8), Error(8))
+ ConfirmReduce([]error{Error(0)}, Error(0), Error(0))
+ ConfirmReduce([]error{Error(0)}, Error(8), Error(8))
+ ConfirmReduce([]error{Error(1)}, Error(0), Error(1))
+ ConfirmReduce([]error{Error(1)}, Error(8), Error(9))
+ ConfirmReduce([]error{Error(0), Error(1)}, Error(0), Error(1))
+ ConfirmReduce([]error{Error(0), Error(1)}, Error(8), Error(9))
+ ConfirmReduce([]error{Error(0), Error(1), Error(2)}, Error(0), Error(3))
+ ConfirmReduce([]error{Error(0), Error(1), Error(2)}, Error(8), Error(11))
+ ConfirmReduce([]error{Error(0), Error(1), Error(2), Error(4)}, Error(0), Error(7))
+ ConfirmReduce([]error{Error(0), Error(1), Error(2), Error(4)}, Error(8), Error(15))
+}
+
func TestReduceFloat32Slice(t *testing.T) {
iterators := []interface{}{
func(seed, v float32) float32 { return seed + v },
@@ -533,7 +570,7 @@ func TestReduceStringSlice(t *testing.T) {
defer reportReductionError(t, o, seed)
for n, f := range iterators {
if v := Reduce(o, seed, f); v != result {
- t.Fatalf("Reduce(%v, %v, iterators[%v]): should have returned %v [%T] but instead returned %v [%T]", o, seed, n, result, result, v, v)
+ t.Fatalf("Reduce(%v, %v, iterators[%v]): should have returned %v but instead returned %v", o, seed, n, result, result, v, v)
}
}
}

0 comments on commit 858c05a

Please sign in to comment.