Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Chan ¶
Chan consumes ch with a pool and returns an output channel that yields do(v) results. Results are emitted as soon as they're ready (order is NOT guaranteed).
Example ¶
package main
import (
"fmt"
"github.com/rvflash/sama"
)
func main() {
ch := make(chan int, 3)
ch <- 1
ch <- 2
ch <- 3
close(ch)
var sum int
for v := range sama.Chan[int, int](ch, func(v int) int {
return v * 3
}) {
sum += v
}
fmt.Println(sum)
}
Output: 18
func Kun ¶
Kun consumes ch with a pool and calls do(v) for each item. It does not produce any output.
Example ¶
package main
import (
"fmt"
"sync/atomic"
"github.com/rvflash/sama"
)
func main() {
ch := make(chan int, 3)
ch <- 1
ch <- 2
ch <- 3
close(ch)
var sum atomic.Int32
sama.Kun[int](ch, func(v int) {
sum.Add(int32(v))
})
fmt.Println(sum.Load())
}
Output: 6
func San ¶
San is like Chan but guarantees that the output preserves the input order. The i-th value read from ch produces the i-th value on the returned channel.
Example ¶
package main
import (
"bytes"
"fmt"
"strconv"
"time"
"github.com/rvflash/sama"
)
func main() {
ch := make(chan int, 3)
ch <- 3
ch <- 2
ch <- 1
close(ch)
buf := new(bytes.Buffer)
for v := range sama.San[int, string](ch, func(v int) string {
time.Sleep(time.Duration(v) * 100 * time.Millisecond)
return strconv.Itoa(v)
}, 2) {
_, _ = buf.WriteString(v)
}
fmt.Println(buf.String())
}
Output: 321
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.