Hello Christophe,

Sorry for the slow response.  I posted an answer in that thread, but just
so it's easy to find here, the current workaround is:

function countletters(n)
    return @match n begin
        1000                      => length("one thousand")
        _, if 100 <= n <= 999 end => 1
    endend


Cheers,
   Kevin

On Sat, Jan 17, 2015 at 8:39 AM, Christophe Meyer <
[email protected]> wrote:

> How would you match a range with current functionality or is there a
> workaround ? (please see issue
> https://github.com/kmsquire/Match.jl/issues/4)
>
> Le jeudi 24 octobre 2013 14:29:30 UTC+2, [email protected] a écrit :
>
>> Nice! Looks like most features are supported already.
>>
>> For ranges, you have to decide whether to support non-integer values,
>> which is trickier because of the bounds.
>>
>>
>> Regards
>>
>> Le jeudi 24 octobre 2013 14:13:42 UTC+2, Kevin Squire a écrit :
>>>
>>> I think you can actually do this in scala as well.  Anyway, I ended up
>>> making a package which handles most of this:
>>>
>>> https://github.com/kmsquire/Match.jl
>>>
>>> It doesn't handle ranges in this way yet, but that shouldn't be too hard
>>> to add. Thanks for the suggestion!
>>>
>>> Cheers!
>>>
>>>   Kevin
>>>
>>> On Wednesday, October 23, 2013, wrote:
>>>
>>>> Sorry for bumping such an old thread, but I thought Rust's match may be
>>>> worth taking inspiration from. While similar to Scala's, it is especially
>>>> interesting for the fact that its syntax allows to easily specify
>>>> several values or ranges (incredibly useful to recode data with a readable
>>>> syntax):
>>>>
>>>> match my_number {
>>>>   0     => println("zero"),
>>>>   1 | 2 => println("one or two"),
>>>>   3..10 => println("three to ten"),
>>>>   _     => println("something else")
>>>> }
>>>>
>>>> Of course in Julia ranges would rather be written using : . Allowing to 
>>>> specify whether bounds are included or excluded from the range could be 
>>>> useful, but maybe this would create too much a specific syntax to be worth 
>>>> it.
>>>>
>>>> Another feature Rust provides is that the compiler checks that all 
>>>> possible cases have been considered, or that a _ branch has explicitly 
>>>> been used.
>>>>
>>>>
>>>>
>>>> For more details:
>>>> http://static.rust-lang.org/doc/0.8/tutorial.html#pattern-matching
>>>>
>>>>
>>>> My two cents
>>>>
>>>>
>>>> Le mardi 18 décembre 2012 18:41:51 UTC+1, Stefan Karpinski a écrit :
>>>>>
>>>>> That would be cool. I could see having both switch and match – the
>>>>> former for computed goto and the latter for pattern matching.
>>>>>
>>>>>
>>>>> On Tue, Dec 18, 2012 at 12:37 PM, Kevin Squire <[email protected]>
>>>>> wrote:
>>>>>
>>>>>> I'm looking forward to what you come up with, Toivo.
>>>>>>
>>>>>> I've been thinking about this for a while as well, and would love to
>>>>>> see something like Scala's match statement, which I have found to be an
>>>>>> elegant way of matching almost any type.  In Julia, it would probably 
>>>>>> look
>>>>>> something like this;
>>>>>>
>>>>>>
>>>>>> type Card
>>>>>>    num::Int8
>>>>>>    suit::Stringend
>>>>>> # Note: American card names
>>>>>> name(c::Card) = match(c)
>>>>>>    case Card(1, _) "Ace"     end   # _ used as a catch all, though could 
>>>>>> be a variable
>>>>>>    case Card(11,_) "Jack"    end
>>>>>>    case Card(12,_) "Queen"   end
>>>>>>    case Card(13,_) "King"    end
>>>>>>    case Card(n, _) string(n) end   # n takes on card valueend
>>>>>>
>>>>>> hearts_points(c::Card) = match(c)
>>>>>>    case Card(12, "spades") 13 end
>>>>>>    case Card(_, "spades")   1 end
>>>>>>    case Card(n, "diamonds")        # not really necessary, just wanted
>>>>>>       n > 9 && n < 11 ? -10 : 0    # to show a match and variable subst.
>>>>>>    end
>>>>>>    case Card(_, _)          0 endend
>>>>>>
>>>>>> It works as a general switch statement as well.
>>>>>>
>>>>>> All of the above could be done in other ways, of course, but I
>>>>>> personally find this syntax quite appealing.
>>>>>>
>>>>>> Kevin
>>>>>>
>>>>>> On Sunday, December 16, 2012 5:18:08 AM UTC-8, Toivo Henningsson
>>>>>> wrote:
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> On Sunday, December 16, 2012 2:02:18 PM UTC+1, Stefan Karpinski
>>>>>>> wrote:
>>>>>>>>
>>>>>>>> That could also be a worthwhile language addition. I'm not sure if
>>>>>>>> the two can be made to coincide though – pattern matching and computed 
>>>>>>>> goto
>>>>>>>> are really very different under the hood and trying to squeeze both 
>>>>>>>> into
>>>>>>>> the same feature might or might not work well.
>>>>>>>
>>>>>>>
>>>>>>> I think you are most probably right, but we will not know for sure
>>>>>>> until at least both have been implemented.
>>>>>>> If you do add a switch statement to julia, I might very well hijack
>>>>>>> the syntax to invoke the pattern matching version though :) e.g.
>>>>>>>
>>>>>>>     @match switch
>>>>>>>         ...
>>>>>>>     end
>>>>>>>
>>>>>>>  --
>>>>>>
>>>>>>
>>>>>>
>>>>>
>>>>>

Reply via email to