This is the code that I currently use
val bytes = new Array[Byte](20)
scala.util.Random.nextBytes(bytes)
sendAndReceive(bytes)
Is there a way to turn that into a one-liner? For example, if it is an Integer array I can do
sendAndReceive(Array.fill(20){scala.util.Random.nextInt(9)}
Replacing nextInt
with nextBytes
does not work because nextBytes takes an Array[Byte]
as parameter, instead of returning a single Byte.
How about manually doing it? Byte
range is from -128 to 127. That gives us:
Array.fill(20)((scala.util.Random.nextInt(256) - 128).toByte)
You can also write an implicit if you need it at multiple places.
implicit class ExtendedRandom(ran: scala.util.Random) {
def nextByte = (ran.nextInt(256) - 128).toByte
}
Array.fill(20)(scala.util.Random.nextByte)
As @Chris Martin suggested, you can also use nextBytes
in an implicit class.
implicit class ExtendedRandom(ran: scala.util.Random) {
def nextByteArray(size: Int) = {
val arr = new Array[Byte](size)
ran.nextBytes(arr)
arr
}
}
scala.util.Random.nextByteArray(20)
There's the Kestrel combinator:
def kestrel[A](x: A)(f: A => Unit): A = { f(x); x }
With it you can write:
sendAndReceive(kestrel(Array.fill[Byte](20)(0))(Random.nextBytes))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With