From c083395455b8ddcbce9cab89c0e41e7f0e9d1846 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20W=C3=BChr?= Date: Fri, 13 Sep 2024 08:56:20 +0200 Subject: [PATCH] feat(iterator): implement `iterator_from_string` (closes #3) --- manifest.toml | 11 +++++++++++ src/brilo.gleam | 24 +++++++++++++++++++++--- test/brilo_test.gleam | 15 +++++++++++---- 3 files changed, 43 insertions(+), 7 deletions(-) create mode 100644 manifest.toml diff --git a/manifest.toml b/manifest.toml new file mode 100644 index 0000000..e851620 --- /dev/null +++ b/manifest.toml @@ -0,0 +1,11 @@ +# This file was generated by Gleam +# You typically do not need to edit this file + +packages = [ + { name = "gleam_stdlib", version = "0.40.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "86606B75A600BBD05E539EB59FABC6E307EEEA7B1E5865AFB6D980A93BCB2181" }, + { name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" }, +] + +[requirements] +gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" } +gleeunit = { version = ">= 1.0.0 and < 2.0.0" } diff --git a/src/brilo.gleam b/src/brilo.gleam index ca67592..6168981 100644 --- a/src/brilo.gleam +++ b/src/brilo.gleam @@ -1,5 +1,23 @@ -import gleam/io +import gleam/iterator.{type Iterator, Done, Next} +import gleam/string -pub fn main() { - io.println("Hello from brilo!") +/// Creates an iterator that yields each grapheme from the given string. +/// +/// ## Examples +/// +/// ```gleam +/// iterator_from_string("abcd") +/// |> to_list +/// // -> ["a", "b", "c", "d"] +/// ``` +/// +pub fn iterator_from_string(string: String) -> Iterator(String) { + let yield = fn(s) { + case s |> string.pop_grapheme { + Ok(#(x, xs)) -> Next(x, xs) + _ -> Done + } + } + + iterator.unfold(from: string, with: yield) } diff --git a/test/brilo_test.gleam b/test/brilo_test.gleam index 3831e7a..627252e 100644 --- a/test/brilo_test.gleam +++ b/test/brilo_test.gleam @@ -1,3 +1,6 @@ +import brilo +import gleam/iterator +import gleam/string import gleeunit import gleeunit/should @@ -5,8 +8,12 @@ pub fn main() { gleeunit.main() } -// gleeunit test functions end in `_test` -pub fn hello_world_test() { - 1 - |> should.equal(1) +pub fn iterator_from_string_test() { + let string = "BRILO RULEZ! 🎆" + + string + |> brilo.iterator_from_string + |> iterator.to_list + |> string.join("") + |> should.equal(string) }