From 0a087d5d80b803e70909848f5b7b28ce2b5df4fc Mon Sep 17 00:00:00 2001 From: Richard Viney Date: Mon, 7 Oct 2024 17:40:31 +1300 Subject: Optimise string.repeat --- src/gleam/string.gleam | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/gleam/string.gleam b/src/gleam/string.gleam index e530c8f..0906481 100644 --- a/src/gleam/string.gleam +++ b/src/gleam/string.gleam @@ -438,10 +438,14 @@ pub fn concat(strings: List(String)) -> String { /// ``` /// pub fn repeat(string: String, times times: Int) -> String { - iterator.repeat(string) - |> iterator.take(times) - |> iterator.to_list - |> concat + do_repeat(string, times, "") +} + +fn do_repeat(string: String, times: Int, acc: String) -> String { + case times <= 0 { + True -> acc + False -> do_repeat(string, times - 1, acc <> string) + } } /// Joins many `String`s together with a given separator. -- cgit v1.2.3