feat(iterator): implement iterator_from_string (closes #3)

This commit is contained in:
Alex 2024-09-13 08:56:20 +02:00
parent 3e31ef4e7c
commit c083395455
Signed by: l-x
SSH key fingerprint: SHA256:MK3uQVPHEV0Oo2ry/dAqvVK3pAwegKAwSlyfgLd/yQM
3 changed files with 43 additions and 7 deletions

11
manifest.toml Normal file
View file

@ -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" }

View file

@ -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)
}

View file

@ -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)
}