in Review, Uncategorized

Elm-Lang : Client side functional programming

Elm is a client side pure functional programming language for programming. Elm code compiles down to HTML, CSS and Javascript. The introduction page mentions it being a competitor to React.

Elm is a functional language that compiles to JavaScript. It competes with projects like React as a tool for creating websites and web apps.

Dont just dismiss this as Yet another language that transpiles to JS. Elm has a lot of nifty features, that makes it a choice worth considering.

Tooling

Elm comes with comprehensive tooling.

  • elm - Top level command to run other commands
  • elm-package - Nifty package manager with some cool things under the hood, like semantic versioning
  • elm-make - Pretty standard make tool for elm programs
  • elm-repl - A interactive command line 
  • elm-reactor - A browser version of elm-make spins up a server from where build can be managed.

Enough already? Lets see some code now.

Code below is the obligatory counter example.

import Html exposing (Html, button, div, text)
import Html.App as App
import Html.Events exposing (onClick)

main =
  App.beginnerProgram { model = 0, view = view, update = update }

type Msg = Increment | Decrement

update msg model =
  case msg of
    Increment ->
      model + 1

    Decrement ->
      model - 1

view model =
  div []
    [ button [ onClick Decrement ] [ text "-" ]
    , div [] [  text (toString model) ]
    , button [ onClick Increment ] [ text "+" ]
    ]

 

To see the above code in action –

Elm-Make

Run elm-make on the file, this should create Html artifacts which could be run on any server, easiest would be a python-simpleHTTPServer.Screen Shot 2016-09-04 at 9.29.00 PM.png

Elm-Reactor

You can also run elm-reactor in the directory to get a web interface such as below.Screen Shot 2016-09-04 at 9.58.49 PM.png

 

Overall, i found elm to refreshingly new. Having a considerably different syntax, web developers may find a steep learning curve intially. Though funcational programmers would find it fairly comfortable. Elm-debugger is something that has a very intersting approach too.

Write a Comment

Comment