Workshop: Your first Elm program

I got a slot for a 90 min or 45 min workshop at the conference “Agila Sverige” (Agile Sweden). Since my suggestion, a bit to my surprise, was accepted, I need to create said workshop. Oops. :slight_smile:

If you would be so kind and help me here, I will be ever so grateful. I am not completely lost and desperate, I just want to make as good workshop as possible with the help of your feedback.

Assume attendees know nothing about Elm, but are programmers and are curious. Some have their laptop with them.

  1. What do you think should be the learning objectives for such a workshop?
  2. Do you have any thoughts on the format of the workshop?

My objective is of course to raise the interest for Elm. Although the conference is focused on Agile in general, it is here, two years ago that I first heard about Elm.

2 Likes

45 min should be enough for a quick introduction and a small demo. Since this is an Agile conference maybe some demo could be useful.

It could be useful to present the fact that Elm is different from the rest of the languages and to present the trade-offs it makes.

Maybe make a case for “a small number of powerful constructs” (primitive data, ADTs, records, stateless functions) and the emphasis on explicit, predictable and reliable code.

Elm is one of the very few declarative languages that compile to JS. I somehow wish that this be advertised more.

The idea of Railway oriented programming might also be useful in the argument for reliable code.

Best of luck!

2 Likes

Thank you @pdamoc. Yes, focus on some features where Elm shines is a good start.

(In my book, Elm is a functional language while Prolog is declarative. )

Both are declarative. One is functional the other logic. The main thing is that this is different from imperative languages where you have the idea of time complected with the code.

The workshop is found here: https://github.com/crispab/elm-workshop

There are branches for each step in the programming exercise.

In the docs folder there is the presentation.

The workshop will be held May 24th. So if you spot something, please let me know! <3

2 Likes

Thank you Per for sharing this, interesting to see your approach to introducing elm.
When I looked through the presentation, I was surprised a few times by the model in the tic-tac-toe example. What is the NoPlayer value in Model.currentPlayer good for?
In the step3 branch, I see that currentPlayer is initialized to X:


I am wondering when NoPlayer is used in currentPlayer?

What not to learn in 2019 on CodeMentor. One of your attendees might be feeling cantankerous.

This might be useful: a video of a talk/demo https://youtu.be/kLumTtZ8awI there are about 7 minutes (from 2:10 to 9:00) of demo — showing the power of the Elm compiler and its error messages when refactoring.

Your nice workshop example uses the classic transliteration of the board layout to List List Player. Someone will probably suggest using List Player or Array Player eg.

import Array exposing (Array)

type alias Board =
    Array Player

with simple initialisation:

board = Array.fromList [ NoPlayer, NoPlayer, NoPlayer, NoPlayer, NoPlayer, NoPlayer, NoPlayer, NoPlayer, NoPlayer ]

The rest falls into place easily. Post workshop refactoring?

Well spotted.

I am using the type Player both for currentPlayer and the value of each cell on the board. This feels a bit of weak modelling that I am reusing the type for two different purposes.

One could defend it with that who goes first, will in a later version be determined by the user and thus it should initially be NoPlayer.

He he I am pretty confident that those to elect to go to my workshop have a positive bias towards Elm.

The video link was to something in french and auto-translate was not enjoyable but thanks anyway. (Sometimes I get my blog posts translated to French, but it is not me doing it).

It will be interesting to see how the audience reacts to the modelling. There was a point with using lists, I wanted to show pattern matching with lists as here:

listGetAt : Int -> List a -> Maybe a
listGetAt index list =
    case list of
        head :: tail ->
            if index <= 0 then
                Just head

            else
                listGetAt (index - 1) tail

        [] ->
            Nothing

Another initialisation of an Array is by using repeat.

board = Array.repeat 9 NoPlayer

https://youtu.be/kLumTtZ8awI was in French. The code was in English. Works Ok with the sound off !

I hadn’t heard of Elm until two weeks ago - your reply has taught me about pattern matching on Lists and another initialisation of Arrays. Much easier. Thanks.

Another refactoring I did was to Player. This resolves Viir’s question and showed the power of the compiler. Once the code compiled it worked.

type alias Board =
    List (List CellValue)

type Player
    = X
    | O

type CellValue
    = NoPlayer
    | PlayerTag Player  -- nested custom type

The next refactoring was to delete type CellValue and introduce Maybe Player on the Board.

type alias Board =
    List (List (Maybe Player))

type Player
    = X
    | O

A simple search and replace with the editor: CellValue to (Maybe Player) and PlayerTag to Just Player got the code working again.

Your code is very good for learning with. Plenty of small things to try. Good luck with the workshop.

1 Like

You’re on a roll! Thanks for the kind words.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.