Discussion:
[elm-discuss] More pattern matching
David Legard
2017-08-22 12:46:11 UTC
Permalink
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?
--
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+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Peter Damoc
2017-08-22 13:35:30 UTC
Permalink
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
Post by David Legard
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?
--
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
For more options, visit https://groups.google.com/d/optout.
--
There is NO FATE, we are the creators.
blog: http://damoc.ro/
--
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+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Ilias Van Peer
2017-08-22 14:20:01 UTC
Permalink
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
Post by David Legard
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?
--
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+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
David Legard
2017-08-23 00:01:44 UTC
Permalink
Thanks for the suggestions.

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

I shall incorporate these ideas into my app.

Thanks again.
--
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+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Sebastian Porto
2017-08-24 05:35:23 UTC
Permalink
Maybe this package
http://package.elm-lang.org/packages/Fresheyeball/elm-guards/latest ?
--
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+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
David Legard
2017-08-24 08:41:44 UTC
Permalink
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.
--
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+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Continue reading on narkive:
Loading...