Reputation: 11
Is str somehow implicitly converted to str.charAt, so the map function works?
val str = "abcdefghij"
println(List.range(0, 10).map(str));
Upvotes: 1
Views: 118
Reputation: 31252
since str
is Seq of character, .map
on str
will call given index.
eg.
scala> List.range(0, 10).map(str)
res0: List[Char] = List(a, b, c, d, e, f, g, h, i, j)
// or .apply
scala> List.range(0, 10).map(str.apply)
res7: List[Char] = List(a, b, c, d, e, f, g, h, i, j)
And str(index)
gives the value for index, see StringOps#apply(index: Int): Char
scala> "my string"(4)
res5: Char = t
Other Array
and Seq
example,
scala> val array = Array(1, 2, 3, 4, 5)
array: Array[Int] = Array(1, 2, 3, 4, 5)
scala> List.range(0, 4).map(array)
res1: List[Int] = List(1, 2, 3, 4)
scala> val list = Seq(10, 20, 30, 40, 50)
list: Seq[Int] = List(10, 20, 30, 40, 50)
scala> List.range(0, 4).map(list)
res3: List[Int] = List(10, 20, 30, 40)
Upvotes: 5