-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IEnumerable增加Aggregate、Skip IQuery删除Clone 优化IEnumerable的First、Index、Values
- Loading branch information
ahl5esoft
committed
Jun 15, 2019
1 parent
e31e24d
commit 6f2dbcf
Showing
18 changed files
with
292 additions
and
257 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package underscore | ||
|
||
import "reflect" | ||
|
||
func (m enumerable) Aggregate(memo interface{}, fn interface{}) IEnumerable { | ||
fnRV := reflect.ValueOf(fn) | ||
iterator := m.GetEnumerator() | ||
memoRV := reflect.ValueOf(memo) | ||
for ok := iterator.MoveNext(); ok; ok = iterator.MoveNext() { | ||
memoRV = fnRV.Call([]reflect.Value{ | ||
memoRV, | ||
iterator.GetValue(), | ||
iterator.GetKey(), | ||
})[0] | ||
} | ||
return chainFromRV(memoRV) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package underscore | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func Benchmark_Aggregate(b *testing.B) { | ||
for n := 0; n < b.N; n++ { | ||
total := 0 | ||
Range2(1, 100, 1).Aggregate(make([]int, 0), func(memo []int, r, _ int) []int { | ||
memo = append(memo, r) | ||
memo = append(memo, -r) | ||
return memo | ||
}).Value(&total) | ||
} | ||
} | ||
|
||
func Benchmark_Aggregate_NoValue(b *testing.B) { | ||
for n := 0; n < b.N; n++ { | ||
Range2(1, 100, 1).Aggregate(make([]int, 0), func(memo []int, r, _ int) []int { | ||
memo = append(memo, r) | ||
memo = append(memo, -r) | ||
return memo | ||
}) | ||
} | ||
} | ||
|
||
func Test_Aggregate(t *testing.T) { | ||
dst := make([]int, 0) | ||
Chain2([]int{1, 2}).Aggregate(make([]int, 0), func(memo []int, n, _ int) []int { | ||
memo = append(memo, n) | ||
memo = append(memo, n+10) | ||
return memo | ||
}).Value(&dst) | ||
if !(len(dst) == 4 && dst[0] == 1 && dst[1] == 11 && dst[2] == 2 && dst[3] == 12) { | ||
t.Error(dst) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.