Skip to content

Commit

Permalink
Removed typed files (#47)
Browse files Browse the repository at this point in the history
* Removed typed files
* Removed unnecessary tests
* Added mustPanicWithError utility function for tests
  • Loading branch information
cleitonmarx authored and ahmetb committed Dec 14, 2016
1 parent 0273c4d commit fdda798
Show file tree
Hide file tree
Showing 64 changed files with 2,343 additions and 3,631 deletions.
79 changes: 78 additions & 1 deletion aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,28 @@ func (q Query) Aggregate(
return result
}

// AggregateT is the typed version of Aggregate.
//
// NOTE: Aggregate method has better performance than AggregateT
//
// f is of type: func(TSource, TSource) TSource
func (q Query) AggregateT(f interface{}) interface{} {

fGenericFunc, err := newGenericFunc(
"AggregateT", "f", f,
simpleParamValidator(newElemTypeSlice(new(genericType), new(genericType)), newElemTypeSlice(new(genericType))),
)
if err != nil {
panic(err)
}

fFunc := func(result interface{}, current interface{}) interface{} {
return fGenericFunc.Call(result, current)
}

return q.Aggregate(fFunc)
}

// AggregateWithSeed applies an accumulator function over a sequence.
// The specified seed value is used as the initial accumulator value.
//
Expand All @@ -52,6 +74,27 @@ func (q Query) AggregateWithSeed(
return result
}

// AggregateWithSeedT is the typed version of AggregateWithSeed.
//
// NOTE: AggregateWithSeed method has better performance than AggregateWithSeedT
//
// f is of a type "func(TAccumulate, TSource) TAccumulate"
func (q Query) AggregateWithSeedT(seed interface{}, f interface{}) interface{} {
fGenericFunc, err := newGenericFunc(
"AggregateWithSeed", "f", f,
simpleParamValidator(newElemTypeSlice(new(genericType), new(genericType)), newElemTypeSlice(new(genericType))),
)
if err != nil {
panic(err)
}

fFunc := func(result interface{}, current interface{}) interface{} {
return fGenericFunc.Call(result, current)
}

return q.AggregateWithSeed(seed, fFunc)
}

// AggregateWithSeedBy applies an accumulator function over a sequence.
// The specified seed value is used as the initial accumulator value,
// and the specified function is used to select the result value.
Expand All @@ -63,7 +106,6 @@ func (q Query) AggregateWithSeed(
// as the initial aggregate value. The result of func replaces the previous aggregated value.
//
// The final result of func is passed to resultSelector to obtain the final result of Aggregate.
//
func (q Query) AggregateWithSeedBy(
seed interface{},
f func(interface{}, interface{}) interface{},
Expand All @@ -79,3 +121,38 @@ func (q Query) AggregateWithSeedBy(

return resultSelector(result)
}

// AggregateWithSeedByT is the typed version of AggregateWithSeedBy.
//
// NOTE: AggregateWithSeedBy method has better performance than AggregateWithSeedByT
//
// f is of a type "func(TAccumulate, TSource) TAccumulate"
//
// resultSelectorFn is of type "func(TAccumulate) TResult"
func (q Query) AggregateWithSeedByT(seed interface{}, f interface{}, resultSelectorFn interface{}) interface{} {
fGenericFunc, err := newGenericFunc(
"AggregateWithSeedByT", "f", f,
simpleParamValidator(newElemTypeSlice(new(genericType), new(genericType)), newElemTypeSlice(new(genericType))),
)
if err != nil {
panic(err)
}

fFunc := func(result interface{}, current interface{}) interface{} {
return fGenericFunc.Call(result, current)
}

resultSelectorGenericFunc, err := newGenericFunc(
"AggregateWithSeedByT", "resultSelectorFn", resultSelectorFn,
simpleParamValidator(newElemTypeSlice(new(genericType)), newElemTypeSlice(new(genericType))),
)
if err != nil {
panic(err)
}

resultSelectorFunc := func(result interface{}) interface{} {
return resultSelectorGenericFunc.Call(result)
}

return q.AggregateWithSeedBy(seed, fFunc, resultSelectorFunc)
}
54 changes: 54 additions & 0 deletions aggregate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ func TestAggregate(t *testing.T) {
}
}

func TestAggregateT_PanicWhenFunctionIsInvalid(t *testing.T) {
mustPanicWithError(t, "AggregateT: parameter [f] has a invalid function signature. Expected: 'func(T,T)T', actual: 'func(int,string,string)string'", func() {
From([]int{1, 1, 1, 2, 1, 2, 3, 4, 2}).AggregateT(func(x int, r string, i string) string {
if len(r) > len(i) {
return r
}
return i
})
})
}

func TestAggregateWithSeed(t *testing.T) {
input := []string{"apple", "mango", "orange", "banana", "grape"}
want := "passionfruit"
Expand All @@ -43,6 +54,17 @@ func TestAggregateWithSeed(t *testing.T) {
}
}

func TestAggregateWithSeedT_PanicWhenFunctionIsInvalid(t *testing.T) {
mustPanicWithError(t, "AggregateWithSeed: parameter [f] has a invalid function signature. Expected: 'func(T,T)T', actual: 'func(int,string,string)string'", func() {
From([]int{1, 1, 1, 2, 1, 2, 3, 4, 2}).AggregateWithSeedT(3, func(x int, r string, i string) string {
if len(r) > len(i) {
return r
}
return i
})
})
}

func TestAggregateWithSeedBy(t *testing.T) {
input := []string{"apple", "mango", "orange", "passionfruit", "grape"}
want := "PASSIONFRUIT"
Expand All @@ -63,3 +85,35 @@ func TestAggregateWithSeedBy(t *testing.T) {
t.Errorf("From(%v).AggregateWithSeed()=%v expected %v", input, r, want)
}
}

func TestAggregateWithSeedByT_PanicWhenFunctionIsInvalid(t *testing.T) {
mustPanicWithError(t, "AggregateWithSeedByT: parameter [f] has a invalid function signature. Expected: 'func(T,T)T', actual: 'func(int,string,string)string'", func() {
From([]int{1, 1, 1, 2, 1, 2, 3, 4, 2}).AggregateWithSeedByT(3,
func(x int, r string, i string) string {
if len(r) > len(i) {
return r
}
return i
},
func(r string) string {
return r
},
)
})
}

func TestAggregateWithSeedByT_PanicWhenResultSelectorFnIsInvalid(t *testing.T) {
mustPanicWithError(t, "AggregateWithSeedByT: parameter [resultSelectorFn] has a invalid function signature. Expected: 'func(T)T', actual: 'func(string,int)string'", func() {
From([]int{1, 1, 1, 2, 1, 2, 3, 4, 2}).AggregateWithSeedByT(3,
func(x int, r int) int {
if x > r {
return x
}
return r
},
func(r string, t int) string {
return r
},
)
})
}
88 changes: 0 additions & 88 deletions aggregatetyped.go

This file was deleted.

Loading

0 comments on commit fdda798

Please sign in to comment.