Nil
in Gleam is a type with a single value, also called Nil
. It is similar to void
in other languages in that it is used when a function does not have any more suitable value to return.
io.println("Hello, Joe!")
// -> Nil
Values in Gleam are not "nil-able" or "nullable" like in some other languages. A value can only be Nil
if it's type is Nil
, and a value of any other type can never be Nil
.
Like most programming languages Gleam has "side effects", so functions can read and change the state of the world, as well as returning a value.
The gleam/io
module in the Gleam standard library provides functions for printing strings to the console.
io.println("Hello, Joe!")
// Hello, Joe!
// -> Nil
Other packages may provide other IO functions, such as simplifile
, a package which provides functions for reading and writing files.
simplifile.read("favourite-colour.txt")
// -> Ok("Pink\n")
Boris is a big model train enthusiast and has decided to share their passion with the world by starting a newsletter. They'll start by sending the first issue of the newsletter to friends and acquaintances that share the hobby, their email addresses are stored in a text file.
Hint: Use the simplifile module for file operations.
Implement the read_emails
function. It takes a path string to a text file that contains email addresses separated by newlines, and returns a list of the email addresses from the file.
read_emails("/home/my_user/documents/model_train_friends_emails.txt")
// -> Ok(["rick@example.com", "choochoo42@example.com", "anna@example.com"])
Sending an email is a task that might fail for many unpredictable reasons, like a typo in the email address or temporary network issues. To ensure that you can retry sending the emails to all your friends without sending duplicates, you need to log the email addresses that already received the email. For this, you'll need a log file.
Implement the create_log_file
function. It takes a file path and creates a new empty file at that location.
create_log_file("/home/my_user/documents/newsletter_issue1_log.txt")
// -> Ok(Nil)
Implement the log_sent_email
function. It takes a path to a log file and a string with the email address. It writes the email address to the file, followed by a newline.
log_sent_email(
"/home/my_user/documents/newsletter_issue1_log.txt",
"joe@example.com",
)
// -> Ok(Nil)
Now that you have all of the building blocks of the email sending procedure, you need to combine them together in a single function.
Implement the send_newsletter
function. It takes a path of the file with email addresses, a path of a log file, and an anonymous function that sends an email to a given email address.
It should read all the email addresses from the given file and attempt to send an email to every one of them. If the anonymous function that sends the email returns an Ok
value, write the email address to the log file. Make sure to do it as soon as the email is sent.
send_newsletter(
"model_train_friends_emails.txt",
"newsletter_issue1_log.txt",
send_email,
)
// -> Ok(Nil)
Sign up to Exercism to learn and master Gleam with 36 concepts, 125 exercises, and real human mentoring, all for free.