Reputation: 225
I'm trying to concatenate 3 sequences as results of individual for loops with yield. I can't get it to work without temporary variables. Does anybody know a better option? The notWorking version gives me a compiler error at the fourth line of the method "illegal start of a simple expression" just after the first ++.
def working() : Seq[Seq[Elem]] = {
val result = for(index <- 0 until COMPLETE_INPUT_CHANNELS) yield {
getModesOfCompleteInputChannel(index)
}
val result2 = for(index <- 0 until INCOMPLETE_INPUT_CHANNELS) yield {
getModesOfIncompleteInputChannel(index)
}
val result3 = for(index <- 0 until OUTPUT_CHANNELS) yield {
getModesOfOutputChannel(index)
}
return result ++ result2 ++ result3
}
def notWorking() : Seq[Seq[Elem]] = {
for(index <- 0 until COMPLETE_INPUT_CHANNELS) yield {
getModesOfCompleteInputChannel(index)
} ++ for(index <- 0 until INCOMPLETE_INPUT_CHANNELS) yield {
getModesOfIncompleteInputChannel(index)
} ++ for(index <- 0 until OUTPUT_CHANNELS) yield {
getModesOfOutputChannel(index)
}
Upvotes: 3
Views: 2095
Reputation: 10431
How about this solution ?
val tasks = Seq(
(COMPLETE_INPUT_CHANNELS, getModesOfOutputChannel),
(INCOMPLETE_INPUT_CHANNELS, getModesOfIncompleteInputChannel),
(OUTPUT_CHANNELS, getModesOfOutputChannel))
tasks flatMap {
case (limit, processing) => 0 until limit map processing
}
Upvotes: 15
Reputation: 15414
Why not this?
(0 until COMPLETE_INPUT_CHANNELS).map(getModesOfCompleteInputChannel) ++
(0 until COMPLETE_INPUT_CHANNELS).map(getModesOfIncompleteInputChannel) ++
(0 until OUTPUT_CHANNELS).map(getModesOfOutputChannel)
Though I do like M.A.D's solution. Very elegant.
Upvotes: 6
Reputation: 8378
I believe, ++
here is treated as a part of the yield
expression. To make it work, just wrap your for
comprehensions in parentheses.
Upvotes: 2