lambdalicious icon indicating copy to clipboard operation
lambdalicious copied to clipboard

Using pattern matching :heart_eyes_cat:

Open h4cc opened this issue 10 years ago • 3 comments

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:

h4cc avatar Nov 28 '14 11:11 h4cc

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.

h4cc avatar Nov 28 '14 13:11 h4cc

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.

mathiasverraes avatar Nov 28 '14 14:11 mathiasverraes

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?

turanct avatar Jun 14 '15 19:06 turanct