<p>I read <a href="http://martinfowler.com/articles/refactoring-pipelines.html" rel="nofollow">this article</a> by Martin Fowler on refactoring loops into collection pipelines. </p>
<p>Can someone provide an example like that in Go? Would it even be advisable to do that?</p>
<hr/>**评论:**<br/><br/>kron4eg: <pre><p><a href="https://clipperhouse.github.io/gen/slice/" rel="nofollow">https://clipperhouse.github.io/gen/slice/</a></p>
<p>This will enable you to do type-safe collection manipulations</p></pre>klaaax: <pre><p>Go doesn't have generics. You'd have to either use interface{} everywhere or write map,select,filter methods for each type given the following type []T .</p>
<p>The idiomatic way in go is to write for loops and copy arrays into other arrays as you transform a collection. Go may have first class functions , but its type system is clearly not suited for any kind of functional programming.</p>
<p>Worse, you cant pass []string to a method signed []interface{} so you'd have to use reflection on a interface{} argument to iterate over interface{} and of course the performances would be horrible.</p>
<p>In short, forget it.</p></pre>sheenobu: <pre><p>Here is a simple example I wrote up:</p>
<p><a href="http://play.golang.org/p/aaxXuUZ5YW" rel="nofollow">http://play.golang.org/p/aaxXuUZ5YW</a></p>
<pre><code>func main() {
items := []string{"one", "two", "three", "four", "five", "six"}
items = ToStringStream(items).Each(strings.ToUpper).Filter(isLen(3)).Reverse().ToList()
fmt.Printf("%s\n", items)
}
</code></pre></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传