Guest User

Untitled

a guest
Dec 9th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. /* SCALA */
  2.  
  3. object SampleImplicits {
  4.  
  5. // mention object to be extended only once
  6. implicit def stringWrapper(s: String) = new {
  7.  
  8. def sort() = {
  9. val arr = s.toCharArray()
  10. java.util.Arrays.sort(arr)
  11. String.valueOf(arr)
  12. }
  13.  
  14. def checksum() = s.map(_.toInt).fold(0)(_ + _)
  15.  
  16. def toUpper() = s.toUpperCase()
  17.  
  18. }
  19.  
  20. }
  21.  
  22. object Sample {
  23.  
  24. def main(args: Array[String]) = {
  25.  
  26. // use extensions where they are needed
  27. import SampleImplicits._
  28.  
  29. val sorted = "Sort me!".sort()
  30. println(sorted)
  31.  
  32. val summed = "Sum me!".checksum()
  33. println(summed)
  34.  
  35. val string = "Upper me!".toUpper()
  36. println(string)
  37.  
  38. }
  39.  
  40. }
Add Comment
Please, Sign In to add comment