林克地图! 或收藏!

发布于 2024-07-16 13:55:13 字数 345 浏览 10 评论 0原文

相当于 map 的 Linq 是什么? 或者 Ruby 中的collect! 方法?

   a = [ "a", "b", "c", "d" ]
   a.collect! {|x| x + "!" }
   a             #=>  [ "a!", "b!", "c!", "d!" ]

可以通过使用 foreach 迭代集合来做到这一点,但我想知道是否有更优雅的 Linq 解决方案。

What is the Linq equivalent to the map! or collect! method in Ruby?

   a = [ "a", "b", "c", "d" ]
   a.collect! {|x| x + "!" }
   a             #=>  [ "a!", "b!", "c!", "d!" ]

I could do this by iterating over the collection with a foreach, but I was wondering if there was a more elegant Linq solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

茶色山野 2024-07-23 13:55:13

地图 = 选择

var x = new string[] { "a", "b", "c", "d"}.Select(s => s+"!");

Map = Select

var x = new string[] { "a", "b", "c", "d"}.Select(s => s+"!");
爱格式化 2024-07-23 13:55:13

高阶函数 map 最好用 Enumerable.Select 是 System.Linq 中的扩展方法。

如果您好奇,其他高阶函数会像这样爆发:

reduce -> Enumerable.Aggregate
过滤器 -> 可枚举。哪里

The higher-order function map is best represented in Enumerable.Select which is an extension method in System.Linq.

In case you are curious the other higher-order functions break out like this:

reduce -> Enumerable.Aggregate
filter -> Enumerable.Where

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文