11package linq
22
3+ import "iter"
4+
35// Aggregate applies an accumulator function over a sequence.
46//
57// Aggregate method makes it simple to perform a calculation over a sequence of
6- // values. This method works by calling f() one time for each element in source
8+ // values. This method works by calling f() one time for each element in a source
79// except the first one. Each time f() is called, Aggregate passes both the
810// element from the sequence and an aggregated value (as the first argument to
9- // f()). The first element of source is used as the initial aggregate value. The
11+ // f()). The first element of the source is used as the initial aggregate value. The
1012// result of f() replaces the previous aggregated value.
1113//
1214// Aggregate returns the final result of f().
13- func (q Query ) Aggregate (f func (interface {}, interface {}) interface {}) interface {} {
14- next := q .Iterate ()
15+ func (q Query ) Aggregate (f func (accumulator , item any ) any ) any {
16+ next , stop := iter .Pull (q .Iterate )
17+ defer stop ()
1518
16- result , any := next ()
17- if ! any {
19+ result , ok := next ()
20+ if ! ok {
1821 return nil
1922 }
2023
@@ -30,7 +33,7 @@ func (q Query) Aggregate(f func(interface{}, interface{}) interface{}) interface
3033// - f is of type: func(TSource, TSource) TSource
3134//
3235// NOTE: Aggregate has better performance than AggregateT.
33- func (q Query ) AggregateT (f interface {}) interface {} {
36+ func (q Query ) AggregateT (f any ) any {
3437 fGenericFunc , err := newGenericFunc (
3538 "AggregateT" , "f" , f ,
3639 simpleParamValidator (newElemTypeSlice (new (genericType ), new (genericType )), newElemTypeSlice (new (genericType ))),
@@ -39,7 +42,7 @@ func (q Query) AggregateT(f interface{}) interface{} {
3942 panic (err )
4043 }
4144
42- fFunc := func (result interface {} , current interface {}) interface {} {
45+ fFunc := func (result any , current any ) any {
4346 return fGenericFunc .Call (result , current )
4447 }
4548
@@ -50,20 +53,18 @@ func (q Query) AggregateT(f interface{}) interface{} {
5053// specified seed value is used as the initial accumulator value.
5154//
5255// Aggregate method makes it simple to perform a calculation over a sequence of
53- // values. This method works by calling f() one time for each element in source
56+ // values. This method works by calling f() one time for each element in a source
5457// except the first one. Each time f() is called, Aggregate passes both the
5558// element from the sequence and an aggregated value (as the first argument to
5659// f()). The value of the seed parameter is used as the initial aggregate value.
5760// The result of f() replaces the previous aggregated value.
5861//
5962// Aggregate returns the final result of f().
60- func (q Query ) AggregateWithSeed (seed interface {},
61- f func (interface {}, interface {}) interface {}) interface {} {
62-
63- next := q .Iterate ()
63+ func (q Query ) AggregateWithSeed (seed any ,
64+ f func (accumulator , item any ) any ) any {
6465 result := seed
6566
66- for current , ok := next (); ok ; current , ok = next () {
67+ for current := range q . Iterate {
6768 result = f (result , current )
6869 }
6970
@@ -76,8 +77,8 @@ func (q Query) AggregateWithSeed(seed interface{},
7677//
7778// NOTE: AggregateWithSeed has better performance than
7879// AggregateWithSeedT.
79- func (q Query ) AggregateWithSeedT (seed interface {} ,
80- f interface {}) interface {} {
80+ func (q Query ) AggregateWithSeedT (seed any ,
81+ f any ) any {
8182 fGenericFunc , err := newGenericFunc (
8283 "AggregateWithSeed" , "f" , f ,
8384 simpleParamValidator (newElemTypeSlice (new (genericType ), new (genericType )), newElemTypeSlice (new (genericType ))),
@@ -86,7 +87,7 @@ func (q Query) AggregateWithSeedT(seed interface{},
8687 panic (err )
8788 }
8889
89- fFunc := func (result interface {} , current interface {}) interface {} {
90+ fFunc := func (result any , current any ) any {
9091 return fGenericFunc .Call (result , current )
9192 }
9293
@@ -106,14 +107,13 @@ func (q Query) AggregateWithSeedT(seed interface{},
106107//
107108// The final result of func is passed to resultSelector to obtain the final
108109// result of Aggregate.
109- func (q Query ) AggregateWithSeedBy (seed interface {} ,
110- f func (interface {}, interface {}) interface {} ,
111- resultSelector func (interface {}) interface {}) interface {} {
110+ func (q Query ) AggregateWithSeedBy (seed any ,
111+ f func (accumulator , item any ) any ,
112+ resultSelector func (any ) any ) any {
112113
113- next := q .Iterate ()
114114 result := seed
115115
116- for current , ok := next (); ok ; current , ok = next () {
116+ for current := range q . Iterate {
117117 result = f (result , current )
118118 }
119119
@@ -127,9 +127,9 @@ func (q Query) AggregateWithSeedBy(seed interface{},
127127//
128128// NOTE: AggregateWithSeedBy has better performance than
129129// AggregateWithSeedByT.
130- func (q Query ) AggregateWithSeedByT (seed interface {} ,
131- f interface {} ,
132- resultSelectorFn interface {}) interface {} {
130+ func (q Query ) AggregateWithSeedByT (seed any ,
131+ f any ,
132+ resultSelectorFn any ) any {
133133 fGenericFunc , err := newGenericFunc (
134134 "AggregateWithSeedByT" , "f" , f ,
135135 simpleParamValidator (newElemTypeSlice (new (genericType ), new (genericType )), newElemTypeSlice (new (genericType ))),
@@ -138,7 +138,7 @@ func (q Query) AggregateWithSeedByT(seed interface{},
138138 panic (err )
139139 }
140140
141- fFunc := func (result interface {} , current interface {}) interface {} {
141+ fFunc := func (result any , current any ) any {
142142 return fGenericFunc .Call (result , current )
143143 }
144144
@@ -150,7 +150,7 @@ func (q Query) AggregateWithSeedByT(seed interface{},
150150 panic (err )
151151 }
152152
153- resultSelectorFunc := func (result interface {}) interface {} {
153+ resultSelectorFunc := func (result any ) any {
154154 return resultSelectorGenericFunc .Call (result )
155155 }
156156
0 commit comments