How to make sure a Seq is of certain minimum length, in Scala?
The below code does what I want (adds empty strings until arr has three entries), but it feels clumsy.
scala> val arr = Seq("a")
arr: Seq[String] = List(a)
scala> arr ++ Seq.fill(3-arr.size)("")
res2: Seq[String] = List(a, "", "")
A way to fulfill this would be a merger of two sequences: take from first, but if it runs out, continue from second. What was such method called...?
I find this slightly better:
scala> (arr ++ Seq.fill(3)("")).take(3)
res4: Seq[String] = List(a, "", "")
And even better, thanks @thomas-böhm
scala> arr.toArray.padTo(3,"")
res5: Array[String] = Array(a, "", "")
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