I’m trying to write a functional approach in scala to get a list of all numbers between 1 & 1000 that are divisible by 3 or 5
Here is what I have so far :
def getListOfElements(): List[Int] = {
val list = List()
for (i <- 0 until 1000) {
//list.
}
list match {
case Nil => 0
}
list
}
The for loop seems like an imperative approach and I’m not sure what to match on in the case class. Some guidance please ?
Here’s how I would do it with a
forexpression.This gives:
Here’s another approach filtering on a
Rangeof numbers.If you really want a
Liston the return value usetoList. e.g.res0.toList.