可以MAP'和'与仿制药一起实现
我决定现在已经将仿制药引入了GO中,例如MAP/RELAD
之类的东西是可能的。因此,我对此很天真地刺伤,我遇到了错误: ./ prog.go:18:36:无法将事物(INT类型的变量)用作i type i的参数to mapper
,
否则没有解释问题是否是基本的,或者我只是在做错事句法。可以在GO中实现通用地图/减少吗?
package main
import "fmt"
func main() {
things := []int{1, 2, 3, 4}
results := Map(things, func(t int) int {
return t + 1
})
fmt.Printf("%v", results)
}
func Map[I interface{}, O interface{}](things []I, mapper func(thing I) O) []O {
results := make([]O, 0, len(things))
for thing := range things {
results = append(results, mapper(thing))
}
return results
}
I decided that now that generics have been introduced into Go that something like map/reduce
should be possible. So, I took a naive stab at it and I get the error:./prog.go:18:36: cannot use thing (variable of type int) as type I in argument to mapper
Which doesn't explain if the problem is fundamental or I am simply doing something wrong syntactically. Can generic map/reduce be implemented in Go?
package main
import "fmt"
func main() {
things := []int{1, 2, 3, 4}
results := Map(things, func(t int) int {
return t + 1
})
fmt.Printf("%v", results)
}
func Map[I interface{}, O interface{}](things []I, mapper func(thing I) O) []O {
results := make([]O, 0, len(things))
for thing := range things {
results = append(results, mapper(thing))
}
return results
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这可以很容易地完成。您的代码中有一个错误,尽管在这里:
您正在迭代索引值(int),而不是类型
i
的值。您还指定2个约束(类型i
和o
)都设置为接口{}
。您只需使用而不是任何
而不是(接口{}
),所以只需写入:
demo
这与我在CodeReview Exchange上评论的某些代码非常密切相关, - 用于传播阵列和映射函数,in-go-1-18/276128#276128“>在这里。浏览了代码并用大量建议编写片段后,我决定创建一个软件包并将其扔到github上。您可以找到repo 在这里。
在其中,有一些示例可能会派上用场,或者可以帮助您通过Golang的其他一些Quirks WRT仿制药工作。我专门考虑了这一点,您可以在其中使用像这样的回调过滤通用地图类型:
This can be done quite easily. You have an error in your code, though right here:
You are iterating over the index values (int), not the values of type
I
. You're also specifying 2 constraints (typesI
andO
) both set to beinterface{}
. You can just useany
instead (it's shorthand forinterface{}
)So simply write:
Demo
This is quite closely related to some code I reviewed on codereview exchange here. After going through the code, and writing snippets with a ton of suggestions, I decided to just create a package and throw it up on github instead. You can find the repo here.
In it, there's some examples that may come in handy, or help you work through some other quirks WRT generics in golang. I wsa specifically thinking about this bit, where you can filter a generic map type using callbacks like so:
您对
range
的使用不正确。从范围提取的单个变量
将是索引(类型int
),而不是值(typei
,它仅偶然地INT
在这种情况下)。尝试
You have incorrect use of
range
. A single variable extracted fromrange
will be the index (typeint
), not the value (typeI
, which is only coincidentallyint
in this case).Try