concurrent

package module
v0.0.0-...-a4bfb75 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 24, 2022 License: MIT Imports: 3 Imported by: 0

README

Simple MapReduce on Go

Test GoDoc

Package concurrent provides high-level APIs for concurrent programming. Package implemented Qt Concurrent API

Examples

Map
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) // [2 4 6 8 10 12]
}
Mapped
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) // [1 2 3 4 5 6]
}
MappedReduced
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
}

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

Examples

Constants

This section is empty.

Variables

View Source
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

func DefMapped[S any, R any](data []S, function func(S) R) ([]R, error)

DefMapped calls function once for each item in sequence and returns a sequence with each mapped item as a result

func Map

func Map[T any](goroutines int, data []T, function func(*T)) error

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

func Mapped[S any, R any](goroutines int, data []S, function func(S) R) ([]R, error)

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
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL