douqi2804 2013-05-18 21:53
浏览 50
已采纳

map()和reduce()是否适合Go中的并发处理?

Coming from a python background, and just starting with Go, I found myself looking for the equivalent of the map() and reduce() functions in Go. I didn't find them, so fell back on for loops. For example, this is what I used instead of a map(), where mapFunction is defined elsewhere:

data := make([]byte, 1024)
count, err := input.Read(data) // error handling removed from this snippet
for i:=0; i<count; i++ {
    data[i] = mapFunction(data[i])
}

and this is what I used instead of a reduce(), where there are 2 state variables that I'm using to keep track of quoting of fields in a CSV as the code moves through each item in the slice:

data := make([]byte, 1024)
count, err := input.Read(data) // error handling removed from this snippet
for i:=0; i<count; i++ {
    data[i], stateVariable1, stateVariable2 =
        reduceFunction(data[i], stateVariable1, stateVariable2)
}

Here are my questions:

  1. Are there builtin capabilties for this that I missed?
  2. Is it appropriate to use mutable slices for each of these?
  3. Would it be a good idea to use goroutines for the map()? Would that allow decoupling of the IO operation to read the file and the process to run the mapping function on each item, and therefore allow parallelization?
  4. Is it correct to say that goroutines would not be appropriate for the reduce() function because the 2 state variables are defined by all of the preceding data, and it must proceed sequentially. In other words, this sequential process cannot benefit from concurrent architecture?

Thanks!

ps - the full code is here: https://github.com/dbro/csvquote/blob/go/csvquote.go

  • 写回答

2条回答 默认 最新

  • doushan2224 2013-05-18 23:19
    关注

    In short:

    1. No, there is no builtin map or reduce.
    2. Yes. What else?
    3. No. Do not even think about such stuff without prior measuring or some proven real need.
    4. Yes.

    A bit longer.

    1. Go is not functional, no map/reduce builtins or in the standard library
    2. There are array and slices in Go. Both are mutable. Slices are the natural choice most of the time.
    3. Premature optimization... , but of course: Reading an processing could go into one loop and wrapping input in a bufio.Reader could be a good idea.
    4. Goroutines are nice, they allow a different type of program construction, but that does not mean that they are to be used for everything. There is no need to complicate a perfectly clear for loop by introducing goroutines.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