From 98b57765676d07ff984d8a27543aa4c5e1388b97 Mon Sep 17 00:00:00 2001 From: Jean Privat Date: Fri, 7 Nov 2014 20:33:55 -0500 Subject: [PATCH] lib: add Array::* for repetitions Signed-off-by: Jean Privat --- lib/standard/collection/array.nit | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) 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` -- 1.7.9.5