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].

Answers


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.

How about:

(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

Need Your Help

show little message on hover over an image button on ASP.NET?

c# asp.net hover imagebutton

I'm developing a system which uses a lot of image buttons for redirections...

SQL Server - Same table in multiple schemas

sql sql-server database schema sql-server-2012-express

I am creating a smaller sized database in Microsoft SQL Server 2012 to keep run data from machines. The company has production machines and R & D machines. I would like to use the same table for

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.