Giter Club home page Giter Club logo

elm-guards's Introduction

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.

elm-guards's People

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.