-
Notifications
You must be signed in to change notification settings - Fork 223
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
54 changed files
with
3,839 additions
and
4,224 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 |
---|---|---|
@@ -1,13 +1,20 @@ | ||
sudo: false | ||
|
||
language: go | ||
go: go1.6 | ||
install: | ||
- go get -v golang.org/x/tools/cmd/cover | ||
- go get -t github.com/smartystreets/goconvey | ||
- go get github.com/jacobsa/oglematchers | ||
go: | ||
- 1.6.2 | ||
- tip | ||
|
||
before_install: | ||
- go get github.com/mattn/goveralls | ||
- go get golang.org/x/tools/cmd/cover | ||
- go get -u github.com/golang/lint/golint | ||
|
||
script: | ||
- test -z "$(gofmt -s -l -w . | tee /dev/stderr)" | ||
- test -z "$(golint . | tee /dev/stderr)" | ||
- go vet -v . | ||
- go test -v -cover ./... | ||
- go vet -x ./... | ||
- golint ./... | ||
- go test -v ./... | ||
- go test -covermode=count -coverprofile=profile.cov | ||
|
||
after_script: | ||
- goveralls -coverprofile=profile.cov -service=travis-ci |
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 |
---|---|---|
@@ -1,14 +1,21 @@ | ||
Copyright 2013 Ahmet Alp Balkan | ||
The MIT License (MIT) | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
Copyright (c) 2016 Alexander Kalankhodzhaev | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,53 @@ | ||
package linq | ||
|
||
// Aggregate applies an accumulator function over a sequence. | ||
// | ||
// Aggregate method makes it simple to perform a calculation over a sequence of values. | ||
// This method works by calling f() one time for each element in source | ||
// except the first one. Each time f() is called, Aggregate passes both | ||
// the element from the sequence and an aggregated value (as the first argument to f()). | ||
// The first element of source is used as the initial aggregate value. | ||
// The result of f() replaces the previous aggregated value. | ||
// | ||
// Aggregate returns the final result of f(). | ||
func (q Query) Aggregate( | ||
f func(interface{}, interface{}) interface{}) interface{} { | ||
next := q.Iterate() | ||
|
||
result, any := next() | ||
if !any { | ||
return nil | ||
} | ||
|
||
for current, ok := next(); ok; current, ok = next() { | ||
result = f(result, current) | ||
} | ||
|
||
return result | ||
} | ||
|
||
// AggregateWithSeed applies an accumulator function over a sequence. | ||
// The specified seed value is used as the initial accumulator value. | ||
// | ||
// Aggregate method makes it simple to perform a calculation over a sequence of values. | ||
// This method works by calling f() one time for each element in source | ||
// except the first one. Each time f() is called, Aggregate passes both | ||
// the element from the sequence and an aggregated value (as the first argument to f()). | ||
// The value of the seed parameter is used as the initial aggregate value. | ||
// The result of f() replaces the previous aggregated value. | ||
// | ||
// Aggregate returns the final result of f(). | ||
func (q Query) AggregateWithSeed( | ||
seed interface{}, | ||
f func(interface{}, interface{}) interface{}, | ||
) interface{} { | ||
|
||
next := q.Iterate() | ||
result := seed | ||
|
||
for current, ok := next(); ok; current, ok = next() { | ||
result = f(result, current) | ||
} | ||
|
||
return result | ||
} |
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,43 @@ | ||
package linq | ||
|
||
import "testing" | ||
|
||
func TestAggregate(t *testing.T) { | ||
tests := []struct { | ||
input interface{} | ||
want interface{} | ||
}{ | ||
{[]string{"apple", "mango", "orange", "passionfruit", "grape"}, "passionfruit"}, | ||
{[]string{}, nil}, | ||
} | ||
|
||
for _, test := range tests { | ||
r := From(test.input).Aggregate(func(r interface{}, i interface{}) interface{} { | ||
if len(r.(string)) > len(i.(string)) { | ||
return r | ||
} | ||
return i | ||
}) | ||
|
||
if r != test.want { | ||
t.Errorf("From(%v).Aggregate()=%v expected %v", test.input, r, test.want) | ||
} | ||
} | ||
} | ||
|
||
func TestAggregateWithSeed(t *testing.T) { | ||
input := []string{"apple", "mango", "orange", "banana", "grape"} | ||
want := "passionfruit" | ||
|
||
r := From(input).AggregateWithSeed(want, | ||
func(r interface{}, i interface{}) interface{} { | ||
if len(r.(string)) > len(i.(string)) { | ||
return r | ||
} | ||
return i | ||
}) | ||
|
||
if r != want { | ||
t.Errorf("From(%v).AggregateWithSeed()=%v expected %v", input, r, want) | ||
} | ||
} |
Oops, something went wrong.