This is for Java programmers who want to get a practical introduction to Scala. I assume basic knowledge about Scala, namely that it has some similarities to Java and produces standard JVM bytecode. In this series of tutorials, I will show you some Scala basics by walking you through a few Project Euler problems.
The first part is rather long and therefore split into three videos. I expect the next parts to be shorter.
Sorry, dis-regard my note about val vs. var above. I can see now that you're revising your solution as you go. :-)
jsvazic 2 months ago
Just a quick few notes: In Scala everything, including primitives, are proper objects, hence the need for Int vs int. Int is a valid object. The Scala compiler will convert it to a primitive int as necessary, but that's the main reason. Also, you might want to emphasis the use of val instead of var. This may change your solution somewhat, but it's still better practice and one that should be encouraged earlier on. Not sure if you've done this in your other vids yet, but it's worth noting.
jsvazic 2 months ago
Ok, a bit overkill to have an object and it is imperative and mutable, I just user the REPL.
Make a function, use the function and sum the collection.
def multiples(from: Int, to: Int) = for { x <- from until to if(x%3== 0 || x%5==0) } yield x
val sum = multiples(1,1000).sum sum: Int = 233168
actofbalance 10 months ago