import akka.actor.typed.ActorSystem
import akka.actor.typed.scaladsl.Behaviors
import org.slf4j.LoggerFactory
import akka.stream.scaladsl.MergeHub
import akka.stream.scaladsl.Sink
import akka.stream.scaladsl.Keep
import scala.concurrent.duration._
import scala.language.postfixOps
import akka.stream.scaladsl.Source
import akka.stream.scaladsl.BroadcastHub
import akka.stream.scaladsl.Flow
import java.nio.file.Paths
import scala.concurrent.Future
import akka.stream.scaladsl.FileIO
import akka.stream.IOResult
import akka.stream.alpakka.csv.scaladsl.CsvParsing
import akka.stream.KillSwitches


object Fdws12 extends App {
    implicit val system = ActorSystem(Behaviors.empty, "system")
    import system.executionContext
    val log = LoggerFactory.getLogger("monLogger")

    val source = Source.tick(0 seconds, 3 seconds, "hello")
    val switch = KillSwitches.single[String]
    val sink = Sink.ignore 

    
    /* kill switch */
    /*
    val sw = source.log("tick").viaMat(switch)(Keep.right).to(sink).run()
    Thread.sleep(10000)
    sw.shutdown()
    */
    def createDynamicFlow[A](name: String) = {
        val mergehub = MergeHub.source[A]
        val broadcasthub = BroadcastHub.sink[A]
        val switchableFlow = KillSwitches.single[A] 
        val ((talk, kill), listen) = mergehub.log(name)
          .viaMat(switchableFlow)(Keep.both)
          .toMat(broadcasthub)(Keep.both)
          .run()
      (talk, listen, kill)  
    }

    /* substreams et groupBy */
    Source(1 to 10).groupBy(Int.MaxValue, _ % 3)
    .mergeSubstreams
    .fold("")((s,e)=> s + s"-$e")
    .to(Sink.foreach(println)) //.run()


    /* FileIO avec alpakka */
    val file = Paths.get("example.csv") /* entier id, entier année, chaîne formation */
    FileIO.fromPath(file) /* 1,2020,M1 */
    .via(CsvParsing.lineScanner()) /* List("1","2020","M1")*/
    .map(_.map(_.utf8String)).log("alpakka")
   // .runWith(Sink.ignore)

    /* définir le format des données */
    case class Inscription(id: Int, annee: Int, formation: String)
    FileIO.fromPath(file) 
    .via(CsvParsing.lineScanner()) /* List("1","2020","M1")*/
    .map(_.map(_.utf8String).toVector)
    .map((xs) => Inscription(xs(0).toInt, xs(1).toInt, xs(2)))
    .log("data")
    .runWith(Sink.ignore)

    /* La sortie qu'on attend sur le fichier fourni est 
    */

    Thread.sleep(30000)
    log.info("Sayonara")
    system.terminate()    
} 

Scala Online Compiler

Write, Run & Share Scala code online using OneCompiler's Scala online compiler for free. It's one of the robust, feature-rich online compilers for Scala language, running on the latest version 2.13.8. Getting started with the OneCompiler's Scala compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as Scala and start coding.

Read input from STDIN in Scala

OneCompiler's Scala online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample Scala program which takes name as input and prints hello message with your name.

object Hello {
	def main(args: Array[String]): Unit = {
	  val name = scala.io.StdIn.readLine()        // Read input from STDIN
    println("Hello " + name ) 
	}
}

About Scala

Scala is both object-oriented and functional programming language by Martin Odersky in the year 2003.

Syntax help

Variables

Variable is a name given to the storage area in order to identify them in our programs.

var or val Variable-name [: Data-Type] = [Initial Value];

Loops and conditional statements

1. If family:

If, If-else, Nested-Ifs are used when you want to perform a certain set of operations based on conditional expressions.

If

if(conditional-expression){    
//code    
} 

If-else

if(conditional-expression) {  
//code if condition is true  
} else {  
//code if condition is false  
} 

Nested-If-else

if(condition-expression1) {  
//code if above condition is true  
} else if (condition-expression2) {  
//code if above condition is true  
}  
else if(condition-expression3) {  
//code if above condition is true  
}  
...  
else {  
//code if all the above conditions are false  
}  

2. For:

For loop is used to iterate a set of statements based on a criteria.

for(index <- range){  
  // code  
} 

3. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while(condition) {  
 // code 
}  

4. Do-While:

Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.

do {
  // code 
} while (condition) 

Functions

Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity.

def functionname(parameters : parameters-type) : returntype = {   //code
}

Note:

You can either use = or not in the function definition. If = is not present, function will not return any value.