Guest User

Untitled

a guest
Mar 16th, 2018
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. /*
  2. Notes on Collections
  3.  
  4. By default, Scala uses immutable collections. However, there are mutable
  5. versions available (in the scala.collection.mutable package). The most common
  6. collections are List, Set, and Map.
  7. */
  8.  
  9. // Declaring a set
  10. val urls = Set("google.com", "yahoo.com", "nytimes.com")
  11.  
  12. //
  13. // Matching values in a set with filter(). filter(Boolean) allows you to iterate through
  14. // a collection to verify whether a value should be filtered in or out (based on a boolean)
  15. // statement
  16. //
  17. // mkString() is another method from Iterable that creates a flat representation
  18. // of the collection and calls each objects toString method.
  19. //
  20. // Output: matching URLs: google.com, yahoo.com
  21. val ooUrls = urls filter (_ contains "oo")
  22. println("matching URLs: " + ooUrls.mkString(", "))
  23.  
  24. //
  25. // Intersection
  26. // Output: The intersection is: google.com, yahoo.com
  27. val urls2 = Set("yahoo.com", "twitter.com", "google.com", "amazon.com")
  28. val intersection = urls ** urls2
  29. println("The intersection is: " + intersection.mkString(", "))
  30.  
  31. //
  32. // Merge
  33. // Output: The merge is: amazon.com, google.com, yahoo.com, nytimes.com, twitter.com
  34. val merge = urls ++ urls2
  35. println("The merge is: " + merge.mkString(", "))
  36.  
  37. //
  38. // map(f(A) => B) will apply the function f to each element of a collection and
  39. // return B
  40. // Output: E-mails: help@google.com, help@yahoo.com, help@nytimes.com
  41. val helpEmails = urls map ("help@" + _)
  42. println("E-mails: " + helpEmails.mkString(", "))
  43.  
  44. // Another example of map, this time using a def'd method and passing it to
  45. // map.
  46. // Output: E-mails: help@google.com, help@yahoo.com, help@nytimes.com
  47. def fn(a : String) : String = {
  48. "help@" + a
  49. }
  50. val helpEmails2 = urls map fn
  51. println("E-mails: " + helpEmails2.mkString(", "))
  52.  
  53. //
  54. // find(p : (A) => Boolean) : Option[A]
  55. val found = urls find (_ contains "google")
  56. val oo = urls find (_ contains "oo")
  57. println(found)
  58. println(oo)
  59.  
  60. /*
  61. // Folding
  62. // This section is pretty interesting. Using the fnFoldLeft and fnFoldRight methods,
  63. // I found that the order of parameters matters for folding. if foldRight is
  64. // executed with fnFoldLeft, the number passed into 'total' parameter will be
  65. // the value pulled out of the list.
  66. //
  67. // In some cases, the foldRight will appear not to work if the parameters of
  68. // the method are out of order
  69. //
  70. // It is also important to keep in mind that folding may be affected by the
  71. // order of operations. Lists are ordered, buts sets are not and so iterating
  72. // over a set with positive and negative numbers could produce unexpected results.
  73. */
  74.  
  75. val nums = List(1,2,3,4,2)
  76. println(nums)
  77. println("Left Concat is: " + nums.foldLeft("")
  78. { (str, item) => str + item.toString() } )
  79. println("Right Concat is: " + nums.foldRight("")
  80. { (item, str) => str + item.toString() } )
  81.  
  82. val nums2 = List(-1,2,-3,4,-5,6)
  83.  
  84. def fnFoldLeft(total : Int, num : Int) : Int = {
  85. println(total.toString() + " - " + num.toString())
  86. total - num
  87. }
  88. def fnFoldRight(num : Int, total : Int) : Int = {
  89. println(total.toString() + " - " + num.toString())
  90. total - num
  91. }
  92. println("foldLeft:")
  93. println(nums2.foldLeft(0)(fnFoldLeft))
  94. println("foldRight:")
  95. println(nums2.foldRight(0)(fnFoldRight))
Add Comment
Please, Sign In to add comment