Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Notes on Collections
- By default, Scala uses immutable collections. However, there are mutable
- versions available (in the scala.collection.mutable package). The most common
- collections are List, Set, and Map.
- */
- // Declaring a set
- val urls = Set("google.com", "yahoo.com", "nytimes.com")
- //
- // Matching values in a set with filter(). filter(Boolean) allows you to iterate through
- // a collection to verify whether a value should be filtered in or out (based on a boolean)
- // statement
- //
- // mkString() is another method from Iterable that creates a flat representation
- // of the collection and calls each objects toString method.
- //
- // Output: matching URLs: google.com, yahoo.com
- val ooUrls = urls filter (_ contains "oo")
- println("matching URLs: " + ooUrls.mkString(", "))
- //
- // Intersection
- // Output: The intersection is: google.com, yahoo.com
- val urls2 = Set("yahoo.com", "twitter.com", "google.com", "amazon.com")
- val intersection = urls ** urls2
- println("The intersection is: " + intersection.mkString(", "))
- //
- // Merge
- // Output: The merge is: amazon.com, google.com, yahoo.com, nytimes.com, twitter.com
- val merge = urls ++ urls2
- println("The merge is: " + merge.mkString(", "))
- //
- // map(f(A) => B) will apply the function f to each element of a collection and
- // return B
- // Output: E-mails: help@google.com, help@yahoo.com, help@nytimes.com
- val helpEmails = urls map ("help@" + _)
- println("E-mails: " + helpEmails.mkString(", "))
- // Another example of map, this time using a def'd method and passing it to
- // map.
- // Output: E-mails: help@google.com, help@yahoo.com, help@nytimes.com
- def fn(a : String) : String = {
- "help@" + a
- }
- val helpEmails2 = urls map fn
- println("E-mails: " + helpEmails2.mkString(", "))
- //
- // find(p : (A) => Boolean) : Option[A]
- val found = urls find (_ contains "google")
- val oo = urls find (_ contains "oo")
- println(found)
- println(oo)
- /*
- // Folding
- // This section is pretty interesting. Using the fnFoldLeft and fnFoldRight methods,
- // I found that the order of parameters matters for folding. if foldRight is
- // executed with fnFoldLeft, the number passed into 'total' parameter will be
- // the value pulled out of the list.
- //
- // In some cases, the foldRight will appear not to work if the parameters of
- // the method are out of order
- //
- // It is also important to keep in mind that folding may be affected by the
- // order of operations. Lists are ordered, buts sets are not and so iterating
- // over a set with positive and negative numbers could produce unexpected results.
- */
- val nums = List(1,2,3,4,2)
- println(nums)
- println("Left Concat is: " + nums.foldLeft("")
- { (str, item) => str + item.toString() } )
- println("Right Concat is: " + nums.foldRight("")
- { (item, str) => str + item.toString() } )
- val nums2 = List(-1,2,-3,4,-5,6)
- def fnFoldLeft(total : Int, num : Int) : Int = {
- println(total.toString() + " - " + num.toString())
- total - num
- }
- def fnFoldRight(num : Int, total : Int) : Int = {
- println(total.toString() + " - " + num.toString())
- total - num
- }
- println("foldLeft:")
- println(nums2.foldLeft(0)(fnFoldLeft))
- println("foldRight:")
- println(nums2.foldRight(0)(fnFoldRight))
Add Comment
Please, Sign In to add comment