master > master: codego rekursives Aufrufen mit Channels ausgelagert

This commit is contained in:
RD 2021-05-12 18:46:37 +02:00
parent 8778d31676
commit c10f194ce4

View File

@ -0,0 +1,51 @@
package rekursion
import (
"logik/aussagenlogik/syntaxbaum"
)
/* ---------------------------------------------------------------- *
* EXPORTS
* ---------------------------------------------------------------- */
func RekursiveCallInt(fn func(ch chan int, tree syntaxbaum.SyntaxBaum), children []syntaxbaum.SyntaxBaum) []int {
subChannel := make(chan int)
values := make([]int, len(children))
// start parallel computations on subformulae
for _, subtree := range children {
go fn(subChannel, subtree)
}
// successively read values
for i := 0; i < len(children); i++ {
values[i] = <-subChannel
}
return values
}
func RekursiveCallString(fn func(ch chan string, tree syntaxbaum.SyntaxBaum), children []syntaxbaum.SyntaxBaum) []string {
subChannel := make(chan string)
values := make([]string, len(children))
// start parallel computations
for _, subtree := range children {
go fn(subChannel, subtree)
}
// successively read values
for i := 0; i < len(children); i++ {
values[i] = <-subChannel
}
return values
}
func RekursiveCallStringList(fn func(ch chan []string, tree syntaxbaum.SyntaxBaum), children []syntaxbaum.SyntaxBaum) [][]string {
subChannel := make(chan []string)
values := make([][]string, len(children))
// start parallel computations
for _, subtree := range children {
go fn(subChannel, subtree)
}
// successively read values
for i := 0; i < len(children); i++ {
values[i] = <-subChannel
}
return values
}