‏הצגת רשומות עם תוויות Scala. הצג את כל הרשומות
‏הצגת רשומות עם תוויות Scala. הצג את כל הרשומות

יום שישי, 4 בדצמבר 2015

using @ in Scala match case

There is an option In Scala match case to use Boolean extractors in match case .
The  unapply method  instead of returning an object returns Boolean expression.
The following example demonstrates it :

class  DataEntity (val mValue:Int) extends IDataEntity
object DataEntityValueChecker {
  def unapply(pIDataEntity: IDataEntity): Boolean = 
    {
      pIDataEntity.mValue > 164
    }
}
object MeshamemLiMavet10 extends App {
   println ("Starting MeshamemLiMavet 10 ")
    val theIDataEntity: IDataEntity = new DataEntity(165)
    theIDataEntity match {
        case pIDataEntity @ DataEntityValueChecker() => {println (pIDataEntity.mValue.toString() +  " > 164")}
        case _ => {println ( " > 164")}
   }

And the result:
Starting MeshamemLiMavet 10
165 > 164

יום חמישי, 24 באפריל 2014

Scala foreach

Just playing with Scala foreach method :

package myScalaProject
 object thePlayer{
    var myList = List ("Zvika","Dalit","Gal","Shahaf","Lior")
	myList foreach ( nextItem => println (nextItem))
	var myMap = Map ("1"->"Golda meir" , "2"->"Fohad be eliazer" , "3"->"Stas aamos")
		myMap foreach ( nextItem => println (nextItem._1 + ":" + nextItem._2))
}
object myScalaMain {
	def main(args: Array[String]): Unit = {
		thePlayer
	}
}

And the Result:
Zvika
Dalit
Gal
Shahaf
Lior
1:Golda meir
2:Fohad ben eliazer
3:Stas aamos

Please note that in order to set the scala main class in NetBeans ide use the following Dialog :
image001

יום רביעי, 16 באפריל 2014

Play with Scala actors

The following code is a little demonstration for Scala actors:

package myScalaProject
import scala.actors.Actor
import scala.actors.Actor._
import java.lang.Float
class ActorTester extends Actor {
def act() {
       println("Actor Started.")
       println ("The actor thread id :" +  Thread.currentThread().getId())
              loop {
                     receive {
                     case i: Int => println("Int: " + i.toString)
                     case s: String => println("String: " + s)      
                     case f: Float => println("Float: " + f) 
                     case 5=> println("Five")  
                     case _ => println("Default.")
                     }
              }
                 println("Actor ended.") //doesn’t called
       }
}     
object PlayWithActors {
 println ("The main thread thread id :" +  Thread.currentThread().getId())
 val myActorTester = new ActorTester
 myActorTester.start
 
 //Sending data to the actor
 myActorTester ! "Hello actor"
 myActorTester ! 5
 myActorTester !  164.1
 myActorTester ! 15
}

And the Result :


The main thread thread id :1
Actor Started.
The actor thread id :12
String: Hello actor
Int: 5
Default.
Int: 15