From 5f928128b3904bf4fb415013ec8082016424c382 Mon Sep 17 00:00:00 2001 From: Moritz Poldrack Date: Sun, 25 Apr 2021 22:07:12 +0200 Subject: char shift >> --- mocking-bob/more-paralellism.go | 84 ----------------------------------------- mocking-bob/more-parallelism.go | 84 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 84 deletions(-) delete mode 100644 mocking-bob/more-paralellism.go create mode 100644 mocking-bob/more-parallelism.go diff --git a/mocking-bob/more-paralellism.go b/mocking-bob/more-paralellism.go deleted file mode 100644 index 64b841b..0000000 --- a/mocking-bob/more-paralellism.go +++ /dev/null @@ -1,84 +0,0 @@ -package main - -import ( - // we want *real* random - "crypto/rand" - "fmt" - "os" - "unicode" -) - -func main() { - jobList := make(chan [2]chan rune, 4096) - - // spin up an appropriate amount of workers for this task - go worker(jobList) - go worker(jobList) - go worker(jobList) - go worker(jobList) - go worker(jobList) - go worker(jobList) - go worker(jobList) - go worker(jobList) - go worker(jobList) - go worker(jobList) - go worker(jobList) - go worker(jobList) - go worker(jobList) - go worker(jobList) - go worker(jobList) - go worker(jobList) - - outchans := make(chan chan rune, 4096) - done := make(chan struct{}, 1) - - go func(o chan chan rune) { - for r := range o { - fmt.Printf("%c", <-r) - } - done <- struct{}{} - }(outchans) - - for _, str := range os.Args[1:] { - for _, c := range str { - in := make(chan rune, 1) - out := make(chan rune, 1) - outchans <- out - jobList <- [2]chan rune{in, out} - in <- c - close(in) - } - out := make(chan rune, 1) - outchans <- out - out <- ' ' - close(out) - } - close(outchans) - - <-done -} - -func worker(jobList chan [2]chan rune) { - b := make([]byte, 1) - for job := range jobList { - c := <-job[0] - _, err := rand.Read(b) - if err != nil { - job[1] <- c - close(job[1]) - continue - } - if b[0]%2 == 0 { - // switch case - if unicode.ToUpper(c) == c { - job[1] <- unicode.ToLower(c) - } else { - job[1] <- unicode.ToUpper(c) - } - close(job[1]) - continue - } - job[1] <- c - close(job[1]) - } -} diff --git a/mocking-bob/more-parallelism.go b/mocking-bob/more-parallelism.go new file mode 100644 index 0000000..64b841b --- /dev/null +++ b/mocking-bob/more-parallelism.go @@ -0,0 +1,84 @@ +package main + +import ( + // we want *real* random + "crypto/rand" + "fmt" + "os" + "unicode" +) + +func main() { + jobList := make(chan [2]chan rune, 4096) + + // spin up an appropriate amount of workers for this task + go worker(jobList) + go worker(jobList) + go worker(jobList) + go worker(jobList) + go worker(jobList) + go worker(jobList) + go worker(jobList) + go worker(jobList) + go worker(jobList) + go worker(jobList) + go worker(jobList) + go worker(jobList) + go worker(jobList) + go worker(jobList) + go worker(jobList) + go worker(jobList) + + outchans := make(chan chan rune, 4096) + done := make(chan struct{}, 1) + + go func(o chan chan rune) { + for r := range o { + fmt.Printf("%c", <-r) + } + done <- struct{}{} + }(outchans) + + for _, str := range os.Args[1:] { + for _, c := range str { + in := make(chan rune, 1) + out := make(chan rune, 1) + outchans <- out + jobList <- [2]chan rune{in, out} + in <- c + close(in) + } + out := make(chan rune, 1) + outchans <- out + out <- ' ' + close(out) + } + close(outchans) + + <-done +} + +func worker(jobList chan [2]chan rune) { + b := make([]byte, 1) + for job := range jobList { + c := <-job[0] + _, err := rand.Read(b) + if err != nil { + job[1] <- c + close(job[1]) + continue + } + if b[0]%2 == 0 { + // switch case + if unicode.ToUpper(c) == c { + job[1] <- unicode.ToLower(c) + } else { + job[1] <- unicode.ToUpper(c) + } + close(job[1]) + continue + } + job[1] <- c + close(job[1]) + } +} -- cgit v1.2.3-70-g09d2