Skip to content

Commit

Permalink
v2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
kalaninja committed Aug 29, 2016
1 parent 0cb9dbb commit b056e5e
Show file tree
Hide file tree
Showing 54 changed files with 3,839 additions and 4,224 deletions.
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
*.o
*.a
*.so
*.out

# Folders
_obj
Expand All @@ -22,3 +21,9 @@ _testmain.go

*.exe
*.test
*.prof

### Code ###
# Visual Studio Code - https://code.visualstudio.com/
.settings/
.vscode/
25 changes: 16 additions & 9 deletions .travis.yml
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
27 changes: 17 additions & 10 deletions LICENSE
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.
100 changes: 0 additions & 100 deletions README.md

This file was deleted.

53 changes: 53 additions & 0 deletions aggregate.go
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
}
43 changes: 43 additions & 0 deletions aggregate_test.go
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)
}
}
Loading

0 comments on commit b056e5e

Please sign in to comment.