Skip to content

Instantly share code, notes, and snippets.

@leobm
Last active November 3, 2023 11:35
Show Gist options
  • Save leobm/00b15acd9858df05794c396491c2f3f5 to your computer and use it in GitHub Desktop.
Save leobm/00b15acd9858df05794c396491c2f3f5 to your computer and use it in GitHub Desktop.
import writer
import string_writer
pub fn main() {
string_writer.create()
|> writer.write("Text1")
|> writer.write("Text2")
|> string_writer.print()
}
import writer.{Writer}
import gleam/string_builder.{StringBuilder}
import gleam/io
pub fn create() -> Writer(StringBuilder) {
Writer(
target: string_builder.from_strings([]),
write: fn(writer, str) {
let assert Writer(builder, write) = writer
let next_builder = string_builder.append(builder, str)
Writer(next_builder, write)
},
)
}
pub fn print(writer: Writer(StringBuilder)) {
let assert Writer(builder, _) = writer
io.print(string_builder.to_string(builder))
}
pub type Writer(a) {
Writer(target: a, write: fn(Writer(a), String) -> Writer(a))
}
pub fn write(writer: Writer(a), str) {
let assert Writer(_, write) = writer
write(writer, str)
}
@leobm
Copy link
Author

leobm commented Nov 2, 2023

Idea to simulate Iterfaces in gleam ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment