Documentation
¶
Overview ¶
Package concurrent provides high-level APIs for concurrent programming. Package implemented Qt Concurrent API https://doc.qt.io/qt-6/qtconcurrent-index.html
Index ¶
- Variables
- func DefMap[T any](data []T, function func(*T))
- func DefMapped[S any, R any](data []S, function func(S) R) ([]R, error)
- func Map[T any](goroutines int, data []T, function func(*T)) error
- func Mapped[S any, R any](goroutines int, data []S, function func(S) R) ([]R, error)
- func MappedReduced[S any, R any](goroutines int, data []S, mapFunction func(S) R, reduceFunction func(*R, R), ...) (R, error)
- type ReduceOption
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var DefGoroutines = runtime.NumCPU()
DefGoroutines default goroutines count(runtime.NumCPU())
Functions ¶
func DefMap ¶
func DefMap[T any](data []T, function func(*T))
DefMap calls function once for each item in sequence. The function takes a reference to the item, so that any modifications done to the item will appear in sequence.
func DefMapped ¶
DefMapped calls function once for each item in sequence and returns a sequence with each mapped item as a result
func Map ¶
Map calls function once for each item in sequence. The function takes a reference to the item, so that any modifications done to the item will appear in sequence.
Example ¶
package main import ( "fmt" concurrent "github.com/RPG-18/concurrent" ) func main() { numbers := []int{1, 2, 3, 4, 5, 6} _ = concurrent.Map(4, numbers, func(value *int) { *value *= 2 }) fmt.Println(numbers) }
func Mapped ¶
Mapped calls function once for each item in sequence and returns a sequence with each mapped item as a result
Example ¶
package main import ( "fmt" "strconv" concurrent "github.com/RPG-18/concurrent" ) func main() { numbers := []int{1, 2, 3, 4, 5, 6} strings, _ := concurrent.Mapped(4, numbers, func(value int) string { return strconv.Itoa(value) }) fmt.Println(strings) }
func MappedReduced ¶
func MappedReduced[S any, R any](goroutines int, data []S, mapFunction func(S) R, reduceFunction func(*R, R), reduceOptions ReduceOption) (R, error)
MappedReduced calls mapFunction once for each item in sequence. The return value of each mapFunction is passed to reduceFunction.
Note that while mapFunction is called concurrently, only one thread at a time will call reduceFunction. The order in which reduceFunction is called is determined by reduceOptions.
Example ¶
package main import ( "fmt" "strconv" concurrent "github.com/RPG-18/concurrent" ) func main() { numbers := []int{1, 2, 3, 4, 5, 6} ordered, _ := concurrent.MappedReduced(3, numbers, func(value int) string { return strconv.Itoa(value) }, func(sum *string, value string) { *sum = *sum + value }, concurrent.OrderedReduce) unordered, _ := concurrent.MappedReduced(3, numbers, func(value int) string { return strconv.Itoa(value) }, func(sum *string, value string) { *sum = *sum + value }, concurrent.UnorderedReduce) fmt.Println(ordered, unordered) // 123456 142536 }
Types ¶
type ReduceOption ¶
type ReduceOption int
ReduceOption reduce order
const ( UnorderedReduce ReduceOption = iota + 1 // UnorderedReduce unordered OrderedReduce // OrderedReduce ordered )