$f = fn(InputType $x): OutputType => $y;
Maybe
Either
Set
Sequence
State
IO
Free
...
Maybe
Either
function userController(string $id): Response {
return getUser($id)->match(
fn(User $user) => new Response($user->toArray()),
fn() => new Response(null, 404),
);
}
function userController(string $id): Response {
return getUser($id)
->map(fn(User $user) => $user->toArray())
->match(
fn(array $data) => new Response($data),
fn() => new Response(null, 404),
);
}
function brotherController(string $id): Response {
return getUser($id)
->map(fn(User $user) => $user->getBrother())
->map(fn(User $brother) => $brother->toArray())
->match(
fn(array $data) => new Response($data),
fn() => new Response(null, 404),
);
}
function brotherController(string $id): Response {
return getUser($id)
->map(fn(User $user) => $user->getBrotherId())
->flatMap(fn(string $brotherId) => getUser($brotherId))
->map(fn(User $brother) => $brother->toArray())
->match(
fn(array $data) => new Response($data),
fn() => new Response(null, 404),
);
}
function brotherController(string $id): Response {
return getUser($id)
->map(fn(User $user) => $user->getBrotherId())
->flatMap(fn(string $brotherId) => getUser($brotherId))
->map(fn(User $user) => $user->toArray())
->match(
fn(array $data) => new Response($data),
fn() => new Response(null, 404),
);
}