-
Notifications
You must be signed in to change notification settings - Fork 2
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
1 changed file
with
48 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,49 @@ | ||
# glinq | ||
linq for golang | ||
LINQ for Golang | ||
|
||
examples: | ||
```go | ||
var employees = NewListOf([]Employee{ | ||
{"Hao", 44, 0, 8000.5}, | ||
{"Bob", 36, 10, 5000.5}, | ||
{"Alice", 23, 5, 9000.0}, | ||
{"Jack", 26, 0, 4000.0}, | ||
{"Tom", 48, 9, 7500.75}, | ||
{"Marry", 29, 0, 6000.0}, | ||
{"Mike", 38, 8, 4000.3}, | ||
}) | ||
|
||
func TestEmployeeList(t *testing.T) { | ||
// 添加 Max 员工 | ||
employees.Push(Employee{"Max", 26, 0, 4000.0}) | ||
// 所有人的薪水 | ||
totalPaySalary := Reduce(employees.ToQueryable(), 0.0, func(result float64, employee Employee) float64 { | ||
return result + employee.Salary | ||
}) | ||
assert.Equal(t, totalPaySalary, 43502.05+4000.0) | ||
|
||
//统计年龄大于40岁的员工数 | ||
orderCount := employees.ToQueryable().Where(func(employee Employee) bool { | ||
return employee.Age > 35 | ||
}).Count() | ||
assert.Equal(t, orderCount, 4) | ||
|
||
//统计薪水超过 6000元的员工数 | ||
moreThan6000Count := employees.ToQueryable().Where(func(employee Employee) bool { | ||
return employee.Salary >= 6000 | ||
}).Count() | ||
assert.Equal(t, moreThan6000Count, 4) | ||
|
||
//统计年龄小于30岁员工要支付的所有薪水 [ Max: 4000.0, Alice: 9000.0 ,Jack: 4000.0, Marry: 6000.0 ] = 23000.0 | ||
employeeQuery := employees.ToQueryable() | ||
// Reduce are equal Sum of Map to Queryable | ||
youngerTotalPaySalary := Reduce(Map(employeeQuery.Where(func(employee Employee) bool { | ||
return employee.Age < 30 | ||
}), func(e Employee) float64 { | ||
return e.Salary | ||
}), 0.0, func(result float64, item float64) float64 { | ||
return result + item | ||
}) | ||
assert.Equal(t, youngerTotalPaySalary, 23000.0) | ||
} | ||
``` |