TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Show HN: An Experimental 'LINQ to Objects' in Go

1 点作者 suzuki超过 6 年前

1 comment

suzuki超过 6 年前
This LINQ implementation is unique in that it is not related to any particular data structure.<p>The LINQ code in C# found at <a href="https:&#x2F;&#x2F;docs.microsoft.com&#x2F;dotnet&#x2F;api&#x2F;system.linq.enumerable.range" rel="nofollow">https:&#x2F;&#x2F;docs.microsoft.com&#x2F;dotnet&#x2F;api&#x2F;system.linq.enumerable...</a> can be written in Go with this implementation as follows:<p><pre><code> &#x2F;&#x2F; Generate a sequence of integers from 1 to 10 and then select their squares. squares := Range(1, 10).Select(func(x Any) Any { return x.(int) * x.(int) }) squares(func(num Any) { fmt.Println(num) }) </code></pre> Here the function Range is defined as follows:<p><pre><code> &#x2F;&#x2F; Range creates an Enumerator which counts from start &#x2F;&#x2F; up to start + count - 1. func Range(start, count int) Enumerator { end := start + count return func(yield func(Any)) { for i := start; i &lt; end; i++ { yield(i) } } } </code></pre> Note that Enumerator is just a function type defined as func(func(Any)). The space complexity is O(1) and you can yield values infinitely if you want.