英文:
Use channel for matrix and box counting
问题
这段代码来自最流行的Go矩阵包https://github.com/skelterjohn/go.matrix/blob/go1/util.go
我在谷歌上搜索了这个函数,似乎是用于计算分形维度。但在这个包中,这个函数从未被使用过,所以我很难理解它的作用。
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
}
为什么我们在这里只有一个匿名函数时还需要goroutine?
有人知道这个函数在矩阵工作方面的作用吗?
提前感谢。
英文:
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.
答案1
得分: 2
它返回一个具有 cap - start 个排队的整数事件的通道。(即,您可以从通道中“读取”start、start+1、...、cap,然后关闭通道)。
如果您在代码中查找,它使用了类似的结构来为稀疏矩阵非零条目的索引创建迭代器。请查看 sparse.go。
在我找到的代码中没有使用它,可能只是为了测试这个想法。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论