Question

This code is from the most popular go matrix package https://github.com/skelterjohn/go.matrix/blob/go1/util.go

I googled this function and seems like it is for computing the fractal dimension. But in this package, this function is never used so I am having a hard time understanding this.

func countBoxes(start, cap int) chan box {
        ints := make(chan box)
        go func() {
                for i := start; i < cap; i++ {
                        ints <- i
                }
                close(ints)
        }()
        return ints
}

Why do we need goroutine when we have only one anonymous function here? And does anybody know what this function does in terms of matrix work?

Thanks in advance.

Was it helpful?

Solution

It returns a channel with cap - start queued integer events. (i.e. You can 'read' start,start+1..,cap from the channel and then it closes ).

If you poke around in the code, it uses a similar kind of construct to create an iterator for the the indices of non-zero entries of sparse matrices. Look in sparse.go.

It's not used anywhere in the code that I can find, it may have been just to test the idea.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top