The Phonebook App
See the phonebook app in action.
We'll cover the following...
Patterns implementation
Before completing the pattern matching, it’s possible to add two routines to categorize, and hence simplify the printing output.
- The first of these functions,
registryAction
, conveys a message that signals the completion of a write action. It’s suitable for thecreate
anddelete
pattern primitives. - The second,
showRegistryData
, prints formatted registry contents, and works well with theall
andsearch
pattern primitives, which are extensions of the phonebook’s read faculty.
Press + to interact
<?phpfunction registryAction(callable $action, ...$args): IO{$action = m\mcompose(// print message about action completion/failurefn (bool $res) => IO\IO(f\concat(' ', 'Action', ($res ? 'completed' : 'not completed'))),// pass function arguments to IO functionfn ($args) => $action(...$args));return $action(IO\IO($args));}function showRegistryData(callable $transform, ...$args): IO{$show = m\mcompose(// format printable datafn ($data) => IO\IO(formatOutput($data)),fn ($args) => $transform(...$args));return $show(IO\IO($args));}
The rest of the patterns should now resemble the code in the following snippet:
Press + to interact
<?php'["add", name, phone]' => fn (string $name, string $phone) => (registryAction(create, $name, $phone)),'["delete", name]' => fn (string $name) => (registryAction(delete, $name)),'["search", name]' => fn (string $name) => (showRegistryData(search, $name)),'["all"]' => fn () => showRegistryData(readRegistry),
Phonebook in action
The REPL is almost complete at this point. The only glaring absence in the design is an actual loop. ...