-
Notifications
You must be signed in to change notification settings - Fork 0
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
Javier Orfo
committed
Nov 5, 2024
1 parent
e614115
commit 2416387
Showing
10 changed files
with
175 additions
and
5 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 was deleted.
Oops, something went wrong.
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,47 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/javiorfo/steams" | ||
"github.com/javiorfo/steams/examples/data" | ||
) | ||
|
||
func main() { | ||
steams.OfSlice(data.PersonsWithPets). | ||
Filter(func(p data.Person) bool { | ||
return p.Age > 21 | ||
}). | ||
Peek(func(p data.Person) { fmt.Println("After Filter => Person:", p.Name) }). | ||
FlatMapToAny(func(p data.Person) steams.Steam[any] { | ||
results := make(steams.List[any], 0) | ||
for _, v := range p.Pets { | ||
results = append(results, v) | ||
} | ||
return results | ||
}). | ||
Peek(func(p any) { fmt.Println("After FlatMap = Pet:", p.(data.Pet).Name) }). | ||
Filter(func(p any) bool { | ||
animal, ok := p.(data.Pet) | ||
if ok { | ||
if animal.Type == data.CAT { | ||
return true | ||
} | ||
} | ||
return false | ||
}). | ||
Peek(func(p any) { fmt.Println("After second Filter => Pet:", p.(data.Pet).Name) }). | ||
GetCompared(comparator).IfPresentOrElse(print, func() { fmt.Println("No results") }) | ||
|
||
} | ||
|
||
func comparator(a any, b any) bool { | ||
ageA := a.(data.Pet).Age | ||
ageB := b.(data.Pet).Age | ||
return ageA < ageB | ||
} | ||
|
||
func print(cat any) { | ||
younger := cat.(data.Pet) | ||
fmt.Printf("The younger cat of the list is %s, age %d", younger.Name, younger.Age) | ||
} |
File renamed without changes.
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,23 @@ | ||
package data | ||
|
||
type Person struct { | ||
Name string | ||
Age int | ||
Pets []Pet | ||
} | ||
|
||
type Pet struct { | ||
Name string | ||
Type string | ||
Age int | ||
} | ||
|
||
const DOG = "DOG" | ||
const CAT = "CAT" | ||
|
||
var PersonsWithPets = []Person{ | ||
{Name: "Carl", Age: 34, Pets: []Pet{}}, | ||
{Name: "John", Age: 20, Pets: []Pet{{Name: "Bobby", Type: DOG, Age: 2}, {Name: "Mike", Type: DOG, Age: 12}}}, | ||
{Name: "Grace", Age: 40, Pets: []Pet{{Name: "Pepe", Type: DOG, Age: 4}, {Name: "Snowball", Type: CAT, Age: 8}}}, | ||
{Name: "Robert", Age: 40, Pets: []Pet{{Name: "Ronny", Type: CAT, Age: 3}}}, | ||
} |
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,36 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/javiorfo/steams" | ||
"github.com/javiorfo/steams/examples/data" | ||
) | ||
|
||
func main() { | ||
fmt.Println("Get all the animals") | ||
animals := steams.OfSlice(data.PersonsWithPets).FlatMapToAny(func(p data.Person) steams.Steam[any] { | ||
results := make(steams.List[any], 0) | ||
for _, v := range p.Pets { | ||
results = append(results, v) | ||
} | ||
return results | ||
}) | ||
fmt.Println(animals) | ||
|
||
fmt.Println() | ||
fmt.Println("GroupBy type of animal") | ||
steams.GroupBy(animals, classifier).ForEach(steams.Println2) | ||
|
||
fmt.Println() | ||
fmt.Println("GroupByCounting type of animal") | ||
steams.GroupByCounting(animals, classifier).ForEach(steams.Println2) | ||
} | ||
|
||
func classifier(v any) string { | ||
animal, ok := v.(data.Pet) | ||
if ok { | ||
return animal.Type | ||
} | ||
return "None" | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,9 @@ | ||
package main | ||
|
||
import "github.com/javiorfo/steams" | ||
|
||
func main() { | ||
s1 := steams.Of(1, 2, 3) | ||
s2 := steams.Of("a", "b", "c") | ||
steams.Zip(s1, s2).ForEach(steams.Println) | ||
} |
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