fork download
  1. object Main {
  2.  
  3. def main(args: Array[String]) {
  4. val secret = List('3', '_', 'r', '@', '!', '?', 'a')
  5.  
  6. // Just a Map from Char to Char
  7. val key = Map(
  8. '@' -> 'a',
  9. 'x' -> 'y',
  10. '?' -> 'm',
  11. '4' -> 't',
  12. '3' -> 'e',
  13. 'r' -> 'p')
  14.  
  15. val decoded: List[Char] = for { // let's yield (build) a new list
  16. x <- secret // let x iterate on Chars of the List secret
  17. y <- key get x // let y be the value looked up in the Map (if exists, otherwise skip)
  18. } yield y.toUpper // append y uppercased to the List we are building
  19.  
  20. val collected = decoded.mkString // join the Chars to form a String
  21.  
  22. for {
  23. // sliding(2) returns an iterator over sliding windows of length 2 the string
  24. // zipWithIndex transforms the iterator to yield (A, index) instead just A, for some type A
  25. // so slice is a String of length 2
  26. (slice, i) <- collected.sliding(2).zipWithIndex
  27. } println(" " * i + slice) // print the string slice "animated"
  28.  
  29. }
  30.  
  31. }
Success #stdin #stdout 0.25s 211776KB
stdin
Standard input is empty
stdout
EP
 PA
  AM