I’ve started learning PureScript - and I’m working through the PureScript by example exercises.
In Ch3 the final exercise is
(Difficult) Write a function removeDuplicates which
removes duplicate address book entries with the same first
and last names. Hint: Use PSCi to find the type of the
Data.List.nubBy function, which removes duplicate
elements from a list based on an equality predicate.
instead of using nubBy I decided that I wanted to try creating an Eq instance for the relevant records. I figured out that this required wrapping them in newtype - which then adds some over head for unwrapping and wrapping the record.
in one example I had to change a function like so
-- before
findAddress :: String -> String -> String -> AddressBook -> Maybe Entry
findAddress street city state = head <<< filter filterAddresses
where
filterAddresses :: Entry -> Boolean
filterAddresses { address: a } =
a.street == street &&
a.city == city &&
a.state == state
-- after
findAddress :: String -> String -> String -> AddressBook -> Maybe Entry
findAddress street city state = head <<< filter filterAddresses
where
filterAddresses :: Entry -> Boolean
filterAddresses = unwrap >>> (\{ address: wa } ->
(unwrap >>> (\a ->
a.street == street &&
a.city == city &&
a.state == state)) wa)
nesting calls to unwrap feels particularly ugly to me - and I’m certain that there must be a cleaner way to do this. Any suggestions?