如何在地图中使用Maybe(列表a)?

时间:2016-09-13 20:45:53

标签: elm

我有一种奇怪的情况,我无法弄明白。

type alias Schedule =
    { name : String
    , display : String
    , interval : Maybe Int
    , events : Maybe (List Event)
    }


type alias Event =
    { schedule : Maybe String
    , interval : Maybe Int
    , hook : String
    , args : List ( String, String )
    , timestamp : Int
    , seconds_due : Int
    }

setScheduledEventDueNow : Event -> Schedule -> Schedule
setScheduledEventDueNow event schedule =
    case schedule.events of
        Just events ->
            { schedule | events = List.map (setMatchedEventDueNow event) events }

        Nothing ->
            schedule

List中可能有Events Schedule events : Maybe (List Event),因此设置为Event

响应对已知-- TYPE MISMATCH ----------------------------------------- src/elm/CronPixie.elm The 1st and 2nd branches of this `case` produce different types of values. 373| case schedule.events of 374| Just events -> 375| { schedule | events = List.map (setMatchedEventDueNow event) events } 376| 377| Nothing -> 378|> schedule The 1st branch has this type: { a | events : List Event } But the 2nd is: { a | events : Maybe (List Event) } Hint: All branches in a `case` must have the same type. So no matter which one we take, we always get back the same type of value. Detected errors in 1 module. 的操作我想按计划运行事件(如果有),并可能返回更新的事件列表。

但是,我收到以下错误:

case

我的初学者大脑是因为我已经验证了确实存在List.map语句的事件列表,所以应该很好地映射它。

但是编译器认识到它是一个简单的case而没有一个Maybe的机会,所以很自然会抱怨cleartool find -avobs -type f -element '(created_since(1-Jun-2016) && !created_since(1-Sep-2016)) && (Element_Type==""Program"" || Element_Type==""Output"" || Element_Type==""Data"")' -ver 'created_since(1-Jun-2016) && !created_since(1-Sep-2016)' -exec 'cleartool describe -fmt ""#Name:%Xn Date:%Nd User:%u Label:%1.400Cl Attributes:%a Version:%Vn Comment:%Nc \n"" $CLEARCASE_XPN' >| test.txt 的两个分支返回的内容之间存在差异。

我知道如何解决这个问题吗?

1 个答案:

答案 0 :(得分:2)

您需要使用Just

包装列表
Just events ->
    { schedule | events = Just <| List.map (setMatchedEventDueNow event) events }

您可以使用Maybe.map更简洁地编写整个函数,如果事件列表已经Nothing,则返回Nothing,否则使用提供的函数映射到基础列表:

setScheduledEventDueNow : Event -> Schedule -> Schedule
setScheduledEventDueNow event schedule =
  { schedule | events = Maybe.map (List.map <| setMatchedEventDueNow event) schedule.events }