My first scala steps

2Disclaimer(this is not a tutorial or how to, i am a newb to scala and i recod my thoughts)

I was reading about scala for a time now and yesterday i started coding. I started with a simple application, making some simple function and try to get used with the syntax. So i started coding without looking in the book(Programing in scala) or on the web(to really learn it,i used to copy paste or read then type code in the scala interpretor but those methods don’t let you make mistakes that will help you remember )



package scalaprim

object Main {

  /**
   * @param args the command line arguments
   */
  def main(args: Array[String]): Unit = {
    println("Hello, world!")
    println(divide(10,2))
    println(divide(10,3))
    println(isPrime(11))
    println(isPrime(4))
    //findPrimes(100)
    val primes=findPrimes3(100)
    //primes.foreach(println)
  //consecutivePrimes(primes);
  consecutivePrimes2(primes);
  }
def divide(number:Int,divizor:Int):Boolean=(number%divizor==0);
def isPrime(number:Int):Boolean={

  val range = 2.until(Math.sqrt(number).intValue +1)
  for (i <- range)
    {
    if(divide(number, i))
     return false;
  }
  return  true;
}
def findPrimes(number:Int):Unit={
    for(i <-2.until(number))
      {
        if(isPrime(i))
          println(i)
      }
  }
  def findPrimes2(number:Int):IndexedSeq[Int]={
    val range=2 until number+1
    val primes=range.filter(isPrime)
    return primes
  }
  def findPrimes3(number:Int):IndexedSeq[Int]={
    (2 until number+1)filter isPrime   
  }

  def consecutivePrimes(listOfPrimes:IndexedSeq[Int]):Unit={
    var i:Int=0;
    while(i{
        if(prev+2==curent)
          println(prev,",",curent)
         curent
        }})
  }
}


My conclusion on this project is that using scala i can write less code, especially it could be possible to skip using lots of for, i worked on a c++ program and i think i have more then 100 fors, and nested fors that could be easily replaced in scala with calls to filter or exist or something else. And reducing for is not only about code clarity but often you can make bugs in for loops, like mistaking the indices in nested loops or getting outside of the array bounds.

I will investigate Scala in my free time and hope i could put it to use in a large project.

P.S. blogilo is failing uploading my posts, hope eventualy this post will be uploaded(finding a fix and trying another apps)