There are numerous ways to solve this first problem, but two that
quickly spring to mind are:
1. filter:
val x = (1 until 1000) filter { x => x % 3 == 0 || x % 5 == 0 }
2. flatMap (using a for comprehension):
val y = for { i <- (1 until 1000) if (i %3 == 0 || i % 5 == 0) } yield i
Regards,
Gary
Euler problems are really, really entertaining - albeit that the later
ones can be extremely frustrating until that "aha" moment.
For numeric types, such as Int in this case, the sum function is also
available on the collections.
Eg (using the defined y value): y.sum == 233168
Regards,
Gary
As well as Project Euler, this is a great resource for learning Scala:
http://aperiodic.net/phil/scala/s-99/
Adapted from the similar "99 Prolog problems" and "99 Lisp problems"
tutorials. Unlike project Euler, it tends to have less of an
algorithmic/mathematical "aha" focus and more aimed at teaching the
functional side of programming. Plus you can peek at the solutions
once you've had your try ;-)
P.