From: Jean Privat Date: Sat, 8 Nov 2014 01:33:55 +0000 (-0500) Subject: lib: add Array::* for repetitions X-Git-Tag: v0.6.11~44^2~5 X-Git-Url: http://nitlanguage.org lib: add Array::* for repetitions Signed-off-by: Jean Privat --- diff --git a/lib/standard/collection/array.nit b/lib/standard/collection/array.nit index de06850..7d710bd 100644 --- a/lib/standard/collection/array.nit +++ b/lib/standard/collection/array.nit @@ -394,6 +394,26 @@ class Array[E] res.append(other) return res end + + # Repetition of arrays. + # + # returns a new array built by concatenating `self` `repeat` times. + # + # var a = [1,2,3] + # assert (a * 0).is_empty + # assert a * 1 == [1,2,3] + # assert a * 2 == [1,2,3,1,2,3] + # assert (a * 10).length == 30 + fun *(repeat: Int): Array[E] + do + assert repeat >= 0 + var res = new Array[E].with_capacity(length * repeat) + while repeat > 0 do + res.add_all(self) + repeat -= 1 + end + return res + end end # An `Iterator` on `AbstractArray`