This is an alternative site for discovering Elm packages. You may be looking for the official Elm package site instead.
1.0.0
guards notation defined in user space
version 1.0.1
license BSD3
native-modules False
elm-version 0.18.0 <= v < 0.19.0
Tag 1.0.1
Committed At 2016-11-22 18:10:13 UTC
elm-lang/core 5.0.0 <= v < 6.0.0 5.1.1

Modules

README

Guards

This is guards notation defined in user space.

Usage

If statements in Elm can get... verbose.

foo : Int -> String
foo x =
  if x < 10 then
    "x was small"
  else if x > 100 then
    "x was big"
  else if x == 25 then
    "x was 25 exactly"
  else
    "we hit default case"

Guards help with all that

foo : Int -> String
foo x = x < 10  => "x was small"
     |= x > 100 => "x was big"
     |= x == 25 => "x was 25 exactly"
     |= "we hit default case"

User space?

Guards are usually compiler feature, its even one Elm had, but it was removed. There are 3 advantages to doing this in user space.

  • Functions are total, (there is no way to avoid providing a default)
  • Same tight notation as when supported by the compiler
  • Fewer language features (user space is its own reward)

You can nest and get crazy. Don't.

Guards make code more readable for simple flat expressions with lot of branches, not for times like this:

foo m a = case m of
  Just x -> x >= 0 => "positive!"
         |= x <  0 => (a == 100 => "wowzers!"
                      |= a > 100 => "trousers!"
                      |= "a wasn't cool")
         |= case a of
           0 -> "a was 0"
           _ -> "who knows"
  Nothing -> "murf"

The above code will compile, but you should be sad if your code looks like that.