silikontape.blogg.se

Scala filter method map
Scala filter method map





Note that these transformations create copies of the collection, and Transforms take an existing collection and create a new collection modified in

scala filter method map

distinct // Removes all duplicates res12 : Array = Array ( 1, 2, 3, 4, 5, 6, 7, 8 ) 4.4.scala drop ( 2 ) // Discard first two elements res10 : Array = Array ( 3, 4, 5 ) Array ( 1, 2, 3, 4, 5 ). take ( 2 ) // Keep first two elements res9 : Array = Array ( 1, 2 ) Array ( 1, 2, 3, 4, 5 ). filter (i => i % 2 = 1 ) // Keep only elements not divisible by 2 res8 : Array = Array ( 1, 3, 5 ) Array ( 1, 2, 3, 4, 5 ). See example 4.1 - BuildersFactories 4.1.3 Transforms Array ( 1, 2, 3, 4, 5 ). This can be more convenient than usingīuilders ( 4.1.1) in many common use cases. tabulate ( 5 ) (n => s "hello $n " ) // Array with 5 items, each computed from its index res5 : Array = Array ( "hello 0", "hello 1", "hello 2", "hello 3", "hello 4" ) Array ( 1, 2, 3 ) ++ Array ( 4, 5, 6 ) // Concatenating two Arrays into a larger one res6 : Array = Array ( 1, 2, 3, 4, 5, 6 ) 4.3.scalaįactory methods provide another way to instantiate collections: with everyĮlement the same, with each element constructed depending on the index, or from

scala filter method map

fill ( 5 ) ( "hello" ) // Array with "hello" repeated 5 times res4 : Array = Array ( "hello", "hello", "hello", "hello", "hello" ) Array. This is most useful forĬonstructing Arrays or immutable collections where you cannot add or removeĮlements once the collection has been constructed. "freezing" it into the collection you want at the end. result ( ) res3 : Array = Array ( 1, 2 ) 4.2.scalaīuilders let you efficiently construct a collection of unknown length, ArrayBuilder = ArrayBuilder b += 1 b += 2 b.

scala filter method map

In Chapter 3: Basic Scala, but they also apply to all the collections we willĬover in this chapter: Vectors ( 4.2.1), Sets ( 4.2.3), These operations are present on the Arrays we saw Scala collections provide many common operations for constructing them, querying







Scala filter method map