Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Compile and Execute Scala Online

object HelloWorld {
   def main(args: Array[String]) {
      var days = Array("Poniedziałek", "Wtorek", "Sroda", "Czwartek", "Piątek", "Sobota", "Niedziela");
      // petla for
      for( x <-  days){
         println( x );
      }
      println("-------------------")
      
      //petla for z nazwami na p
      for( x <-  days if x.startsWith("P")){
          
          println(x);
      }
      println("-------------------")
      
      //petla foreach
      days.foreach{
          println
      }
      
      println("-------------------")
      
      //petla while
      println("Petla while:")
      var x = 0;
      while( x < days.length){
          println(days(x))
          x += 1;
      }
      println("-------------------")
      
    //funkcja rekurencyjna
      println("funkcja rekurencyjna:")
      def printArr( i: Int, arr:Array[String]): Unit = {
          if(i<arr.length){
            println(arr(i));
            printArr(i+1,days)
          }
      }
      printArr(0,days);
      println("-------------------")
      
    //funkcja rekurencyjna od konca listy
      println("funkcja rekurencyjna od konca listy:")
      def printArrRev( i: Int, arr:Array[String]): Unit = {
          var j = arr.length - 1;
          if(i <= j){
            println(arr(j-i));
            printArrRev(i+1,days)
          }
      }
      printArrRev(0,days);
      println("-------------------")
      
    //metody foldl i foldr
      println(days.foldLeft(""){(x,y) => x + y + " "});
      println(days.foldRight(" "){(x,y) => x + " " + y});
      println(days.foldLeft(""){(x,y) => x + y.startsWith("P")})

      println("-------------------")
    //Metody foldl wypisując tylko dni z nazwami zaczynającymi się na „P”
    
//second task
      var products = Map("T-shirt" -> 10, "Skirt" -> 30, "Dress" -> 100, "Boots" -> 150, "Jacket" -> 200);
   
      var products2 = products.mapValues(_*0.9)
      println(products2)
      println("-------------------")
      
//third task

      def PrintTuple( tup: Product): Unit = {
            tup.productIterator.foreach(println)
          
      }
      var t = ("Hi", 10, true)
      PrintTuple(t)

//task four

//     var products = Map("T-shirt" -> 10, "Skirt" -> 30, "Dress" -> 100, "Boots" -> 150, "Jacket" -> 200);
    def ShowOptions(x: Option[Float]):Any = x match {
      case Some(s) => s
      case None => "No such element"
    }
        //var keys = products.keys.toList
    val keys = products.keys.toList
    val keys2 = "Auto" :: keys

    keys2.foreach( x => print(ShowOptions(products.get(x)) + " ") )
    
// task  five

    def showDay(x: String) : String = {
    
        var weekendDay: String = "weekendDay"
        var workDay: String = "workDay"
        x match {
            
            case "Poniedziałek" => workDay
            case "Wtorek" => workDay
            case "Sroda" => workDay
            case "Czwartek" => workDay
            case "Piątek" => workDay
            case "Sobota" => weekendDay
            case "Niedziela" => weekendDay
            case _ => "RandomDay"
        }
    }
        
    days.foreach( x => println(showDay(x)))
    
//six task
    class Konto {
        private var stan  = 0
        
        def Stan = stan
        
        def this(kwota: Int)  {
            this()
            this.stan = kwota
        }
        def Wplata(kwota :Int): Unit = {
            this.stan += kwota
        }
        def Wyplata(kwota :Int): Unit = {
            if(this.stan - kwota >= 0)
              this.stan -= kwota
            else
              println("Need more money !")
        }
    }
    
    
       
//task eight 
    
    var lista = List(1,2,4,5,0,6,0,4,0,6,0,5,2,3)

    def ZeroEraser (lista:List[Int]):List[Int] = {   
      var newList:ListBuffer[Int] = new ListBuffer()
    
      for(value <- lista)
        if(value != 0)
          newList += value
        return newList.toList
      }
    println(ZeroEraser(lista))
        
//task nine

    def OneAdder (lst:List[Int]):List[Int] = {
      var newList = lst.map(_+1)
      return newList
    }
    println(OneAdder(lst))
 }
}

