You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -239,11 +239,11 @@ It generates `elm.json` file that defines where the source files are and what ar
239
239
"source-directories": [
240
240
"src"
241
241
],
242
-
"elm-version": "0.19.0",
242
+
"elm-version": "0.19.1",
243
243
"dependencies": {
244
244
"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",
247
247
"elm/html": "1.0.0"
248
248
},
249
249
"indirect": {
@@ -352,8 +352,8 @@ linear a b x =
352
352
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.
353
353
354
354
```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
357
357
358
358
>List.length numbers
359
359
5:Int
@@ -364,14 +364,14 @@ False : Bool
364
364
> double n = n *2
365
365
<function>: number -> number
366
366
>List.map double numbers
367
-
[2,6,10,14,22]:List number
367
+
[2,6,10,14,22]:List number
368
368
369
369
>List.map (\n -> n *2) numbers
370
-
[2,6,10,14,22]:List number
370
+
[2,6,10,14,22]:List number
371
371
372
372
373
373
>List.map ((*)2) numbers
374
-
[2,6,10,14,22]:List number
374
+
[2,6,10,14,22]:List number
375
375
```
376
376
377
377
@@ -380,14 +380,14 @@ False : Bool
380
380
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.
381
381
382
382
```elm
383
-
>Dict.fromList [("Spencer",25),("Zoe",21)]
384
-
Dict.fromList [("Spencer",25),("Zoe",21)]:Dict.DictString number
383
+
>Dict.fromList [("Spencer",25),("Zoe",21)]
384
+
Dict.fromList [("Spencer",25),("Zoe",21)]:Dict.DictString number
385
385
386
386
>Dict.insert "Spencer"25Dict.empty
387
-
Dict.fromList [("Spencer",25)]:Dict.DictString number
387
+
Dict.fromList [("Spencer",25)]:Dict.DictString number
388
388
389
-
> dict =Dict.fromList [("Spencer",25),("Zoe",21)]
390
-
Dict.fromList [("Spencer",25),("Zoe",21)]:Dict.DictString number
389
+
> dict =Dict.fromList [("Spencer",25),("Zoe",21)]
390
+
Dict.fromList [("Spencer",25),("Zoe",21)]:Dict.DictString number
391
391
>Dict.isEmpty dict
392
392
False:Bool
393
393
>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
402
402
403
403
404
404
```elm
405
-
> person =("Joe",21)
406
-
("Joe",21):(String, number )
405
+
> person =("Joe",21)
406
+
("Joe",21):(String, number )
407
407
408
408
>Tuple.first person
409
409
"Joe":String
@@ -415,25 +415,25 @@ A tuple is a collection of items of various type with the fixed size. There is a
415
415
We can use pattern matching for tuples in functions:
416
416
417
417
```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."
420
420
```
421
421
422
422
423
423
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.
424
424
425
425
```
426
-
> vector4 = (4, 10, 12, 3)
426
+
> vector4 = (4, 10, 12, 3)
427
427
-- BAD TUPLE --------------------------------------------------------------- elm
428
428
429
429
I only accept tuples with two or three items. This has too many:
430
430
431
-
8| vector4 = (4, 10, 12, 3)
432
-
^^^^^^^^^^^^^^
431
+
8| vector4 = (4, 10, 12, 3)
432
+
^^^^^^^^^^^^^^^^
433
433
I recommend switching to records. Each item will be named, and you can use the
434
434
`point.x` syntax to access them.
435
435
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
437
437
working with large chunks of data in Elm.
438
438
```
439
439
@@ -446,6 +446,10 @@ Records contain keys and values. Each value can have a different type.
446
446
> vector4 ={ w =4, x =10, y =12, z =3}
447
447
{ w =4, x =10, y =12, z =3}
448
448
:{ 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}
449
453
```
450
454
451
455
@@ -458,8 +462,8 @@ For accessing record properties, Elm has by default accessors defined as `.<key>
458
462
>.x vector4
459
463
10: number
460
464
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
463
467
```
464
468
465
469
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
479
483
```
480
484
481
485
482
-
We can use pattern matching for record keys:
486
+
We can use pattern matching (desctructuring) for record keys:
483
487
484
488
```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)
486
490
<function>:{ b | w :Float, x :Float, y :Float, z : a }->Float
487
491
> length vector4
488
492
16.61324772583615:Float
@@ -511,6 +515,16 @@ type alias Person =
511
515
isAdult:Person->Bool
512
516
isAdult { age }=
513
517
age >=18
518
+
519
+
520
+
getName:Person->Name
521
+
getName { name }=
522
+
name
523
+
524
+
525
+
getName2:Person->String
526
+
getName2 { name }=
527
+
name
514
528
```
515
529
516
530
```elm
@@ -536,9 +550,9 @@ We can define custom types that have several variants. We can also associate dat
536
550
537
551
538
552
```elm
539
-
type Gender
540
-
=Male
541
-
|Female
553
+
type Animal
554
+
=Cat
555
+
|Dog
542
556
543
557
544
558
type Tree a
@@ -553,24 +567,27 @@ type Profile
553
567
554
568
555
569
556
-
gender=Female
570
+
animal=Dog
557
571
558
572
tree =Branch(Leaf1)(Branch(Leaf2)(Leaf0))
559
573
560
574
profile =Error"Cannot load profile"
561
575
562
576
```
563
577
578
+
*Note*: There are two more complex techniques, how to design data structure - opaque types and phantom types.
579
+
580
+
564
581
### Pattern Matching
565
582
566
583
```elm
567
-
isFemale:Gender->Bool
568
-
isFemale gender=
569
-
case genderof
570
-
Male->
584
+
isDog:Animal->Bool
585
+
isDog animal=
586
+
case animalof
587
+
Cat->
571
588
False
572
589
573
-
Female->
590
+
Dog->
574
591
True
575
592
```
576
593
@@ -587,6 +604,24 @@ isLoading profile =
587
604
False
588
605
589
606
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
+
590
625
profileStatus:Profile->String
591
626
profileStatus profile =
592
627
case profile of
@@ -817,10 +852,7 @@ number: 4
817
852
818
853
## Packages
819
854
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.
824
856
825
857
826
858
To install a package, we use `elm install` command in the project directory.
@@ -830,7 +862,7 @@ $ elm install elm-community/maybe-extra
830
862
Here is my plan:
831
863
832
864
Add:
833
-
elm-community/maybe-extra 5.0.0
865
+
elm-community/maybe-extra 5.2.0
834
866
835
867
Would you like me to update your elm.json accordingly? [Y/n]: y
836
868
Dependencies loaded from local cache.
@@ -918,7 +950,8 @@ Generates `main.html` file with the Elm application.
918
950
919
951
-[An Introduction to Elm](https://guide.elm-lang.org)
920
952
-[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/)
922
954
-[Small Assets without the Headache](https://elm-lang.org/blog/small-assets-without-the-headache)
923
955
-[Elm in Production: Surprises & Pain Points](https://www.youtube.com/watch?v=LZj_1qVURL0)
0 commit comments