Advertisement
ZivkicaI

MapFlatFilterFor

Nov 29th, 2019
679
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.18 KB | None | 0 0
  1. package part3Udemy
  2.  
  3. object MapFlatMapFilterFor extends App {
  4.  
  5.   val list = List(1,2,3,4)
  6.  
  7.   println(list.head)
  8.   println(list.tail)
  9.  
  10.  
  11.   println(list.map(x=>x+1))  //vrakja lista map-ot
  12.   println(list.map(_ + " is a member"))
  13.  
  14.   println(list.filter(_ % 2 ==0))  //lista so x, kade x e paren broj
  15.  
  16.   val toPair= (x:Int) => List(x, x+2)
  17.   println(list.flatMap(toPair))  //flatMap pravi parovi od sekoj element so elementot+2 List(1, 3, 2, 4, 3, 5, 4, 6)
  18.  
  19.  
  20.   val numbers=List(1,2,3)
  21.   val chars=List('a', 'b', 'c', 'd')
  22.   val colors=List("black", "white")
  23.  
  24.   //printing all the combinations between two lists
  25.  
  26.   val combination = numbers.flatMap(n => chars.map(c=> "" + c + n))
  27.   println(combination)
  28.  
  29.   //printing all the combinations between 3 list (ITERATING)
  30.   val combination1 = numbers.flatMap(n => chars.flatMap(c => colors.map(color => "" + c + n + color)))
  31.   println(combination1)
  32.  
  33.   //FOREACH
  34.   list.foreach(println)  // printa vo novi redovi sekoj element
  35.  
  36.   //printing all the combinations between 3 list with for
  37.  
  38.   val forche=for {
  39.     n <- numbers  //if n%2==0 e ok
  40.     c <- chars
  41.     color <- colors
  42.   }yield ""+ c + n + color
  43.  
  44.    println(forche)
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement