Scala - generate Array of bytes based on range
I am trying to solve a (possibly) trivial problem. I would like a nice, concise way to instantiate Array of bytes based on range. So far this works
Array(1 : Byte, 2 : Byte)
but I would like to use sth like
((1: Byte) to (10: Byte)).toArray
this is however Array[Int].
Range is not generic; it inherits from IndexedSeq[Int], so there's no way to make a "Range of Byte". (Edit: See Daniel C. Sobral's answer for a generic range type!)
When you try ((1: Byte) to (10: Byte)), the Bytes are implicitly converted back to Int again.
(1 to 10).map(_.toByte).toArray
That will result in two passes over the collection; if that's an issue, a non-strict view will rectify that:
(1 to 10).view.map(_.toByte).toArray