项目作者: kasparsbergs

项目描述 :
revisiting elm
高级语言:
项目地址: git://github.com/kasparsbergs/elm-practice.git
创建时间: 2019-04-12T23:45:45Z
项目社区:https://github.com/kasparsbergs/elm-practice

开源协议:MIT License

下载


Elm practice

Revisiting elm…

Elm talk video

video

Long story short…

Uncaught TypeError: Cannot read property

TypeError: ‘undefined’ is not an object (evaluating

TypeError: null is not an object

(unknown): Script error

TypeError: Object doesn’t support property

TypeError: ‘undefined’ is not a function

Uncaught RangeError

TypeError: Cannot read property ‘length’

Uncaught TypeError: Cannot set property

ReferenceError: event is not defined


OK now..

Introduction

Elm is a domain-specific programming language for declaratively creating web browser-based graphical user interfaces. Elm is purely functional, and is developed with emphasis on usability, performance, and robustness. It advertises “no runtime exceptions in practice”, made possible by the Elm compiler’s static type checking.


key points taken fron the internet

  • They say it just won’t catch fire like the javascript forest does.

  • They say it will get you fired because it allows only bug free code and the application will never have a runtime exception.

How to start with Elm?

Installing elm

just as easy as everything else if you used npm before: npm install -g elm

Configure Your Editor

ext install sbrink.elm

Then, in .vscode/settings.json, add the following:

  1. "elm.compiler": "./node_modules/.bin/elm",
  2. "elm.makeCommand": "./node_modules/.bin/elm-make"

typing elm in the terminal..

lists useful commands for elm development..

  1. $ elm
  2. Hi, thank you for trying out Elm 0.19.0. I hope you like it!
  3. -------------------------------------------------------------------------------
  4. I highly recommend working through <https://guide.elm-lang.org> to get started.
  5. It teaches many important concepts, including how to use `elm` in the terminal.
  6. -------------------------------------------------------------------------------
  7. The most common commands are:
  8. elm repl
  9. Open up an interactive programming session. Type in Elm expressions like
  10. (2 + 2) or (String.length "test") and see if they equal four!
  11. elm init
  12. Start an Elm project. It creates a starter elm.json file and provides a
  13. link explaining what to do from there.
  14. elm reactor
  15. Compile code with a click. It opens a file viewer in your browser, and
  16. when you click on an Elm file, it compiles and you see the result.
  17. There are a bunch of other commands as well though. Here is a full list:
  18. elm repl --help
  19. elm init --help
  20. elm reactor --help
  21. elm make --help
  22. elm install --help
  23. elm bump --help
  24. elm diff --help
  25. elm publish --help
  26. Adding the --help flag gives a bunch of additional details about each one.
  27. Be sure to ask on the Elm slack if you run into trouble! Folks are friendly and
  28. happy to help out. They hang out there because it is fun, so be kind to get the
  29. best results!

writing code in the Elm repl

in terminal type elm repl

  1. ---- Elm 0.19.0 ----------------------------------------------------------------
  2. Read <https://elm-lang.org/0.19.0/repl> to learn more: exit, help, imports, etc.
  3. --------------------------------------------------------------------------------
  4. >

Information on elm version and a link to the appears, all very helpful so far so good!

let’s try to write something..

Numbers

by simply adding two numbers > 1+1
we get 2 : number with a type inference, amazing!

typing 1.5 gives us 1.5 : Float

and > 1.5 +0.5 gives us 2 : Float

so the number 2 may look like an int but in elm there is no implicit type casting, meaning elm will never do implicit casts.
you are welcome to read more on Elm Implicit Casts in the official documentation.

Strings

All strings are in double quotes "a"

  1. > "a"
  2. "a" : String

Char

char can be represented with single quotes ‘a’

  1. > 'a'
  2. 'a' : Char
  3. >
some errors trying to append char to a string
  1. 'a' ++"bb"
  2. -- TYPE MISMATCH ----------------------------------------------------------- elm
  3. The (++) operator cannot append this type of value:
  4. 5| 'a' ++"bb"
  5. ^^^
  6. I am seeing a character of type:
  7. Char
  8. But the (++) operator is only for appending List and String values. Maybe put
  9. this value in [] to make it a list?
  10. Hint: Only strings and lists are appendable.
concatenating strings can be done with ++ operator
  1. > "a"++"pple"
  2. "apple" : String
Useful error explanations from the compiler..
  1. > "a"+"pple"
  2. -- TYPE MISMATCH ----------------------------------------------------------- elm
  3. I cannot do addition with String values like this one:
  4. 4| "a"+"pple"
  5. ^^^
  6. The (+) operator only works with Int and Float values.
  7. Hint: Switch to the (++) operator to append strings!

everything in elm is made with functions…

defining a function..

just write a name for it add followed by arguments a b and after the = sign what it will do a + b it is so simple.

  1. > add a b = a+b
  2. <function> : number -> number -> number
  3. > add 12 8
  4. 20 : number