Each and Continue can be used to calculate composite numbers and prime numbers.
def compositeNumbersBelow(maxNumber: Int) = collection.immutable.HashSet {
val factor = !Each(2 until math.ceil(math.sqrt(maxNumber)).toInt)
!Each(2 * factor until maxNumber by factor)
}
compositeNumbersBelow(13) should be(Set(4, 6, 8, 9, 10, 12))
def primeNumbersBelow(maxNumber: Int) = Seq {
val compositeNumbers = compositeNumbersBelow(maxNumber)
val i = !Each(2 until maxNumber)
if (compositeNumbers(i)) !Continue
i
}
primeNumbersBelow(13) should be(Array(2, 3, 5, 7, 11))
Note
This Continue keyword is usually used with Each, to skip an element in the loop.
A keyword to skip the current iteration in a collection comprehension block.
Author:
杨博 (Yang Bo)
Each and Continue can be used to calculate composite numbers and prime numbers.
This Continue keyword is usually used with Each, to skip an element in the loop.
Each for creating collection comprehensions.