Skip to content

Commit e2587fe

Browse files
jechollpil
authored andcommitted
Refactor string.gleam
1 parent d313a03 commit e2587fe

File tree

1 file changed

+6
-14
lines changed

1 file changed

+6
-14
lines changed

src/gleam/string.gleam

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import gleam/string_builder
55
import gleam/dynamic.{Dynamic}
6+
import gleam/iterator
67
import gleam/list
78
import gleam/order
89
import gleam/result
@@ -312,13 +313,6 @@ pub fn concat(strings: List(String)) -> String {
312313
|> string_builder.to_string
313314
}
314315

315-
fn repeat_help(chunk: String, result: List(String), repeats: Int) -> String {
316-
case repeats <= 0 {
317-
True -> concat(result)
318-
False -> repeat_help(chunk, [chunk, ..result], repeats - 1)
319-
}
320-
}
321-
322316
/// Create a new string by repeating a string a given number of times.
323317
///
324318
/// This function runs in linear time.
@@ -329,7 +323,9 @@ fn repeat_help(chunk: String, result: List(String), repeats: Int) -> String {
329323
/// "hahaha"
330324
///
331325
pub fn repeat(string: String, times times: Int) -> String {
332-
repeat_help(string, [], times)
326+
iterator.repeat(string)
327+
|> iterator.take(times)
328+
|> concat
333329
}
334330

335331
/// Join many strings together with a given separator.
@@ -344,14 +340,11 @@ pub fn repeat(string: String, times times: Int) -> String {
344340
pub fn join(strings: List(String), with separator: String) -> String {
345341
strings
346342
|> list.intersperse(with: separator)
347-
|> string_builder.from_strings
348-
|> string_builder.to_string
343+
|> concat
349344
}
350345

351346
type Direction {
352-
353347
Leading
354-
355348
Trailing
356349
Both
357350
}
@@ -466,8 +459,7 @@ external fn int_to_utf_codepoint(Int) -> UtfCodepoint =
466459
pub fn utf_codepoint(value: Int) -> Result(UtfCodepoint, Nil) {
467460
case value {
468461
i if i > 1114111 -> Error(Nil)
469-
i if i == 65534 -> Error(Nil)
470-
i if i == 65535 -> Error(Nil)
462+
65534 | 65535 -> Error(Nil)
471463
i if i >= 55296 && i <= 57343 -> Error(Nil)
472464
i -> Ok(int_to_utf_codepoint(i))
473465
}

0 commit comments

Comments
 (0)