序列理解中的多重产量?
我正在尝试学习 Scala 并尝试编写一个序列理解,从序列中提取一元组、二元组和三元组。例如,[1,2,3,4] 应该转换为(不是 Scala 语法)
[1; _,1; _,_,1; 2; 1,2; _,1,2; 3; 2,3; 1,2,3; 4; 3,4; 2,3,4]
在 Scala 2.8 中,我尝试了以下操作:
def trigrams(tokens : Seq[T]) = {
var t1 : Option[T] = None
var t2 : Option[T] = None
for (t3 <- tokens) {
yield t3
yield (t2,t3)
yield (t1,t2,Some(t3))
t1 = t2
t2 = t3
}
}
但这不会编译为 显然,在 for
理解中只允许一个 yield
(也没有块语句)。是否有任何其他优雅的方式来获得相同的行为,只需一次传递数据?
I'm trying to learn Scala and tried to write a sequence comprehension that extracts unigrams, bigrams and trigrams from a sequence. E.g., [1,2,3,4] should be transformed to (not Scala syntax)
[1; _,1; _,_,1; 2; 1,2; _,1,2; 3; 2,3; 1,2,3; 4; 3,4; 2,3,4]
In Scala 2.8, I tried the following:
def trigrams(tokens : Seq[T]) = {
var t1 : Option[T] = None
var t2 : Option[T] = None
for (t3 <- tokens) {
yield t3
yield (t2,t3)
yield (t1,t2,Some(t3))
t1 = t2
t2 = t3
}
}
But this doesn't compile as, apparently, only one yield
is allowed in a for
-comprehension (no block statements either). Is there any other elegant way to get the same behavior, with only one pass over the data?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
for 循环中不能有多个 yield,因为 for 循环是 map(或 flatMap)操作的语法糖:
转换为
完全没有 Yield
转换为
因此
for
循环的整个主体变成单个闭包,并且yield
关键字的存在决定了在集合上调用的函数是map
还是foreach
(或者 <代码>flatMap)。由于这种翻译,以下行为是被禁止的:yield
旁边使用命令式语句来确定将产生什么。(更不用说您提出的版本将返回一个
List[Any]
因为元组和 1-gram 都是不同的类型。您可能想要获得一个List[ List[Int]]
代替)请尝试以下操作(将 n 元语法按其出现的顺序排列):
或者
如果您更喜欢 n 元语法按长度顺序,请尝试:
You can't have multiple yields in a for loop because for loops are syntactic sugar for the map (or flatMap) operations:
translates into
Without a yield at all
translates into
So the entire body of the
for
loop is turned into a single closure, and the presence of theyield
keyword determines whether the function called on the collection ismap
orforeach
(orflatMap
). Because of this translation, the following are forbidden:yield
to determine what will be yielded.(Not to mention that your proposed verison will return a
List[Any]
because the tuples and the 1-gram are all of different types. You probably want to get aList[List[Int]]
instead)Try the following instead (which put the n-grams in the order they appear):
or
If you prefer the n-grams to be in length order, try:
我想我应该多考虑一下。
然后从三元组中提取一元组和二元组。但是谁能向我解释为什么不允许“多重收益”,以及是否有其他方法可以达到其效果?
I guess I should have given this more thought.
Then extract the unigrams and bigrams from the trigrams. But can anyone explain to me why 'multi-yields' are not permitted, and if there's any other way to achieve their effect?
您可以尝试没有分配的功能版本:
You could try a functional version without assignments: