Collection pipelines in Go?

xuanbao · · 666 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<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&#39;t have generics. You&#39;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&#39;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{&#34;one&#34;, &#34;two&#34;, &#34;three&#34;, &#34;four&#34;, &#34;five&#34;, &#34;six&#34;} items = ToStringStream(items).Each(strings.ToUpper).Filter(isLen(3)).Reverse().ToList() fmt.Printf(&#34;%s\n&#34;, items) } </code></pre></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

666 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传