Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Elm 入門

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on. →

Elm 入門

Avatar for Knovour

Knovour

May 08, 2017

More Decks by Knovour

Other Decks in Programming

Transcript

  1. Elm Platform # Mac Homebrew brew cask install elm-platform #

    Or by npm sudo npm i elm -g  FMNSFQM❜✽䒭➝꬗  FMNNBLF管陼䧭+4  FMNSFBDUPSEFWXFCTFSWFS  FMNQBDLBHF⺫盘椚㐼FMNTUVFMNQBDLBHFKTPO
  2. -- Single line comment {- Multiline comment -} True |

    False -- Bool 42 -- Int 3.14 -- Float 'a' -- Char "abc" -- String "a" ++ "b" -- Multiline String """ Line 1 Line 2 """ 㛇劥㘗䢀
  3. -- List Int [ 1, 2, 3, 4 ] 1

    :: 2 :: [ 3, 4 ] [ 1, 2 ] ++ [ 3, 4 ] -- List String [ "One" , "Two" , "Three" ] List
  4. div [ class "block" ] [ text "Hello Elm" ]

    div [ class "users" ] [ button [ class "get-random" ] [ text "Get random user"] , div [ class "user-details" ] [ h1 [ class "user-name" ] [ text "Username" ] , h4 [ class "user-email" ] [ text "Email" ] , a [ class "user-website" , href "link" ] [ text "Website" ] ] ] "UUSJCVUFT $POUFOUT DOM
  5. user = { name = "Username" , email = "[email protected]"

    } user.name { name } = user { user | name = "New Name" } -- Update Records
  6. List.head [ 1, 2, 3, 4 ] == Just 1

    List.head [] == Nothing Maybe.withDefault 0 (Just 1) == 1 Maybe.withDefault 0 Nothing == 0 Maybe &MN尝剤OVMM
  7. n = 2 if n /= 2 then "Not equal"

    else if n == 2 then "Equal" else "Do Nothing" 䗳갭㹒デ If … else
  8. n = 2 case n of 1 -> "case n

    == 1:" 2 -> "case n == 2:" _ -> “default:" &MN剚䓽ⵖ銴⡹⴪ⴀ䨾剤〳腋䚍⡎♶剚嫦妄鿪僽@ Case … of
  9. add : Int -> Int -> Int add arg1 arg2

    = arg1 + arg2 add 1 2 -- add(1)(2) 1 + 2 (+) 1 2 㔐⫄㘗䢀 $VSSJFE'VODUJPO ⿮侸㘗䢀 䲾鶤ꬌ䓽ⵖ 穡圓 㻜⡲
  10. ⼣そⴅ䒭 (\x y -> x + y) 1 2 --

    3 List.map (\x -> x + 1) [ 1, 2, 3 ] -- [ 2, 3, 4 ] List.map (\user -> user.name ) [{ name = "Someone" }] -- [ "Someone" ] List.map .name [{ name = "Someone" }]
  11. type alias Str = String type alias Numbers = List

    Int -- Record is not a type type alias User = { name : String , email : String } Type Alias
  12. type alias User = { name : String , email

    : String } user = { name = "Username" , email = "[email protected]" } getUserName : ? -> String getUserName : { name : String, email : String } -> String getUserName : { a | name : String } -> String getUserName : User -> String getUserName user = user.name getUserName { name } = name BOZUIJOH
  13. Pipe Operator num = 2 add 4 (multiply 2 num)

    -- 8 num |> multiply 2 |> add 4 add 4 <| multiply 2 <| num num * 2 + 4 num |> (*) 2 |> (+) 4
  14. module Main exposing (..) import Calculator exposing (..) Calculator.addOne Calculator.minusOne

    1 addOne 1 minusOne 1 import Calculator exposing (addOne) Calculator.minusOne 1 -- pass minusOne 1 -- error Calculator.elm module Calculator exposing (..) addOne args = args + 1 minusOne args = args - 1 Main.elm 嫦⦐墂呪Ꟛ걧䗳꨾㹒デNPEVMF ➿邍鿈ⴅ䒭〳䭸㹁そ珖 FYQPTJOH剤㹒デ儘〳湬䱺⢪欽 〫腋湬䱺ㄎ〭BEE0OF
  15. module Main exposing (..) import Toolbox.Calculator as Calculator Toolbox.Calculator.addOne Calculator.addOne

    module Toolbox.Calculator exposing (..) Toolbox/Calculator.elm Main.elm ♶⢪欽BT
  16. Union Type type Door = Open | Close type Movie

    = Add String | Remove String Add "Ghost in the Shell" Remove "Twilight" 䖕꬗〳䱺⿮侸⺫䭍Ⱖ➮6OJPO5ZQF
  17. IUUQTFMMJFBQQDPN:8Q#9EUT/LB 眕⢿⸈幾㐼 type alias Model = Int model : Model

    model = 0 main = Html.beginnerProgram { model = model , view = view , update = update } view : Model -> Html Msg view model = div [] [ button [ onClick Increment ] [ text "+" ] , span [] [ text (toString model) ] , button [ onClick Decrement ] [ text "-" ] ] type Msg = Increment | Decrement update : Msg -> Model -> Model update msg model = case msg of Increment -> model + 1 Decrement -> model - 1 玑䒭鹎Ⰵ럊 蕰♶⺫わ✲⟝⵱欽)UNMNTH
  18. Html Program Html.beginnerProgram { model = model , view =

    view , update = update } -- OR Html.program { init = (model, Cmd.none) , view = view , update = update , subscriptions = subscriptions } subscriptions : Model -> Sub Msg subscriptions model = Sub.none $NE涮鷑"+"9《䖤植㖈徿롗⡙縨禺窡儘꟦瘞 4VC䭰糵鎹ꏗ徿롗⡙縨禺窡儘꟦瘞✲⟝