Last active
December 17, 2024 13:50
-
-
Save leobm/00b15acd9858df05794c396491c2f3f5 to your computer and use it in GitHub Desktop.
A Writer Interface in gleam
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import writer | |
import string_writer | |
pub fn main() { | |
string_writer.create() | |
|> writer.write("Text1") | |
|> writer.write("Text2") | |
|> string_writer.print() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Idea to simulate Iterfaces in gleam ?