Skip to content

Lists

Clean a queue of job names, add an urgent job, and inspect the next one. List<A> is ordered and immutable. Match [] and [head | tail] to handle empty and non-empty queues explicitly.

Names arrive from a form with whitespace and blank entries. Trim each name, then remove empty names. Keep duplicates: two jobs with the same name may be two separate requests.

mod lists
alias gale_std.list
alias gale_std.string
pub fn urgent(name: String, jobs: List<String>) -> List<String> {
[name | jobs]
}
pub fn next(jobs: List<String>) -> Option<String> {
case jobs {
[] -> None
[head | _] -> Some(head)
}
}
pub fn clean(jobs: List<String>) -> List<String> {
jobs
|> list.map(fn(name: String) -> string.trim(name))
|> list.filter(fn(name: String) -> name != "")
}

|> passes the value into the first argument of the next call. map transforms each entry; filter keeps entries for which the callback returns true. Trimming must happen first, otherwise a whitespace-only entry would survive.

urgent builds a new list with one extra head. The original queue stays unchanged. next returns Some(name) or None, so an empty queue is an ordinary outcome. It does not remove the job from the queue.