Lens Person
Read and change fields deep inside a nested record of a person, their birth and their address, without writing out every level by hand.
Adapted from Exercism’s problem specification (MIT). Listed under Collections & Algorithms.
// FILE: lens_person.nomi
//# Reads and updates fields deep inside a nested record, as lenses do.
import std::calendar.{self, Date}
/// A person: their name, where and when they were born, and where they live.
pub struct Person
name: Name
born: Born
address: Address
end
/// The fore names and surname of a person.
pub struct Name
fore_name: String
sur_name: String
end
/// Where and on what day someone was born.
pub struct Born
at: Address
on: Date
end
/// A postal address.
pub struct Address
house_number: Int
street: String
place: String
country: String
end
/// The street of the address where someone was born.
pub fn born_street(born: Born) -> String
born.at.street
end
/// `person` living on `street`, at the same house number and place.
pub fn set_current_street(person: Person, street: String) -> Person
{..person, address: {.., street:}}
end
/// `person` born in `month` of the same year, on the same day, or the
/// calendar's error when that month has no such day.
pub fn set_birth_month(person: Person, month: Int) -> Result<Person, calendar.Error>
on = person.born.on
on = Date.new(Date.year(on), month, Date.day(on))?
Ok({..person, born: {.., on:}})
end
/// `person` with `rename` applied to the street they were born on and the
/// street they live on.
pub fn rename_streets(person: Person, rename: (String) -> String) -> Person
{
..person,
born: {.., at: {.., street: rename(..)}},
address: {.., street: rename(..)},
}
end
// FILE: lens_person_test.nomi
import
std::calendar.Date
lens_person.{self, Person}
end
once jane = Person{
name: {fore_name: "Jane Joanna", sur_name: "Doe"},
born: {
at: {
house_number: 22,
street: "Implicit Street",
place: "Lazy Town",
country: "Haskell Republic",
},
on: Date"1970-03-02",
},
address: {
house_number: 1337,
street: "Coder Street",
place: "Hacker City",
country: "Functionland",
},
}
test "bornStreet"
assert lens_person.born_street(jane.born) == "Implicit Street"
end
test "setCurrentStreet"
moved = lens_person.set_current_street(jane, "Puppy Street")
assert moved.address.street == "Puppy Street"
assert moved == {..jane, address: {.., street: "Puppy Street"}}
end
test "setBirthMonth"
assert Ok(person) = lens_person.set_birth_month(jane, 9)
assert person.born.on == Date"1970-09-02"
assert person.born.at == jane.born.at
end
test "renameStreets birth"
person = lens_person.rename_streets(jane, String.uppercase)
assert person.born.at.street == "IMPLICIT STREET"
end
test "renameStreets current"
person = lens_person.rename_streets(jane, String.uppercase)
assert person.address.street == "CODER STREET"
end
// Not canonical: the Haskell track's calendar clips a day past the month's
// end; std/calendar refuses it.
test "a birth month without the birth day is an error"
born_on_the_31st = {..jane, born: {.., on: Date"1970-03-31"}}
assert Err(_) = lens_person.set_birth_month(born_on_the_31st, 2)
endok bornStreet
ok setCurrentStreet
ok setBirthMonth
ok renameStreets birth
ok renameStreets current
ok a birth month without the birth day is an error
test result: ok. 6 passed, 0 failed
How it’s written
Section titled “How it’s written”- Adapted: the exercise asks for lenses, getter and setter pairs that compose, because in Haskell changing a field three records down means rebuilding each record on the way. Nomi’s spread update does that rebuilding itself:
{..person, address: {.., street:}}replaces one nested field and keeps the rest, at any depth, and a field path such asborn.at.streetreads one. So the four functions are plain reads and updates, with noLenstype; a lens type would be a second spelling of what the update already says. - The records are four structs,
Person,Name,BornandAddress, with the Haskell track’s fields in snake case, and the birth day a std/calendarDate. set_birth_monthanswers aResult:Date.newrefuses a day the new month does not have, where the Haskell track’sfromGregorianclips it to the month’s last day. The test file adds one case, marked not canonical, that sets February for a person born on the 31st.rename_streetstakes the renaming as a function and applies it to both streets in one update, where..in eachstreet:is the street already there.- The exercise has no canonical data. The tests follow the Haskell track’s: one person, Jane Joanna Doe, as a
once, with her birth day written as the checked literalDate"1970-03-02", and one test per function. The setters also check that nothing else changed by comparing with a spread update of the original.