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 the create and delete pattern primitives.
  • The second, showRegistryData, prints formatted registry contents, and works well with the all and search pattern primitives, which are extensions of the phonebook’s read faculty.
<?php
function registryAction(callable $action, ...$args): IO
{
$action = m\mcompose(
// print message about action completion/failure
fn (bool $res) => IO\IO(f\concat(' ', 'Action', ($res ? 'completed' : 'not completed'))),
// pass function arguments to IO function
fn ($args) => $action(...$args)
);
return $action(IO\IO($args));
}
function showRegistryData(callable $transform, ...$args): IO
{
$show = m\mcompose(
// format printable data
fn ($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:

<?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 ...