ãã®è¨äºã¯Elm Advent Calendar 2016 ã®4æ¥ç®ã§ãã
ä¼ç¤¾ã§æ¸ããã¦ããã£ã¦ãElm製ã¢ããªã10000è¡ãè¶ ããã®ã§ãç¾æç¹ã§å人çã«ããã¯ã¨æããã¹ããã©ã¯ãã£ã¹ãå®éã®ã½ã¼ã¹ä»ãã§æ¸ãã¦ã¿ãã
ï¼ã¢ããªã«ã¤ãã¦ã®æ å ±ã¯æ©ä¼ãããã°ï¼
1.å¿ ãã¹ã¿ã¤ã«ã¬ã¤ãã«å¾ã
è¡æ°ãå¢ããå¾åã«ããããããªãèªã¿ããããªãã®ã§çµ¶å¯¾ã«å¾ã£ãæ¹ãè¯ãã
é¢é£ã³ããã ï¼let...in
ä¸ã«ç©ºè¡ãæ¿å
¥ãã¦ããï¼
2.ãã¼ã¿æ§é ã«ã¿ãã«ã使ããªã
ä¾ãã°ãã¦ã¹ã®ä½ç½®ãªã©ãã¿ãã«ã§(Int, Int)
ã®ããã«æ¸ããããªãããããå¾æããã®ã§ãããæ¹ãè¯ãã
-- å¾®å¦ calculateX : Model -> Int calculateX model = let (x, y) = model.position in max 0 x
-- å¾®å¦ calculateX : Model -> Int calculateX model = max 0 (Tuple.first model.position)
ã¿ãã«ããå¤ãåãåºãã«ã¯let...in
ä¸ã§å±éããããTuple.first
ãªã©ã使ããããªããåç´ã«æ±ããªãããfirst
ã¯å¤ã®æå³ã表ãã¦ããªãã代ããã«type alias Position = { x : Int, y : Int }
ã使ãã
-- è¯ã calculateX : Model -> Int calculateX model = max 0 model.position.x
ãã®ããã«ãæããã«ãã£ãããããä»ã«ãé åã表ãã®ã«(Int, Int, Int, Int)
ãªã©ã使ã£ã¦ããã¨ã(left, top, right, bottom)
ãªã®ã(left, top, width, height)
ãªã®ãåãããªãã¦ã¨ã¦ãæ··ä¹±ããã代ããã«ãå
ã»ã©ã®Position
ãtype alias Size = { width : Int, height : Int }
ã使ãã¨åããããããªãã
ã¿ãã«ã¯æåéããè¤æ°ã®çµæããçµãã«ãã¦è¿ãããç¨éã§ä¸æçã«ä½¿ãã®ãè¯ãããã¼ã¿æ§é ã表ãã®ã«ä½¿ãã¨å¤±æããã
- é¢é£ã³ããã ï¼ããã¨ããããå ´æã«ãã£ã
(Int, Int)
ã®å¤§é¨åãPosition
ã«ç½®ãæããï¼
3.åã®å¥åï¼type aliasï¼ã使ã£ã¦å¯èªæ§ãä¸ãã
å注éãStringã ããã«ãªãã¨ä½ã表ãã¦ããã®ãåãããªã£ã¦æ··ä¹±ããã
-- å¾®å¦ setPerson : String -> String -> Model -> Model
ããã次ã®ããã«ããã¨ãä½ããã¦ããã®ãåããããã«ãªãã
-- è¯ã type alias ObjectId = String type alias PersonId = String setPerson : ObjectId -> PersonId -> Model -> Model
ããã«åããã¦å¤æ°åããã ã®id
ããxxxId
ã«å¤æ´ããã¨ãããã¨ã¦ãèªã¿ããããªã£ãã
- é¢é£ã³ããã ï¼
String
åãPersonId
ãFloorId
ãªã©ã«ç½®ãæããï¼
4.importæã«exposingããã®ã¯åã®ã¿ã«ãã
次ã®ããã«importããã¨é¢æ°ãã©ã®ã¢ã¸ã¥ã¼ã«ç±æ¥ã ãããã«åãããªããªãã
-- æªã import Foo.Bar exposing (..)
exposingããã®ã¯åã®ã¿ã«ãã¦ãé¢æ°ã¯ã¢ã¸ã¥ã¼ã«åããæ¸ããæ¹ãè¯ãã
-- è¯ã import Foo.Bar as Bar exposing (Bar)
- é¢é£ã³ããã ï¼
exposing (..)
ãããã¦ã¢ã¸ã¥ã¼ã«åããé¢æ°ãæ¸ãããã«ããï¼
5.ä¸ã¤ã®ã¢ã¸ã¥ã¼ã«ãã沢山ã®åãå ¬éããªã
ãããåãã§ãåãã©ã®ã¢ã¸ã¥ã¼ã«ç±æ¥ã ãããã«åãããªããªããJavaã»ã©ã¬ãã¬ãã«ããå¿ è¦ã¯ãªãããåºæ¬çã«ã¢ã¸ã¥ã¼ã«åã¨åãååã®åãã²ã¨ã¤ã ãå ¬éããã®ãè¯ãã
-- å¾®å¦ import Foo.Bar exposing (Bar, Baz, Pen, Pineapple, Apple)
- é¢é£ã³ããã ï¼
ObjectsOperation
ã¢ã¸ã¥ã¼ã«ã«ãã£ãDirection
åãDirection
ã¢ã¸ã¥ã¼ã«ã«åé¢ããï¼
6.ãã¤ãã使ã
Maybe
ãList
ã使ã£ã¦ããã¨ãã©ãã©ãcase...of
ãå¢ãã¦èªã¿ã¥ãããªãã
-- å¾®å¦ message = case List.head (List.reverse (getPeople model)) of Just person -> person.name ++ "was the first person." Nothing -> "No one found."
ããã§ããã¤ãã使ãããã¡ã½ãããã§ã¼ã³ã®ãããªãã®ã ã
-- è¯ã message = getPeople model |> List.reverse |> List.head |> Maybe.map (\person -> person.name ++ "was the first person.") |> Maybe.withDefault "No one found."
è¦ç·ããã£ã¡ã«ãã£ãããã£ã¡ã«è¡ã£ããããªãã¦è¯ããªãããã ãæ £ããã¾ã§ã«ã¯å°ãæéãããããæ©ãã«æ £ãããã
- é¢é£ã³ããã ï¼Maybeåã®å¦çããã¤ãã§æ¸ãç´ããï¼
7.CmdãããTaskãã¼ã¹ã®APIãæä¾ãã
Cmdã¯Taskããä¸æ®µæ½è±¡åº¦ãé«ããé£éããããã¨ã許å¯ãã¦ããªãã
Taskã§ããã°ã次ã®ããã«è²ã é£éããããªã¯ã¨ã¹ããæãããã¨ãåºæ¥ãã
getUser config |> Task.andThen(\user -> getOrganization config user.orgId |> Task.map(\org -> { user = user, org = org })) |> Task.onError(\err -> Task.succeed (Error err)) |> Task.perform GotInfo
- é¢é£ã¢ã¸ã¥ã¼ã« ï¼APIã¢ã¸ã¥ã¼ã«ã¯ãµã¼ãã¨ã®éä¿¡ããã¹ã¦
Task Http.Error a
åã§å®ç¾©ãã¦ããï¼
8.Subscriptionãã±ãã
Virtual DOMã¨ããå¯è±ªçãªä»çµã¿ãæ¡ç¨ãã¦ãã以ä¸ãåºæ¥ããã¨ãªãMouse.moves
ã®ãããªé »ç¹ã«èµ·ããã¤ãã³ãã¯é¿ãããã
subscriptions
ã®åã¯Model -> Sub msg
ã¨ãªã£ã¦ããã®ã§ãã¢ãã«ã®ç¶æ
ã«å¿ãã¦èå£ãç·©ããããã«ããã°ããã次ã®ä¾ã¯å
¬å¼ã®ãã©ãã°ï¼ããããã®ä¾ããã
subscriptions : Model -> Sub Msg subscriptions model = case model.drag of Nothing -> Sub.none Just _ -> Sub.batch [ Mouse.moves DragAt, Mouse.ups DragEnd ]
- é¢é£ã³ããã ï¼åçç¡ç¨ã§åãæµãã¦ãã
Mouse.moves
ããã©ãã°ä¸ã«éå®ããï¼
9.ãã¼ã¿æ§é ãçå£ã«èãã
æ£ãããã¼ã¿æ§é ï¼åï¼ãå®ç¾©ãããã¨ã§ãå
ç¢æ§ãä¸ãããã¨ãåºæ¥ããåç´ãªä¾ã ã¨ãå¿
ãï¼ã¤ä»¥ä¸ã®å¤ãæã¤ãªã¹ãããList a
åã§è¡¨ç¾ãããã¨ãã§ããããããã ã¨ä½ãã®æåã«ç©ºã®ãªã¹ãã«ãªãå¯è½æ§ããããããã§æ¬¡ã®ãããªãã¼ã¿ãä½ãã
type alias NonEmptyList a = { head : a , tail : List a }
ããã«ãã£ã¦ãå¿
ãhead
ï¼æåã®è¦ç´ ï¼ãåå¨ãããã¨ã¯ä¿è¨¼ã§ããããä½ããã®æä½ãããæã«ãªã¹ãã®é·ããï¼ã§ãã£ã¦ã¯ãªããªããã¨ãããã¹ããä¸è¦ã«ãªãã
ãã£ã¨å ·ä½çã»ç¾å®çãªä¾ã¯ãRichard Feldmanæ°ã®ãã¼ã¯ãMaking Impossible State Impossibleï¼ä¸å¯è½ãªç¶æ ãä¸å¯è½ã«ããï¼ãã§æ²¢å±±ç´¹ä»ããã¦ããã®ã§ããã²åç §ãã¦ã»ããã
- é¢é£ã³ããã ï¼ã¬ã³ã¼ããã¦ããªã³ã«ç´ãã¦ããããªããã¿ã¼ã³ãæ²æ» ããï¼
- é¢é£ã³ããã ï¼ã¦ããªã³ãã¬ã³ã¼ãã«ç´ãã¦ãããããã¿ã¼ã³ã復å ããï¼
10.ã«ãã»ã«åãã
å
ç¢æ§ã«è²¢ç®ããè¦ç´ ã¨ãã¦ããã¼ã¿æ§é ã®æ£ããã¨åããããéè¦ãªã®ãã«ãã»ã«åã ï¼ãããä¸ã®ãã¼ã¯ã§è§¦ãããã¦ããï¼ã
ä¾ãã°ãå
ã»ã©ã®ä¾ã§ã¯è¦ç´ ã空ã«ãªããªããã¨ã¯ä¿è¨¼ã§ããããæ°ããè¦ç´ ãééãã¦tail
ã«çªã£è¾¼ãã§ãã¾ã£ããå
¨ã¦å°ç¡ãã«ãªã£ã¦ãã¾ãã
ãããªããªãããã«ãElmã§ã¯ãã¼ã¿ã¸ã®æä½ãã¢ã¸ã¥ã¼ã«å ã®é¢æ°ã«éå®ãããã¯ããã¯ãããã
-- è¯ã type NonEmptyList a = NonEmptyList { head : a , tail : List a } add : a -> NonEmptyList a -> NonEmptyList a add a (NonEmptyList list) = NonEmptyList { head = a , tail = list.head :: list.tail }
æ°ãããã¼ã¸ã§ã³ã®NonEmptyList
ã¯ãã¢ã¸ã¥ã¼ã«å¤ã§ç´æ¥head
ãtail
ã触ããã¨ãç¦æ¢ãã¦ããã代ããã«ãå
¬éAPIã¨ãã¦add
é¢æ°ãæä¾ããã
ãããããã¨ã§ããæ°ããè¦ç´ ãheadã«è¿½å ããããã¾ã§headã«ãã£ããã®ãtailã®å
é ã«ç§»åããããã¨ããä¸é£ã®æä½ãå®å
¨ã§ãããã¨ãä¿è¨¼ã§ããã
詳細ã¯ãã¡ãã®è¨äºã«ãæ¸ãã¦ããã
- é¢é£ã¢ã¸ã¥ã¼ã« ï¼
Mode
åã®ãã¼ã¿ã¯ãã¹ã¦APIçµç±ã§ããã¢ã¯ã»ã¹ã§ããªãï¼
11.ã³ã³ãã¼ãã³ãã«ãã¹ãããçå£ã«èãã
Elmã«ããã¦ã³ã³ãã¼ãã³ãã¨ã¯ãModel / Update / View ãä¸ç·ã«æä¾ãããã¢ã¸ã¥ã¼ã«ãæãã
ãããã失æãã¿ã¼ã³ã¯ãåå©ç¨å¯è½ã§ççµåãªã³ã³ãã¼ãã³ããä½ããã¨ãã¦æ¬æ¥ã²ã¨ã¤ã§ããã¢ããªã±ã¼ã·ã§ã³ãã¸ãã¯ãåæ¥ãã¦ãã¾ãäºã ããæ¤ç´¢æ¬ã¯ãã£ã¡ãæä¾ããããã¿ã³ãæ¼ããããå ·ä½çãªæ¤ç´¢ãã¸ãã¯ã¯ãã£ã¡ã§ãã£ã¦ãããçµæãã³ã¼ã«ããã¯ããããã£ã¡ã§ãã£ãã·ã¥ãã¦ãããããã£ãã·ã¥ãã¯ãªã¢ãããã¨ãã¯å½ä»¤ãã¦ãããçµæã®è¡¨ç¤ºã¯ãã£ã¡ã§ãããããã£ã¨ã·ã³ãã«ã«ãå ¥åæ¬ã»æ¤ç´¢ãã¸ãã¯ã»çµæ表示ã®ãã¥ã¼ã«åããã°è¯ãã¦ãã³ã³ãã¼ãã³ãã§ããå¿ è¦ããªãã
ç¶ãã¦ãããã失æãã¿ã¼ã³ã¯ãç»é¢ã縦横ã«åå²ãã¦ãã£ã¦ããã®ã¾ã¾ã³ã³ãã¼ãã³ãã«è¦ç«ã¦ã¦ãã¾ããã¨ã ãä¾ãã°ã大æµã©ããªãµã¤ãã«ããããããããã ãããããããã³ã³ãã¼ãã³ããä½ããããªããã¨ãããããããã«ã¯è²ããªæ©è½ï¼ãã°ã¢ã¦ããè¨å®ãªã©ï¼ãããããããããã®ãã¸ãã¯ãã©ãã©ãå¸ãè¾¼ãã§ãã¾ãããã¼ã¸ã«ãã£ã¦å 容ã®éä¸è¶³ãåºã¦ããããªãã¨ã«ãªãããããã«æ±ããããã®ã¯ãã¡ãã¥ã¼ã渡ãããããã横ã«ä¸¦ã¹ããæ©è½ã§ããããèªä½ããããã®æ©è½ãæã£ã¦ããå¿ è¦ã¯ãªãã
ãã¹ã¦ã¯ã³ã³ãã¼ãã³ããã§ã¯ãªãããä¸è¦ãªè¤éããæã¡è¾¼ãåã«ãæ®éã®ãã¥ã¼ããé§ç®ãªã®ãããã¾ãæ¤è¨ãããã
- é¢é£ã³ããã ï¼ãé£ã«ãããã¨ããçç±ã ãã§ãã©ã¼ã ã¨ä¸ä½ã«ãªã£ã¦ãããã¡ã¤ã«èªã¿è¾¼ã¿ç¨ã®ã¢ã¸ã¥ã¼ã«ãåãåºããï¼