More pattern matching

248 views
Skip to first unread message

David Legard

unread,
Aug 22, 2017, 8:46:11 AM8/22/17
to Elm Discuss
Does Elm have anything between an if-then-else flow control and case ?

My use case is, I need to parse a string in various ways to decide which Analysis ADT to assign to the string.

Thus

sconvert : String -> Analysis
sconvert s
=
     
if s=="all" then AllDays
     
else if s=="unplayed" then Unplayed
     
else if String.startsWith "onday" s then OnDay (secondBit s)
     
else if String.contains "totz:" s then Totz (secondBit s)
     
else Unknown


There are about 30 different branches in my app, and it's convenient to keep them together.

I can't use a case expression here, so I guess what I'm looking for is something like F#'s match expression, where you can add conditions using the when modifier. (code written as if the function existed in Elm

fizzBuzz : Int -> String
fizzBuzz x
=
    match x
with
   
| i when i % 15 = 0 ->  "fizzbuzz"
   
| i when i % 3 = 0 -> "fizz"
   
| i when i % 5 = 0 ->  "buzz"
   
| i  -> toString(i)

What would be an elegant way to implement the fizzBuzz function in Elm?
      

Peter Damoc

unread,
Aug 22, 2017, 9:35:36 AM8/22/17
to Elm Discuss
I don't know about elegant but one way to implement something like fizzBuzz would be: 
https://ellie-app.com/469wMYcvrZ3a1/0

Of course, this assumes that you have a lot of patterns and not the 4 in fizzBuzz

If evaluating all the branches is too expensive you can use functions like this:
https://ellie-app.com/469wMYcvrZ3a1/1 


 

--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
There is NO FATE, we are the creators.
blog: http://damoc.ro/

Ilias Van Peer

unread,
Aug 22, 2017, 10:20:01 AM8/22/17
to Elm Discuss
If you're actually parsing a string, you may also want to look into one of the various parsing libraries like `elm-tools/parser`. If you're willing to give a few more details about the format of those strings, I'm sure people here (and on slack) would be more than willing to give you a hand in figuring things out.

Another alternative, in the same vein as Peter's proposals but including more crimes against humanity (and loosely resembling Haskell's guard syntax) is this one: https://ellie-app.com/46bdfm75sqja1/0

Op dinsdag 22 augustus 2017 14:46:11 UTC+2 schreef David Legard:

David Legard

unread,
Aug 22, 2017, 8:01:44 PM8/22/17
to Elm Discuss
Thanks for the suggestions.

Not visually elegant, perhaps, but conceptually quite neat and satisfying.

I shall incorporate these ideas into my app.

Thanks again.

Sebastian Porto

unread,
Aug 24, 2017, 1:35:24 AM8/24/17
to Elm Discuss

David Legard

unread,
Aug 24, 2017, 4:41:44 AM8/24/17
to Elm Discuss

Thanks, that's rather a clever little package.

The maintainer says that guards were in the Elm compiler at one time, but then removed as being unfamiliar and largely unnecessary.

It's nice to have this small (23-line) implementation around, though.
Reply all
Reply to author
Forward
0 new messages