[Elm][1] is a friendly functional programming language compiling to JavaScript. Elm focuses on browser-based GUIs, single-page applications.
Users usually praise it for:
The List
(linked list) shines in sequential access:
On the other hand, it's not ideal for random access (ie. getting nth element) and traversation in reverse order, and you might have better luck (and performance) with the Array
data structure.
Consult http://guide.elm-lang.org/interop/javascript.html from The Elm Guide to aid in understanding these examples.
Please play with these concepts yourself to really master them! The elm-repl
(see the Introduction to the REPL) is probably a good place to play around with the code above. You can also play with elm-repl
online.
Debug.log
takes two parameters, a String
to tag the debug output in the console (so you know where it's coming from / what the message corresponds to), and a value of any type. Debug.log
executes the side-effect of logging the tag and the value to the JavaScript console, and then returns the value. The implementation in JS might look something like:
function log (tag, value){
console.log(tag, value);
return value
}
JavaScript has implicit conversions, so value
doesn't have to be explicitly converted to a String
for the above code to work. However, Elm types must be explicitly converted to a String
, and the Native code for Debug.log
shows this in action.
Json.Decode
exposes two functions to decode a payload, first one is decodeValue
which tries to decode a Json.Encode.Value
, the second one is decodeString
which tries to decode a JSON string. Both function take 2 parameters, a decoder and a Json.Encode.Value
or Json string.
Subscriptions are means to listen to inputs. Incoming ports, keyboard or mouse events, WebSocket messages, geolocation and page visibility changes, all can serve as inputs.