The result of each step is passed on to the next step (as input to one of the binary operator's two arguments). Val a = IndexedSeq.In general, all 6 fold functions apply a binary operator to each element of a collection. ![]() Val couples = women.zip(men) // Vector((Wilma,Fred), (Betty,Barney)) Val men = IndexedSeq("Fred", "Barney") // Vector(Fred, Barney) Val women = IndexedSeq("Wilma", "Betty") // Vector(Wilma, Betty) Val a = IndexedSeq(IndexedSeq(1,2), IndexedSeq(3,4))Ī.flatten // IndexedSeq = Vector(1, 2, 3, 4)įruits.map(_.toUpperCase) // Vector(APPLE, PEAR)įruits.flatMap(_.toUpperCase) // Vector(A, P, P, L, E, P, E, A, R) IndexedSeq(1,2,1,2).distinct // IndexedSeq = Vector(1, 2) ThreeToSeven.diff(oneToFive) // IndexedSeq = Vector(6, 7) OneToFive.diff(threeToSeven) // IndexedSeq = Vector(1, 2) Val threeToSeven = (3 to 7).toIndexedSeq // IndexedSeq = Range 3 to 7 Val oneToFive = (1 to 5).toIndexedSeq // IndexedSeq = Range 1 to 5 Val x = IndexedSeq(Some(1), None, Some(3), None) // IndexedSeq] = Vector(Some(1), None, Some(3), None) Return a new sequence by applying the function f to each element in the IndexedSeqĪ new sequence with the element at index i replaced with the new value vĪ new sequence that contains elements from the current sequence and the sequence s When working with sequences, it works like map followed by flatten Transforms a sequence of sequences into a single sequence : empty.lastĪt .last(Vector.scala:197)īecause IndexedSeq 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 IndexedSeq as you assign the result to a new variable: MethodĪ new collection by applying the partial function pf to all elements of the sequence, returning elements for which the function is definedĪ new sequence with no duplicate elements The first subset of elements that matches the predicate p The last element can throw an exception if the IndexedSeq is emptyĪ sequence of elements from index f (from) to index u (until) Return the intersection of the sequence and another sequence s Return the first element can throw an exception if the IndexedSeq 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 a IndexedSeq)Ī IndexedSeq is an immutable sequence, so you don’t remove elements from it. Therefore, with +: and ++:, these methods comes from the IndexedSeq 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. Val v5 = IndexedSeq(1,2) ++: v4 // Vector(1, 2, 3, 4, 5, 6, 7, 8, 9) About the : character in the method names show more info on classes/objects in repl.
0 Comments
Leave a Reply. |