分组:用于 scala 与 .net linq 中的理解
在阅读有关 scala 中的for compressives
的内容时,我有一种由 .net 的 linq
引起的似曾相识的效果。
它们都允许简洁的序列定义。
现在 - 问题是如何使用 进行理解来简洁地表示分组?
更具体地说。这在 C# 中:
from entry in new[] {
new { Name="Joe", ShoeSize="23" },
new { Name="Alice", ShoeSize="23" },
new { Name="Mary", ShoeSize="17" },
new { Name="Yeti", ShoeSize="170" },
}
group entry by entry.ShoeSize into grouped
select grouped;
产生:
Key=23, Items=(Joe, Alice)
Key=17, Items=(Mary)
Key=170, Items=(Yeti)
如何使用 scala for 理解
简洁地实现相同的效果?
Reading about for comprehensions
in scala I had a deja vu effect caused by .net's linq
.
They both allow for a concise sequence definition.
Now - the question is how to concisely represent grouping using a for comprehension
?
To be more specific. This in C#:
from entry in new[] {
new { Name="Joe", ShoeSize="23" },
new { Name="Alice", ShoeSize="23" },
new { Name="Mary", ShoeSize="17" },
new { Name="Yeti", ShoeSize="170" },
}
group entry by entry.ShoeSize into grouped
select grouped;
Produces:
Key=23, Items=(Joe, Alice)
Key=17, Items=(Mary)
Key=170, Items=(Yeti)
How to achieve the same concisely with scala for comprehensions
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不需要
理解
:There is no need of
for comprehensions
:请注意,如果您需要命名参数(姓名和鞋号),则可以使用案例类。丑陋的
mapValues
部分只需要从元组中提取名称部分,否则你会得到Map(23 -> List((Joe,23), (Alice,23)), 170 -> List((Yeti,170)), 17 -> List((Mary,17)))
,这通常“足够好”。Note that you can use case classes if you want named arguments (name and shoe size). The ugly
mapValues
part is only necessary to extract the name part from the tuple, elso you getMap(23 -> List((Joe,23), (Alice,23)), 170 -> List((Yeti,170)), 17 -> List((Mary,17)))
, which is often "good enough".您可以使用
List#groupBy()
按特定字段对其进行分组。You could use
List#groupBy()
to group it by a certain field.Scala 的理解能力没有分组能力。它只是
map
、flatMap
和filter
的组合(实际上是withFilter
,但是filter< /code> 更容易理解),或者
foreach
和filter
。它们背后的原理并不完全不同,但是 LINQ 是在 SQL 之后建模的,而 for commplion 是在单元转换之后建模的。例如,为了便于理解,您的示例可能会使用以下内容进行建模:
我希望 LINQ 会做几乎相同的事情,但它包含这些转换作为其基本语法的一部分,因为这是预期的。
Scala's for comprehension have no grouping ability. It is just a combination of
map
,flatMap
andfilter
(actually,withFilter
, butfilter
is easier to understand), orforeach
andfilter
.The principle behind them is not completely different, but LINQ was modeled after SQL, while for comprehension is modeled after monadic transformations. For instance, your example might be modeled with the following for comprehension:
I expect LINQ is doing pretty much the same thing, but it includes these transformations as part of its basic syntax because it is expected.