mapp

object HelloWorld {
   def main(args: Array[String]) {
      List(1, 2).map { a =>
          println(a)
          a
        }.map(println)
   }
}

Compile and Execute Scala Online

trait one
{
    def method:Unit
     var a=10
     private final b=20
}

trait two

trait three extends one

abstract class Demo
{
    
}
class HelloWorld extends Demo with one with two with three 
{
    def method=println("This is method with value "+a)
}
object HelloWorld extends two
{
    def main(args:Array[String])
    {
        println("This is trait example1")
        val obj=new HelloWorld
        obj.method
    }
}

premia

object HelloWorld {
  
  def stvorec(n : Int): Int ={
      if(n == 1) 1
      else stvorec(n-1)+n+n-1
    }
    
  def main(args: Array[String]) {
      println(stvorec(5))
   }
}

Compile and Execute Scala Online

object HelloWorld {
    private def test1{
        println("from test1")
    }
    def main(args: Array[String]) {
      println("Hello, world!")
      val obj=new HelloWorld
      obj.x.foreach(println)
   }   
}

class HelloWorld{
    private var x=List(10,20,30)
    HelloWorld.test1

}

Compile and Execute Scala Online

object HelloWorld {
   def main(args: Array[String]) {
      //println("Hello, world!")
    
    
    //If Statement  
      val number = 13;
      if(number%2 == 0)
        println("even number");
      else
      println("odd");
  
  
  //match statemet    
    val num = 11;
    num match {
    case 10 => println("nummber is 10");
    case _ => println("number other than 10");
    }
    
    
    
    

   }
   
   
}

unpacking case class

object HelloWorld {
   
   sealed case class MyRec(a: Int, b: Double, c: String)
   
   def main(args: Array[String]) {
      val rec = MyRec(1, 4.2, "hello")
      MyRec.unapply(rec) match { // unpacks MyRec
          case Some(fields) =>   // into a tuple of individual fields
              fields.productIterator.foreach(x => x match {
                  case y: Int => println(s"integer $y")
                  case y: Double => println(s"double $y")
                  case y: String => println(s"string $y")
              })
          case None => println("impossible!")
      }
   }
}

Compile and Execute Scala Online

object HelloWorld {
   def main(args: Array[String]) {
      val point = new point(10,20)
      printpoint
      def printpoint{
          println("Point X location :" + point.x)
          println("Point Y location :" + point.y)
      }
   }
}
class point(val ax: Int, val ay: Int) {
    var x: Int = ax
    var y: Int = ay
}

testNormDenorm

object HelloWorld {
   def main(args: Array[String]) {
      println("Test1!")
      
      val precision = 31
      val normLat = new BitNormalizedDimension(-90d, 90d, precision)
      val normLon = new BitNormalizedDimension(-180d, 180d, precision)
      
      val lat = 45.354875
      val lon = 1.395856
      
      val uLat = normLat.normalize(lat)
      val uLon = normLon.normalize(lon)
      
      println(uLat)
      println(uLon)
      println(normLat.denormalize(uLat))
      println(normLon.denormalize(uLon))
   }
   
   
   class BitNormalizedDimension(val min: Double, val max: Double, precision: Int) {

    require(precision > 0 && precision < 32, "Precision (bits) must be in [1,31]")

    // (1L << precision) is equivalent to math.pow(2, precision).toLong
    private val bins = 1L << precision
    private val normalizer = bins / (max - min)
    private val denormalizer = (max - min) / bins

     val maxIndex: Int = (bins - 1).toInt // note: call .toInt after subtracting 1 to avoid sign issues

     def normalize(x: Double): Int =
      if (x >= max) { maxIndex } else { math.floor((x - min) * normalizer).toInt }

     def denormalize(x: Int): Double =
      if (x >= maxIndex) { min + (maxIndex + 0.5d) * denormalizer } else { min + (x + 0.5d) * denormalizer }
  }
   
}

Reduce operation example

object HelloWorld {
   def main(args: Array[String]) {
      
    val data = Array(1,2, 3 ,4)
      
   val result =  data.reduce((accumulation ,current) => accumulation +current) 
      
      println(result)
      
   }
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.