I just converted the following Java into Scala:
char[] map = new char[64];
int i=0;
for (char c='A'; c<='Z'; c++) map[i++] = c;
for (char c='a'; c<='z'; c++) map[i++] = c;
for (char c='0'; c<='9'; c++) map[i++] = c;
map[i++] = '+';
map[i++] = '/';
My first attempt was an array:
val map1 = (
('A' to 'Z') ++ ('a' to 'z') ++ ('0' to '9')
).toArray[Char] ++ Array('+', '/');
This works! A bit more reading and I realised that Array
is a mutable type, while List
is immutable, so I changed it to:
val map1 = (
('A' to 'Z') ++ ('a' to 'z') ++ ('0' to '9')
).toList ++ List('+', '/');
The code gets a little more readable here, with the toList
trait not taking a type argument, whereas I needed to write toArray[Char]
. Why? Is this a question of Java interoperability, or is it a library inconsistency, with toList
coming from Iterable
and toArray
coming from Collection
?