Skip to content

Commit

Permalink
examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Javier Orfo committed Nov 5, 2024
1 parent e614115 commit 2416387
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 5 deletions.
55 changes: 53 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,65 @@

## Caveats
- This plugin requires Go 1.23
- Contains several Java style streams (called steams) and Optionals too.

## Intallation
```bash
go get -u https://github.com/javiorfo/steams
```

package main

import (
"fmt"

"github.com/javiorfo/steams"
"github.com/javiorfo/steams/examples/data"
)

## Example
#### More examples [here](https://github.com/javiorfo/steams/tree/master/examples)
```go
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)
}
```

## Interfaces
```go
// Steam[T] is an interface for a collection of elements of type T,
Expand Down Expand Up @@ -77,8 +130,6 @@ func GroupByCounting[K comparable, V any](s Steam[V], classifier func(V) K) Stea
func Zip[T, R any](s1 Steam[T], s2 Steam[R]) Steam[struct { first T; second R }]
```

## Examples

---

### Donate
Expand Down
1 change: 0 additions & 1 deletion examples/advanced.go

This file was deleted.

47 changes: 47 additions & 0 deletions examples/advanced/main.go
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.
23 changes: 23 additions & 0 deletions examples/data/data.go
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}}},
}
36 changes: 36 additions & 0 deletions examples/groupby/main.go
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"
}
1 change: 0 additions & 1 deletion examples/grouping_by.go

This file was deleted.

1 change: 0 additions & 1 deletion examples/zip.go

This file was deleted.

9 changes: 9 additions & 0 deletions examples/zip/main.go
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)
}
7 changes: 7 additions & 0 deletions functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ func Println[T any](v T) {
fmt.Println(v)
}

// Println prints the values of the provided arguments k and v to the standard output.
// It can accept a comparable K value and any type to second value
// due to the use of a type parameter T.
func Println2[K comparable, T any](k K, v T) {
fmt.Println(k, v)
}

// Min returns true if the first argument a is greater than the second argument b.
// It is intended to be used with types that implement the Ordered interface,
// which allows for comparison operations.
Expand Down

0 comments on commit 2416387

Please sign in to comment.