GithubHelp home page GithubHelp logo

try-elm's Introduction

Elm

Try At Least Once (JavaScript libraries and frameworks) > JavaScript Flavors > Elm

Official website: https://elm-lang.org/

Github repository: https://github.com/elm


What is the Elm

Most of the search results in Google starts with this sentence: Elm is a functional language that compiles to JavaScript. In Elm, most of the things you see is a function. Even minus - and plus + operators are functions.

The fundamental principle of writing Elm code is to separate effects from computation and to use different techniques for each. All computation is “pure”, meaning what a function returns depends only on its arguments. It’s predictable and easy to understand.

If you are used to programming in Java or JavaScript, the first thing you will see is the strange syntax in Elm which has no braces but has lots of arrows.

Elm compiler analyzes your to see how values flow through your program. The compiler shows error messages if values mismatch the types or used in an invalid way.

Elm made up of three concepts:

  • Model — the state of the application
  • View — a way to turn the state into HTML
  • Update — takes messages and updates the state based on the content of the message

Installation

Visit https://guide.elm-lang.org/install.html to get the setup instructions. It's highly recommended especially for the functional language beginners, to use online editor and compiler of the Elm which is available at http://elm-lang.org/try.

Quick Example

This simple app almost clarifies the main concepts of the Elm language. There is a button which each time on the click event sends Roll message to the app then in the update section of the app, a function generates a random number between 1 to 100. New generated value returns to the view of the app.

import Browser
import Html exposing (..)
import Html.Events exposing (..)
import Random



-- MAIN


main =
  Browser.element
    { init = init
    , update = update
    , subscriptions = subscriptions
    , view = view
    }



-- MODEL


type alias Model =
  { dieFace : Int
  }


init : () -> (Model, Cmd Msg)
init _ =
  ( Model 1
  , Cmd.none
  )



-- UPDATE


type Msg
  = Roll
  | NewFace Int


update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
  case msg of
    Roll ->
      ( model
      , Random.generate NewFace (Random.int 1 100)
      )

    NewFace newFace ->
      ( Model newFace
      , Cmd.none
      )



-- SUBSCRIPTIONS


subscriptions : Model -> Sub Msg
subscriptions model =
  Sub.none



-- VIEW


view : Model -> Html Msg
view model =
  div []
    [ h1 [] [ text (String.fromInt model.dieFace) ]
    , button [ onClick Roll ] [ text "Roll" ]
    ]

try-elm's People

Contributors

amirhosseinrahmati avatar

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.