lambdalicious
lambdalicious copied to clipboard
Using pattern matching :heart_eyes_cat:
Just a quick idea. If there are placeholders like __
, maybe there is a chance to implement something like erlang has with its pattern matching system. :heart_eyes_cat:
A simple matching on ints, strings or booleans for a start would be interesting. :heartbeat:
Just a very tiny example: https://gist.github.com/h4cc/7bb9da10d6897944a63c
Best idea i had till now was using a Pair(pattern, function) to solve that.
Looks cool :-) Please make a PR so we can explore it further.
We need examples that proof the usefulness though. If we can only do pattern matches that can be easily rewritten as return $x == 42 ? "Called with 42" : "Called with $x";
then there's no point.
this would be very practical if we could do something like this:
<?php
function map($function, $list)
{
match($list,
with(l(), l()),
with(cons($head, $tail),
cons($function($head), map($function, $tail))
)
);
}
trouble with this is that $head
& $tail
are unassigned at the moment we do the with()
call, which will lead to php errors&warnings. Anyone with ideas about how to fix this?