The string builder b to which elements were appended. The string builder to which elements are appended. Res5: StringBuilder = List( 1, 2, 3, 4) b Of all elements of this mutable indexed sequence are separated by the string sep.Įxample: scala> val a = List( 1, 2, 3, 4) Inside, the string representations (w.r.t. The written text begins with the string start and ends with the string end. The expression x = that is equivalent to if (x eq null) that eq null else x.equals(that).Īppends all elements of this mutable indexed sequence to a string builder using start, end, and separator strings. Going right to left with the start value z on the right: op(x_1, op(x_2. Note: :\ is alternate syntax for foldRight xs :\ z is the same as def :+ ( elem: B) ( implicit arg0: ClassTag) : ArrayĪpplies a binary operator to all elements of this mutable indexed sequence and a start value,.Implicit informationĭefinition Classes TraversableOnce → GenTraversableOnce , x n are the elements of this mutable indexed sequence. Going left to right with the start value z on the left: op(.op(op(z, x_1), x_2). The result of inserting op between consecutive elements of this mutable indexed sequence, Note that the folding function used to compute b is equivalent to that used to compute c. Note: /: is alternate syntax for foldLeft z /: xs is the same as As with ++, returns a new collection containing the elements from the left operand followed by theĪpplies a binary operator to a start value and all elements of this mutable indexed sequence, def ++: ( that: collection.TraversableOnce) : Array.This member is added by an implicit conversion from Array toĪrrayOps performed by method genericArrayOps in scala.Predef. Of this mutable indexed sequence followed by all elements of that. Result class That from the current representation type ReprĪ new collection of type That which contains all elements Which means that an implicit instance of type CanBuildFromĪn implicit value of class CanBuildFrom which determines the The same class as the current collection class Repr, but thisĭepends on the element type B being admissible for that class, The element type of the returned collection. Since TraversableOnce has no ++ method, we have to implement thatĭirectly, but Traversable and down can use the overload. Reuse that of ++ because many collections override it with more This overload exists because: for the implementation of ++: we should Mnemonic: the COLon is on the side of the new COLlection type. The resulting collection rather than the left one. It differs from ++ in that the right operand determines the type of Left operand followed by the elements from the right operand. Scala Language Specification, for in-depth information on the transformations the Scala compiler makes on Arrays (Sections 6.6 and 6.15 respectively.)Īs with ++, returns a new collection containing the elements from the "Scala 2.8 Arrays" the Scala Improvement Document detailing arrays since Scala 2.8. "The Scala 2.8 Collections' API" section on Array by Martin Odersky for more information. The value of seqReversed, on the other hand, will be computedīy converting to WrappedArray first and invoking the variant of reverse that returns another Value arrReversed will be of type Array, with an implicit conversion to ArrayOps occurring For instance,Ĭonsider the following code: val arr = Array( 1, 2, 3) The conversion to ArrayOps takes priority over the conversion to WrappedArray. While the conversion to WrappedArray is permanent as all operations return a WrappedArray. The conversion to ArrayOps is temporary, as all operations defined on ArrayOps return an Array, To (a subtype of ).īoth types make available many of the standard operations found in the Scala collections API. To (shown on line 4 of the example above) and a conversion Two implicit conversions exist in scala.Predef that are frequently applied to arrays: a conversion Line 2 is translated into a call to apply(Int), while line 3 is translated into a call to Numbers( 3) = 100 // replace the 4th array element with 100 val biggerNumbers = numbers.map(_ * 2) // multiply all numbers by twoĪrrays make use of two common pieces of Scala syntactic sugar, shown on lines 2 and 3 of the above Val first = numbers( 0) // read the first element Array is Scala's representationįor Java's T. Arrays are mutable, indexed collections of values.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |