#struggling to understand how to use channels

20 messages · Page 1 of 1 (latest)

low basin
#

hello everyone noob here, i just dont understand concurrency man i'm really trying 😦
trying to follow along with the examples in the book

func primeFinder(done <-chan interface{}, valuesStream <-chan int) <-chan int {
    primeNumberStream := make(chan int)

    go func() {
        defer close(primeNumberStream)

        for v := range valuesStream {
            select {
            case <-done:
                return
            case primeNumberStream <- v:
            }
        }
    }()

    return primeNumberStream
}

i want this function to be able to return a receive channel of only prime numbers from the valueStream parameter how do i add that logic in this for-select loop?

twin pivot
#

why you want to return a channel

spiral stump
#

it seems like missing some parts

low basin
#

well from the book this function wasn't actually defined. it was just used

#

so i'm trying to recreate it somehow

#

lemme put in the thing from the book

#

thanks for trying to help btw

#
package main

import (
    "fmt"
    "math/rand"
    "time"
)

func Take[T interface{}](done <-chan interface{}, valueStream <-chan T, num int) <-chan interface{} {
    takeStream := make(chan interface{})

    go func() {
        defer close(takeStream)
        for i := 0; i < num; i++ {
            select {
            case <-done:
                return
            case takeStream <- <-valueStream:
            }
        }
    }()
    return takeStream
}

func RepeatFn(done <-chan interface{}, fn func() interface{}) <-chan interface{} {
    valueStream := make(chan interface{})

    go func() {
        defer close(valueStream)
        for {
            select {
            case <-done:
                return
            case valueStream <- fn():
            }
        }
    }()
    return valueStream
}

func toInt(done <-chan interface{}, valuesStream <-chan interface{}) <-chan int {
    intStream := make(chan int)
    go func() {
        defer close(intStream)

        for v := range valuesStream {
            select {
            case <-done:
                return
            case intStream <- v.(int):
            }
        }
    }()
    return intStream
}

func randomInteger() interface{} {
    return rand.Intn(50_000_000)
}

func main() {
    done := make(chan interface{})

    defer close(done)

    start := time.Now()
    randIntStream := toInt(done, RepeatFn(done, randomInteger))
    fmt.Println("Primes: ")
    for prime := range Take(done, primeFinder(done, randIntStream), 10) {
        fmt.Printf("\t%d\n", prime)
    }

    fmt.Printf("Search took: %v", time.Since(start))
}
#

so yeah its just missing that primeFinder function

#

i'm trying to create it following the pattern this current topic is discussing

#

with the done channels and the for-select pattern

spiral stump
#

check the v before the select, if its prime number, send it trough the channel.
if not, continue.

low basin
#

aaah

#

like this?

func primeFinder(done <-chan interface{}, valuesStream <-chan int) <-chan int {
    primeNumberStream := make(chan int)

    go func() {
        defer close(primeNumberStream)

        for v := range valuesStream {
            for i := 2; i < (v/2)+1; i++ {
                if v%i == 0 {
                    primeNumberStream <- v
                    break
                }
            }
            select {
            case <-done:
                return
            case primeNumberStream <- v:
            }
        }
    }()

    return primeNumberStream
}
low basin
#

i got it i got it

#
func primeFinder(done <-chan interface{}, valuesStream <-chan int) <-chan int {
    primeNumberStream := make(chan int)

    go func() {
        defer close(primeNumberStream)

        for v := range valuesStream {

            prime := true

            for i := 2; i < (v/2)+1; i++ {
                if v%i == 0 {
                    prime = false
                    break
                }
            }
            if prime {
                select {
                case <-done:
                    return
                case primeNumberStream <- v:
                }
            }
        }
    }()

    return primeNumberStream
}
#

wtf is this mess

#

it works but why

#

lmao