Advertisement
Guest User

Untitled

a guest
Mar 16th, 2015
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.97 KB | None | 0 0
  1. def encode(data : List[_]) : List[Any] = {
  2.       var counts : Map[String, Int] = Map();
  3.  
  4.       for (i <- data)
  5.       {
  6.         if (counts.contains(i.toString))
  7.         {
  8.           counts += (i.toString -> (counts(i.toString)+1));
  9.         }
  10.         else
  11.         {
  12.           counts += (i.toString -> 1);
  13.         }
  14.       }
  15. //
  16. //      var list = List[Any]()
  17. //
  18. //      for ((k,v) <- counts)
  19. //      {
  20. //        var tup = Tuple2[String, Int]
  21. //        tup = (k => v);
  22. //        list += tup;
  23. //      }
  24.  
  25.       //      for (c <- 0 until counts.count(_))
  26. //      {
  27. //        var tup = Tuple2[String, Int];
  28. //        tup = (counts.ke,counts(c))
  29. //        retData += tup;
  30. //
  31. //      }
  32. //      var list = List[Any]
  33. //      counts.map { case (k,v) => (k.toString, v) }(collection.breakOut): List[(List[String], List[Int])]
  34. //      return (counts.toSeq: _*).toList
  35.  
  36.  
  37.       val output = counts.view.map{ case(k,v) => (k.toString, v) }.toList
  38.  
  39.       return output;
  40.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement