Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/alihammad-gist/streamy
lazy loaded slice/array filtering
https://github.com/alihammad-gist/streamy
Last synced: 24 days ago
JSON representation
lazy loaded slice/array filtering
- Host: GitHub
- URL: https://github.com/alihammad-gist/streamy
- Owner: alihammad-gist
- Created: 2014-12-05T10:31:56.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2014-12-08T17:46:30.000Z (about 10 years ago)
- Last Synced: 2024-11-12T06:27:08.047Z (3 months ago)
- Language: Go
- Homepage:
- Size: 125 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
### Streamy - Lazy loaded Filtering
Filter arrays/slices using lazy loaded functions.
#### GetFirst / GetLast
Use GetLast to get the Last value that passes all the filters successfully or use GetFirst
to get the First value that passes through provided filters```go
package mainimport (
"fmt"
"github.com/alihammad-gist/streamy"
)func main() {
slice := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}// Get last even number which is also divisible by 4 [made up problem using 2 filters]
l, ok := streamy.New(len(slice)).Filter(
func(i int) bool {
fmt.Println("Even check: ", i)
if slice[i]%2 == 0 {
return true
}
return false
},
).Filter(
func(i int) bool {
fmt.Println("DivisBy 4 check: ", i)
if slice[i]%4 == 0 {
return true
}
return false
},
).GetLast()fmt.Println("ok: ", ok, " - num: ", slice[l])
}
```Output
```
Even check: 10
DivisBy 4 check: 10
Even check: 9
Even check: 8
DivisBy 4 check: 8
ok: true - num: 8
```#### GetN(resultCount, order)
GetN requires you pass in 'n' (count of matched values). Iteration stops when resultCount is achieved, pass 0 to get all matched indexes. Second argument is order the slice should be evaluated in `streamy.OrderAsc` or `streamy.OrderDesc`. Note, returned list of matched indexes will follow the same order.```go
package mainimport (
"fmt"
"github.com/alihammad-gist/streamy"
)func main() {
slice :=
[]int{
0,
1,
2,
3,
4,
5,
6,
7,
8, 9, 10, 11, 12, 13, 14, 15}// Get First 2 odd numbers which are
// greater thn 5
res := streamy.New(len(slice)).Filter(
func(i int) bool {
fmt.Println("> 5 check:", i)
if slice[i] > 5 {
return true
}
return false
},
).Filter(
func(i int) bool {
fmt.Println("odd check:", i)
if slice[i]%2 == 1 {
return true
}
return false
},
).GetN(2, streamy.OrderAsc)for _, i := range res {
fmt.Println("Match:", slice[i])
}
}
```Output
```
> 5 check: 0
> 5 check: 1
> 5 check: 2
> 5 check: 3
> 5 check: 4
> 5 check: 5
> 5 check: 6
odd check: 6
> 5 check: 7
odd check: 7
> 5 check: 8
odd check: 8
> 5 check: 9
odd check: 9
Match: 7
Match: 9
```See main_test.go for more examples.