![]() ![]() OneToFive. Val oneToFive = (1 to 5).toSeq # val oneToFive = (1 to 5).toSeq ![]() Val x = Seq(Some(1), None, Some(3), None) Return a new sequence by applying the function f to each element in the SeqĪ new seq with the element at index i replaced with the new value vĪ new seq that contains elements from the current seq and the sequence s When working with sequences, it works like map followed by flatten Transforms a list of lists into a single list 28 elided How to “update” Seq elementsīecause Seq is immutable, you can’t update elements in place, but depending on your definition of “update,” there are a variety of methods that let you update a Seq as you assign the result to a new variable: MethodĪ new collection by applying the partial function pf to all elements of the seq, returning elements for which the function is definedĪ new sequence with no duplicate elements : head of empty listĪt $.head(List.scala:428)Īt $.head(List.scala:425)Īt (LinearSeqOptimized.scala:146)Īt $(LinearSeqOptimized.scala:145)Īt .last(List.scala:86) The first subset of elements that matches the predicate p The last element can throw an exception if the Seq is emptyĪ sequence of elements from index f (from) to index u (until) Return the intersection of the seq and another sequence s Return the first element can throw an exception if the Seq is empty Return the first element that matches the predicate p ![]() Return all elements that do not match the predicate p ![]() Return all elements that match the predicate p Return all elements except the last n elementsĭrop the first sequence of elements that matches the predicate p Return all elements after the first n elements Return a new sequence with no duplicate elements These methods let you “remove” elements during this process: Method Instead, you describe how to remove elements as you assign the results to a new collection. Filtering methods (how to “remove” elements from an Seq)Ī Seq is an immutable sequence, so you don’t remove elements from a Seq. Therefore, with +: and ++:, these methods comes from the Seq that’s on the right of the method name. The correct technical way to think about this is that a Scala method name that ends with the : character is right-associative, meaning that the method comes from the variable on the right side of the expression. I use that as a way to remember these methods. Note that during these operations the : character is always next to the old (original) sequence. Seq is immutable, so in all of the examples that follow you need to assign the result of the operation shown to a new variable, like this: A LinearSeq provides fast access only to the first element via head, but also has a fast tail operation.”Īlso, please see the “Seq is not Vector” section at the end of this post, because as that name implies, Seq behaves differently than Vector in almost all of these examples. An IndexedSeq provides fast random-access of elements and a fast length operation. “ Seq has two principal subtraits, IndexedSeq and LinearSeq, which give different guarantees for performance. Important note about Seq, IndexedSeq, and LinearSeqĪs an important note, I use Seq in the following examples to keep things simple, but in your code you should be more precise and use IndexedSeq or LinearSeq where appropriate. Summary: This page contains many examples of how to use the methods on the Scala Seq class, including map, filter, foldLeft, reduceLeft, and many more. show more info on classes/objects in repl.Previous value associated with specified key. This code will print "stackOverflow" i.e. Previous value associated with specified key, or null if there was no mapping for key. Key - key whose mapping is to be removed from the map. (A null return can also indicate that the map previously associated null with the specified key if the implementation supports null values.) The map will not contain a mapping for the specified key once the call returns. Returns the value to which the map previously associated the key, or null if the map contained no mapping for this key. (The map can contain at most one such mapping.) More formally, if this map contains a mapping from key k to value v such that (key=null ? k=null : key.equals(k)), that mapping is removed. Removes the mapping for this key from this map if it is present (optional operation). ![]()
0 Comments
Leave a Reply. |