Scala:

stiahni

Akka:

stiahni

Príklady pre Scalu:


1.For cyklus:

              for (i <- 1.to(5)) {
 		println(i);
 	      }

	      for (i <- 1 until 6) {
 	        println(i);
 	      }

 	      for (i <- 1 to 5; j <- 1 to 3 ) {
 		println(i + " " + j);
   	      }
              

2.Funkcie:

            def add(x:Int, y:Int):Int = {
		return x+y;
	    }

	    def square(x:Int):Int = {
 		return x*x;
	     }

	    def compose(f:Int=>Int,g:Int=>Int):Int=>Int=x=>f(g(x))

              

3.Pattern Matching:

            def matchTest(x: Int): String = x match {
      		case 1 => "one"
      		case 2 => "two"
     		case _ => "many"
   	    }


	    def matchTest(x: Any) = 
	    x match {
     		case i:Int => println("It's Integer "+ i)
      		case s:String => println("It's String "+ s)
      		case d:Double => println("It's Double "+ d)
      		case _ => "Something else"
	    }


	    matchTest(List(1,2))
           

Úlohy:

  • 1. Vytvorte 10-prvkové pole a spočítajte sumu všetkých jeho prvkov
  • 2. Vymyslite príklad na zloženú funkciu(iný ako v ukážke)

Príklady pre Akku:


1.Pracovanie s JSON:

           import java.util.UUID

	   import akka.actor.typed.ActorSystem
	   import akka.actor.typed.scaladsl.Behaviors
	   import akka.http.scaladsl.Http
	   import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
	   import akka.http.scaladsl.server.Directives._
	   import akka.stream.scaladsl.Flow
	   import akka.http.scaladsl.server.Route
	   import de.heikoseeberger.akkahttpcirce.FailFastCirceSupport
	   import de.heikoseeberger.akkahttpjackson.JacksonSupport

	   case class Person(name: String, age: Int)
	   case class UserAdded(id: String, timestamp: Long)


	   import spray.json._

	   trait PersonJsonProtocol extends DefaultJsonProtocol{

	     implicit val personFormat = jsonFormat2(Person)
	     implicit val userAddedFormat = jsonFormat2(UserAdded)
	   }

	   object AkkaHttpJson extends PersonJsonProtocol  with SprayJsonSupport{

	     implicit val system = ActorSystem(Behaviors.empty, "AkkaHttpJson")

	     val route: Route = (path("api" / "user") & post) {
	       entity(as[Person]) {person: Person =>
	         complete(UserAdded(UUID.randomUUID().toString, System.currentTimeMillis()))
	       }
	     }

	     def main(args: Array[String]): Unit = {
	       Http().newServerAt("localhost", 8081).bind(route)
	     }
	   }

	   object AkkaHttpCirce extends FailFastCirceSupport{
	     import io.circe.generic.auto._

	     implicit val system = ActorSystem(Behaviors.empty, "AkkaHttpJson")

	     val route: Route = (path("api" / "user") & post) {
	       entity(as[Person]) {person: Person =>
	         complete(UserAdded(UUID.randomUUID().toString, System.currentTimeMillis()))
	       }
	     }

 	    def main(args: Array[String]): Unit = {
 	      Http().newServerAt("localhost", 8082).bind(route)
 	    }
	   }              
	   

3.Dependencies:

           val akkaVersion = "2.6.5"
	   val akkaHttpVersion = "10.2.0"
	   val akkaHttpJsonSerializersVersion = "1.34.0"
	   val circeVersion = "0.12.3"

	   libraryDependencies ++= Seq(
	     "com.typesafe.akka" %% "akka-actor-typed" % akkaVersion,
	     "com.typesafe.akka" %% "akka-http-spray-json" % akkaHttpVersion,
	     "com.typesafe.akka" %% "akka-stream" % "2.6.5",
	     "de.heikoseeberger" %% "akka-http-circe" % akkaHttpJsonSerializersVersion,
	     "de.heikoseeberger" %% "akka-http-jackson" % akkaHttpJsonSerializersVersion,

	     "io.circe" %% "circe-core" % circeVersion,
	     "io.circe" %% "circe-generic" % circeVersion,
	     "io.circe" %% "circe-parser" % circeVersion
	   )
           

Úlohy:

  • 1. Vyskusajte pracu s Jacksonom (velmi podobne ako predchadzajuce)

Zdroje:

  • https://blog.rockthejvm.com/
  • https://www.tutorialspoint.com/scala/scala_overview.htm
  • https://doc.akka.io/docs/akka/current/typed/guide/tutorial.html