Skip to content

Commit

Permalink
Merge pull request #4 from gomoni/break
Browse files Browse the repository at this point in the history
Problem: break the chain example different from an actual code
  • Loading branch information
vyskocilm authored Feb 25, 2024
2 parents 4881c67 + 1e52e19 commit e79faaf
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 36 deletions.
76 changes: 60 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -371,25 +371,69 @@ Some crazy and not so crazy ideas to expolse

## break the chain

One of the coolest (keep in mind it was a late night one) ideas may be
One of the coolest (keep in mind it was a late night one) ideas may be breaking
the chain. The prototype exists in ideas_test.go, just not sure if it _is_
actually a good idea. It is definitely doable and possible in Go.

```go
n := []string{...}
chain := it.NewChain(n).Filter().Something().Seq()
// break the chain implementing a part as a for range loop
for s, err := range chain {
n, err := strconv.Atoi(s)
if err != nil {
break
}
// do the magic here and resume the chain
magicYield(n)
}

Map(magicSeq, foo).Filter(bar).Slice()
```
package it_test

import (
"fmt"

"github.com/gomoni/it"
)

type pusher struct {
stack chan string
}

func (y *pusher) push(s string) {
y.stack <- s
}

Implemented in break_da_chain example test in `ideas_test.go`.
func (y pusher) seq() func(func(string) bool) {
return func(yield func(string) bool) {
for {
select {
case s, open := <-y.stack:
if !open || !yield(s) {
return
}
}
}
}
}

func (y pusher) wait() {
<-y.stack
}

func Example_break_da_chain() {
n := []string{"aa", "aaa", "aaaaaaa", "a"}

// create a method chain
chain := it.NewChain(it.From(n)).
Filter(func(s string) bool { return true })

// break it - with some syntax sugar
p := pusher{stack: make(chan string)}
defer p.wait()
go func() {
defer close(p.stack)
for s := range chain.Seq() {
p.push(s)
}
}()

// continue here
chain2 := it.NewChain(p.seq()).
Filter(func(s string) bool { return len(s) > 2 })
slice := chain2.Slice()
fmt.Println(slice)
// Output: [aaa aaaaaaa]
}
```


## make iterations context aware?????
Expand Down
22 changes: 4 additions & 18 deletions README.md.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -166,25 +166,11 @@ Some crazy and not so crazy ideas to expolse

## break the chain

One of the coolest (keep in mind it was a late night one) ideas may be
One of the coolest (keep in mind it was a late night one) ideas may be breaking
the chain. The prototype exists in ideas_test.go, just not sure if it _is_
actually a good idea. It is definitely doable and possible in Go.

```go
n := []string{...}
chain := it.NewChain(n).Filter().Something().Seq()
// break the chain implementing a part as a for range loop
for s, err := range chain {
n, err := strconv.Atoi(s)
if err != nil {
break
}
// do the magic here and resume the chain
magicYield(n)
}

Map(magicSeq, foo).Filter(bar).Slice()
```

Implemented in break_da_chain example test in `ideas_test.go`.
{{ "Example_break_da_chain" | example }}


## make iterations context aware?????
Expand Down
8 changes: 6 additions & 2 deletions ideas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ func (y pusher) wait() {
func Example_break_da_chain() {
n := []string{"aa", "aaa", "aaaaaaa", "a"}

// create a method chain
chain := it.NewChain(it.From(n)).
Filter(func(s string) bool { return true })

// break it - with some syntax sugar
p := pusher{stack: make(chan string)}
defer p.wait()
go func() {
Expand All @@ -46,8 +48,10 @@ func Example_break_da_chain() {
}
}()

chain2 := it.NewChain(p.seq())
// continue here
chain2 := it.NewChain(p.seq()).
Filter(func(s string) bool { return len(s) > 2 })
slice := chain2.Slice()
fmt.Println(slice)
// Output: [aa aaa aaaaaaa a]
// Output: [aaa aaaaaaa]
}

0 comments on commit e79faaf

Please sign in to comment.