it-ebooks - An Introduction to Elm
Here you can read online it-ebooks - An Introduction to Elm full text of the book (entire story) in english for free. Download pdf and epub, get meaning, cover and reviews about this ebook. year: 2017, publisher: iBooker it-ebooks, genre: Computer. Description of the work, (preface) as well as reviews are available. Best literature library LitArk.com created for fans of good reading and offers a wide selection of genres:
Romance novel
Science fiction
Adventure
Detective
Science
History
Home and family
Prose
Art
Politics
Computer
Non-fiction
Religion
Business
Children
Humor
Choose a favorite category and find really read worthwhile books. Enjoy immersion in the world of imagination, feel the emotions of the characters or learn something new for yourself, make an fascinating discovery.
An Introduction to Elm: summary, description and annotation
We offer to read an annotation, description, summary or preface (depends on what the author of the book "An Introduction to Elm" wrote himself). If you haven't found the necessary information about the book — write in the comments, we will try to find it.
An Introduction to Elm — read online for free the complete book (whole text) full work
Below is the text of the book, divided by pages. System saving the place of the last page read, allows you to conveniently read the book "An Introduction to Elm" online for free, without having to search again every time where you left off. Put a bookmark, and you can go to the page where you finished reading at any time.
Font size:
Interval:
Bookmark:
- 1.1
- 1.2
- 1.3
- 1.4
- 1.4.1
- 1.4.1.1
- 1.4.1.2
- 1.4.1.3
- 1.4.1.4
- 1.4.2
- 1.4.2.1
- HTTP 1.4.2.2
- 1.4.2.3
- 1.4.2.4
- 1.4.3
- 1.4.1
- 1.5
- 1.5.1
- 1.5.2
- 1.5.3
- 1.6
- 1.6.1
- 1.6.2
- 1.6.3
- 1.7
- 1.7.1
- 1.7.2
- 1.8
- 1.8.1
- 1.8.2
- 1.8.3
- 1.8.4
Our first example is a simple counter that can be incremented or decremented. I find that it can be helpful to see the entire program in one place, so here it is! We will break it down afterwards.
import Html exposing ( Html , button, div, text) import Html.Events exposing (onClick) main = Html .beginnerProgram { model = model, view = view, update = update } -- MODEL type alias Model = Int model : Model model = -- UPDATE type Msg = Increment | Decrement update : Msg -> Model -> Model update msg model = case msg of Increment -> model + Decrement -> model - -- VIEW view : Model -> Html Msg view model = div [] [ button [ onClick Decrement ] [ text "-" ] , div [] [ text (toString model) ] , button [ onClick Increment ] [ text "+" ] ]
That's everything!
Note: This section has type
and type alias
declarations. You can read all about these in the upcoming section on types. You do not need to deeply understand that stuff now, but you are free to jump ahead if it helps.
When writing this program from scratch, I always start by taking a guess at the model. To make a counter, we at least need to keep track of a number that is going up and down. So let's just start with that!
type alias Model = Int
Now that we have a model, we need to define how it changes over time. I always start my UPDATE
section by defining a set of messages that we will get from the UI:
type Msg = Increment | Decrement
I definitely know the user will be able to increment and decrement the counter. The Msg
type describes these capabilities as data. Important! From there, the update
function just describes what to do when you receive one of these messages.
update : Msg -> Model -> Model update msg model = case msg of Increment -> model + Decrement -> model -
If you get an Increment
message, you increment the model. If you get a Decrement
message, you decrement the model. Pretty straight-forward stuff.
Okay, so that's all good, but how do we actually make some HTML and show it on screen? Elm has a library called elm-lang/html
that gives you full access to HTML5 as normal Elm functions:
view : Model -> Html Msg view model = div [] [ button [ onClick Decrement ] [ text "-" ] , div [] [ text (toString model) ] , button [ onClick Increment ] [ text "+" ] ]
You can see more examples of basic HTML here.
One thing to notice is that our view
function is producing a Html Msg
value. This means that it is a chunk of HTML that can produce Msg
values. And when you look at the definition, you see the onClick
attributes are set to give out Increment
and Decrement
values. These will get fed directly into our update
function, driving our whole app forward.
Another thing to notice is that div
and button
are just normal Elm functions. These functions take (1) a list of attributes and (2) a list of child nodes. It is just HTML with slightly different syntax. Instead of having <
and >
everywhere, we have [
and ]
. We have found that folks who can read HTML have a pretty easy time learning to read this variation. Okay, but why not have it be exactly like HTML? Since we are using normal Elm functions, we have the full power of the Elm programming language to help us build our views! We can refactor repetitive code out into functions. We can put helpers in modules and import them just like any other code. We can use the same testing frameworks and libraries as any other Elm code. Everything that is nice about programming in Elm is 100% available to help you with your view. No need for a hacked together templating language!
There is also something a bit deeper going on here. The view code is entirely declarative. We take in a Model
and produce some Html
. That is it. There is no need to mutate the DOM manually, Elm takes care of that behind the scenes. This gives Elm much more freedom to make clever optimizations and ends up making rendering faster overall. So you write less code and the code runs faster. The best kind of abstraction!
This pattern is the essence of The Elm Architecture. Every example we see from now on will be a slight variation on this basic pattern: Model
, update
, view
.
Exercise: One cool thing about The Elm Architecture is that it is super easy to extend as our product requirements change. Say your product manager has come up with this amazing "reset" feature. A new button that will reset the counter to zero.
To add the feature you come back to the Msg
type and add another possibility: Reset
. You then move on to the update
function and describe what happens when you get that message. Finally you add a button in your view.
See if you can implement the "reset" feature!
Your app will probably have some options people can mess with. If something happens, should you send them an email notification? If they come across a video, should it start playing by itself? That kind of thing. So you will need to create some HTML like this:
< fieldset > < label > < input type = "checkbox" > Email Notifications </<span class="hljs-type">label >
< label > < input type = "checkbox" > Video Autoplay </<span class="hljs-type">label > < label > < input type = "checkbox" > Use Location </<span class="hljs-type">label > </<span class="hljs-type">fieldset > That will let people toggle the checkboxes, and using means they get a much bigger area they can click on. Lets write an Elm program that manages all this interaction! As always, we will take a guess at our Model
. We know we need to track the users settings so we will put them in our model:
type alias Model = { notifications : Bool , autoplay : Bool , location : Bool }
From there, we will want to figure out our messages and update function. Maybe something like this:
type Msg = ToggleNotifications | ToggleAutoplay | ToggleLocation update : Msg -> Model -> Model update msg model = case msg of ToggleNotifications -> { model | notifications = not model.notifications } ToggleAutoplay -> { model | autoplay = not model.autoplay } ToggleLocation -> { model | location = not model.location }
That seems fine. Now to create our view!
view : Model -> Html Msg view model = fieldset [] [ label [] [ input [ type_ "checkbox", onClick ToggleNotifications ] [] , text "Email Notifications" ] , label [] [ input [ type_ "checkbox", onClick ToggleAutoplay ] [] , text "Video Autoplay" ] , label [] [ input [ type_ "checkbox", onClick ToggleLocation ] [] , text "Use Location" ] ]
Font size:
Interval:
Bookmark:
Similar books «An Introduction to Elm»
Look at similar books to An Introduction to Elm. We have selected literature similar in name and meaning in the hope of providing readers with more options to find new, interesting, not yet read works.
Discussion, reviews of the book An Introduction to Elm and just readers' own opinions. Leave your comments, write what you think about the work, its meaning or the main characters. Specify what exactly you liked and what you didn't like, and why you think so.