尤川豪   ·  1月前
410 貼文  ·  257 留言

Elm 待研究小問題

這兩個為何等價、Task 是什麼用途

Cmd.batch
    [ Task.succeed GenerateNewUuid |> Task.perform identity
    , Task.succeed GenerateNewUuid |> Task.perform identity
    , Task.succeed GenerateNewUuid |> Task.perform identity
    , Task.succeed GenerateNewUuid |> Task.perform identity
    ]
Cmd.batch
    [ send GenerateNewUuid
    , send GenerateNewUuid
    , send GenerateNewUuid
    , send GenerateNewUuid
    ]

send : msg -> Cmd msg
send msg =
    Task.perform identity (Task.succeed msg)

哪些是內建套件、會被視為內建的原則為何

import Random
import Random.Int

前者是內建的

後者需要 elm install elm-community/random-extra

設計哲學為何

Pattern Matching 可以針對 type 也可以針對 value 是嗎

update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    case msg of
        MoveReviewColumnUp index ->
            ( { model | reviewColumns = List.Extra.swapAt index (index - 1) model.reviewColumns }, Cmd.none )

        MoveReviewColumnDown index ->
            ( { model | reviewColumns = List.Extra.swapAt index (index + 1) model.reviewColumns }, Cmd.none )

        DeleteReviewColumn index ->
            ( { model | reviewColumns = List.Extra.removeAt index model.reviewColumns }, Cmd.none )
getInfoType : String -> InfoType
getInfoType value =
    case value of
        "text" ->
            Text

        "address" ->
            Address

        "city" ->
            City

The technique of using pattern matching to to implement logic in your code is a very standard technique in functional programming. In some sense it is a very natural way to have different code to handle different patterns. There are quite a few different things that you can use as patterns:

  • Values
  • Types
  • Guards/Expression
  • special patterns related to data structures (like [],(x::xs) for lists)

ref: https://csmith111.gitbooks.io/functional-reactive-programming-with-elm/content/section2/PatternMatching.html

Add inferred annotation 怎麼有時是 Html Msg 有時是 Html msg?

 moreInfo column =
 reviewColumnsView model =

The first one doesn’t produce any events, so its type variable is not bound to anything yet. The second one produces events of your Msg type, and its type variable is bound to that.

ref: https://discourse.elm-lang.org/t/html-msg-vs-html-msg/2758

google: Html msg vs Html Msg elm

  分享   共 72 次點閱
共有 0 則留言
還沒有人留言。歡迎分享您的觀點、或是疑問。
您的留言
尤川豪
410 貼文  ·  257 留言

Devs.tw 是讓工程師寫筆記、網誌的平台。隨手紀錄、寫作,方便日後搜尋!

歡迎您一起加入寫作與分享的行列!

查看所有文章