Skip to content

Commit 0a97827

Browse files
authored
Merge pull request #33 from MI-AFP/elm-part-update
Elm intro tutorial updated.
2 parents 3c5e583 + 718609c commit 0a97827

2 files changed

Lines changed: 75 additions & 42 deletions

File tree

tutorials/09_elm-intro.md

Lines changed: 75 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ If we have Elm installed, we can run Elm REPL by `elm repl` command. It is like
8585

8686
```
8787
$ elm repl
88-
---- Elm 0.19.0 ----------------------------------------------------------------
89-
Read <https://elm-lang.org/0.19.0/repl> to learn more: exit, help, imports, etc.
88+
---- Elm 0.19.1 ----------------------------------------------------------------
89+
Read <https://github.com/elm/compiler/blob/master/hints/repl.md> to learn more: exit, help, imports, etc.
9090
--------------------------------------------------------------------------------
9191
>
9292
```
@@ -239,11 +239,11 @@ It generates `elm.json` file that defines where the source files are and what ar
239239
"source-directories": [
240240
"src"
241241
],
242-
"elm-version": "0.19.0",
242+
"elm-version": "0.19.1",
243243
"dependencies": {
244244
"direct": {
245-
"elm/browser": "1.0.1",
246-
"elm/core": "1.0.2",
245+
"elm/browser": "1.0.2",
246+
"elm/core": "1.0.5",
247247
"elm/html": "1.0.0"
248248
},
249249
"indirect": {
@@ -352,8 +352,8 @@ linear a b x =
352352
A list is a collection of items of the same type with variable length. There is a [List](https://package.elm-lang.org/packages/elm/core/latest/List) module with various functions for working with lists.
353353

354354
```elm
355-
> numbers = [1, 3, 5, 7, 11]
356-
[1,3,5,7,11] : List number
355+
> numbers = [ 1, 3, 5, 7, 11 ]
356+
[ 1, 3, 5, 7, 11 ] : List number
357357

358358
> List.length numbers
359359
5 : Int
@@ -364,14 +364,14 @@ False : Bool
364364
> double n = n * 2
365365
<function> : number -> number
366366
> List.map double numbers
367-
[2,6,10,14,22] : List number
367+
[ 2, 6, 10, 14, 22 ] : List number
368368

369369
> List.map (\n -> n * 2) numbers
370-
[2,6,10,14,22] : List number
370+
[ 2, 6, 10, 14, 22 ] : List number
371371

372372

373373
> List.map ((*) 2) numbers
374-
[2,6,10,14,22] : List number
374+
[ 2, 6, 10, 14, 22 ] : List number
375375
```
376376

377377

@@ -380,14 +380,14 @@ False : Bool
380380
Dict is a mapping of unique keys to values. There is a [Dict](https://package.elm-lang.org/packages/elm-lang/core/latest/Dict) module with functions for working with dicts.
381381

382382
```elm
383-
> Dict.fromList [("Spencer", 25), ("Zoe", 21)]
384-
Dict.fromList [("Spencer",25),("Zoe",21)] : Dict.Dict String number
383+
> Dict.fromList [ ( "Spencer", 25 ), ( "Zoe", 21 ) ]
384+
Dict.fromList [ ("Spencer", 25 ), ("Zoe", 21 ) ] : Dict.Dict String number
385385

386386
> Dict.insert "Spencer" 25 Dict.empty
387-
Dict.fromList [("Spencer",25)] : Dict.Dict String number
387+
Dict.fromList [ ( "Spencer", 25 ) ] : Dict.Dict String number
388388

389-
> dict = Dict.fromList [("Spencer", 25), ("Zoe", 21)]
390-
Dict.fromList [("Spencer",25),("Zoe",21)] : Dict.Dict String number
389+
> dict = Dict.fromList [ ( "Spencer", 25 ), ( "Zoe", 21 ) ]
390+
Dict.fromList [ ("Spencer", 25 ),( "Zoe", 21 ) ] : Dict.Dict String number
391391
> Dict.isEmpty dict
392392
False : Bool
393393
> Dict.get "Zoe" dict
@@ -402,8 +402,8 @@ A tuple is a collection of items of various type with the fixed size. There is a
402402

403403

404404
```elm
405-
> person = ("Joe", 21)
406-
("Joe",21) : ( String, number )
405+
> person = ( "Joe", 21 )
406+
( "Joe" , 21 ) : ( String, number )
407407

408408
> Tuple.first person
409409
"Joe" : String
@@ -415,25 +415,25 @@ A tuple is a collection of items of various type with the fixed size. There is a
415415
We can use pattern matching for tuples in functions:
416416

417417
```elm
418-
bio : (String, Int) -> String
419-
bio (name, age) = name ++ " is " ++ (String.fromInt age) ++ " years old."
418+
bio : ( String, Int ) -> String
419+
bio ( name, age ) = name ++ " is " ++ ( String.fromInt age ) ++ " years old."
420420
```
421421

422422

423423
Elm has a limit on the maximum number of items in the tuple to be 3. If we need more, we should use a record or our own custom type.
424424

425425
```
426-
> vector4 = (4, 10, 12, 3)
426+
> vector4 = ( 4, 10, 12, 3 )
427427
-- BAD TUPLE --------------------------------------------------------------- elm
428428
429429
I only accept tuples with two or three items. This has too many:
430430
431-
8| vector4 = (4, 10, 12, 3)
432-
^^^^^^^^^^^^^^
431+
8| vector4 = ( 4, 10, 12, 3 )
432+
^^^^^^^^^^^^^^^^
433433
I recommend switching to records. Each item will be named, and you can use the
434434
`point.x` syntax to access them.
435435
436-
Note: Read <https://elm-lang.org/0.19.0/tuples> for more comprehensive advice on
436+
Note: Read <https://elm-lang.org/0.19.1/tuples> for more comprehensive advice on
437437
working with large chunks of data in Elm.
438438
```
439439

@@ -446,6 +446,10 @@ Records contain keys and values. Each value can have a different type.
446446
> vector4 = { w = 4, x = 10, y = 12, z = 3 }
447447
{ w = 4, x = 10, y = 12, z = 3 }
448448
: { w : number, x : number1, y : number2, z : number3 }
449+
450+
> scatterChart = { points = [ { x = 11, y = 8 } ) ], title = "Bar chart", xAxis = "x", yAxis = "y" }
451+
{ points = [ { x = 11, y = 8 } ) ], title = "Bar chart", xAxis = "x", yAxis = "y" }
452+
: { points : List { x : number1, y : number2 }, title : String, xAxis : String, yAxis : String }
449453
```
450454

451455

@@ -458,8 +462,8 @@ For accessing record properties, Elm has by default accessors defined as `.<key>
458462
> .x vector4
459463
10 : number
460464

461-
> List.map .x [vector4, vector4, vector4]
462-
[10,10,10] : List number
465+
> List.map .x [ vector4, vector4, vector4 ]
466+
[ 10, 10, 10 ] : List number
463467
```
464468

465469
If we have a look at the type of `.x` accessor, it says it is any record that has a field `x` of type `a` and returns `a`.
@@ -479,10 +483,10 @@ Since everything is immutable, records cannot be updated. We can create updated
479483
```
480484

481485

482-
We can use pattern matching for record keys:
486+
We can use pattern matching (desctructuring) for record keys:
483487

484488
```elm
485-
> length {w, x, y, z} = sqrt (w * w + x * x + y * y + w * w)
489+
> length { w, x, y, z } = sqrt (w * w + x * x + y * y + w * w)
486490
<function> : { b | w : Float, x : Float, y : Float, z : a } -> Float
487491
> length vector4
488492
16.61324772583615 : Float
@@ -511,6 +515,16 @@ type alias Person =
511515
isAdult : Person -> Bool
512516
isAdult { age } =
513517
age >= 18
518+
519+
520+
getName : Person -> Name
521+
getName { name } =
522+
name
523+
524+
525+
getName2 : Person -> String
526+
getName2 { name } =
527+
name
514528
```
515529

516530
```elm
@@ -536,9 +550,9 @@ We can define custom types that have several variants. We can also associate dat
536550

537551

538552
```elm
539-
type Gender
540-
= Male
541-
| Female
553+
type Animal
554+
= Cat
555+
| Dog
542556

543557

544558
type Tree a
@@ -553,24 +567,27 @@ type Profile
553567

554568

555569

556-
gender = Female
570+
animal = Dog
557571

558572
tree = Branch (Leaf 1) (Branch (Leaf 2) (Leaf 0))
559573

560574
profile = Error "Cannot load profile"
561575

562576
```
563577

578+
*Note*: There are two more complex techniques, how to design data structure - opaque types and phantom types.
579+
580+
564581
### Pattern Matching
565582

566583
```elm
567-
isFemale : Gender -> Bool
568-
isFemale gender =
569-
case gender of
570-
Male ->
584+
isDog : Animal -> Bool
585+
isDog animal =
586+
case animal of
587+
Cat ->
571588
False
572589

573-
Female ->
590+
Dog ->
574591
True
575592
```
576593

@@ -587,6 +604,24 @@ isLoading profile =
587604
False
588605

589606

607+
isLoading2 : Profile -> Bool
608+
isLoading2 profile =
609+
profile == Loading
610+
611+
612+
isLoading3 : Profile -> Bool
613+
isLoading3 profile =
614+
case profile of
615+
Loading ->
616+
True
617+
618+
Error _ ->
619+
False
620+
621+
Success _ ->
622+
False
623+
624+
590625
profileStatus : Profile -> String
591626
profileStatus profile =
592627
case profile of
@@ -817,10 +852,7 @@ number: 4
817852

818853
## Packages
819854

820-
Elm packages are published on [package.elm-lang.org](https://package.elm-lang.org). There is forced [semantic versioning](https://semver.org) for Elm packages. Therefore, we don't have to worry about breaking our application while updating packages.
821-
822-
In 2018, Elm 0.19 was released, and not all packages have been updated yet, so we need to check first whether the package is supported in the latest Elm version.
823-
855+
Elm packages are published on [package.elm-lang.org](https://package.elm-lang.org). There is forced [semantic versioning](https://semver.org) for Elm packages.
824856

825857

826858
To install a package, we use `elm install` command in the project directory.
@@ -830,7 +862,7 @@ $ elm install elm-community/maybe-extra
830862
Here is my plan:
831863
832864
Add:
833-
elm-community/maybe-extra 5.0.0
865+
elm-community/maybe-extra 5.2.0
834866
835867
Would you like me to update your elm.json accordingly? [Y/n]: y
836868
Dependencies loaded from local cache.
@@ -918,7 +950,8 @@ Generates `main.html` file with the Elm application.
918950

919951
- [An Introduction to Elm](https://guide.elm-lang.org)
920952
- [Elm Syntax](https://elm-lang.org/docs/syntax)
921-
- [Blazing Fast HTML](https://elm-lang.org/blog/blazing-fast-html-round-two)
953+
- [Korban book](https://korban.net/elm/book/)
922954
- [Small Assets without the Headache](https://elm-lang.org/blog/small-assets-without-the-headache)
923955
- [Elm in Production: Surprises & Pain Points](https://www.youtube.com/watch?v=LZj_1qVURL0)
956+
- [Elm-demo](https://github.com/deny1994/elm-demo)
924957

tutorials/images/tea.png

68.5 KB
Loading

0 commit comments

Comments
 (0)