有没有更简洁的方法来解开 Elm 中的可能值
Is there a less verbose way to unwrap maybe values in Elm
我已经 运行 遇到了 elm 中的一个常见问题,我有一个函数依赖于多个可能值是 Just。有没有更简洁的方式来编写这段代码:
commandIf apples bananas oranges =
case apples of
Just apples_ ->
case bananas of
Just bananas_ ->
case oranges of
Just oranges_ ->
someCommand apples_ bananas_ oranges_
Nothing ->
Cmd.none
Nothing ->
Cmd.none
Nothing ->
Cmd.none
如果您同时需要所有三个值,您可以将它们作为一个元组匹配在一起,并将所有其他组合(当其中一个或多个是 Nothing
时)留给后备案例:
commandIf apples bananas oranges =
case (apples, bananas, oranges) of
(Just apples_, Just bananas_, Just oranges_) ->
someCommand apples_ bananas_ oranges_
_ ->
Cmd.none
@laughedelic 的回答很好。只是想提供一些替代和更通用的解决方案,因为冗长的 Maybe
解包是我在开始使用 Elm 时也 运行 遇到的一个问题。
如果你有固定数量的Maybe
值,你可以使用map2, map3
等来做你想做的事(docs here) :
commandIf apples bananas oranges =
Maybe.map3 someCommand apples bananas oranges
|> Maybe.withDefault Cmd.none
在这里,someCommand
是你的函数,它接受 3 个参数,returns 是一些命令。
Maybe.map3
仅当所有 3 个变量都是 Just x
时才应用此函数,并将其包装在一个 Maybe
类型中。因此,如果所有 3 个都有值,则结果为 Just (someCommand apples bananas oranges)
。否则,函数 returns Nothing
.
这个结果然后"piped"变成Maybe.withDefault
。其中 returns 一个 Cmd.none
如果输入是 Nothing
,否则 returns 值(您的命令),没有 Just
.
如果您有一个长度未知的 Maybe
值 列表,您可以这样做:
keepOnlyJusts : List (Maybe a) -> List a
keepOnlyJusts listOfMaybes =
listOfMaybes
|> List.filterMap identity
newList = keepOnlyJusts [ Just 1, Nothing, Just 3 ] -- == [1,3]
结果是一个列表(可以是空的),其中只保留值。
Maybe.map3
解决了你的特殊情况,但这个答案是关于 链接的一般模式 可能使用 Maybe.andThen
的值。
commandIf a_ b_ c_ =
a_ |> Maybe.andThen (\a ->
b_ |> Maybe.andThen (\b ->
c_ |> Maybe.andThen (Just << someCommand a b)))
|> Maybe.withDefault Cmd.none
我已经 运行 遇到了 elm 中的一个常见问题,我有一个函数依赖于多个可能值是 Just。有没有更简洁的方式来编写这段代码:
commandIf apples bananas oranges =
case apples of
Just apples_ ->
case bananas of
Just bananas_ ->
case oranges of
Just oranges_ ->
someCommand apples_ bananas_ oranges_
Nothing ->
Cmd.none
Nothing ->
Cmd.none
Nothing ->
Cmd.none
如果您同时需要所有三个值,您可以将它们作为一个元组匹配在一起,并将所有其他组合(当其中一个或多个是 Nothing
时)留给后备案例:
commandIf apples bananas oranges =
case (apples, bananas, oranges) of
(Just apples_, Just bananas_, Just oranges_) ->
someCommand apples_ bananas_ oranges_
_ ->
Cmd.none
@laughedelic 的回答很好。只是想提供一些替代和更通用的解决方案,因为冗长的 Maybe
解包是我在开始使用 Elm 时也 运行 遇到的一个问题。
如果你有固定数量的Maybe
值,你可以使用map2, map3
等来做你想做的事(docs here) :
commandIf apples bananas oranges =
Maybe.map3 someCommand apples bananas oranges
|> Maybe.withDefault Cmd.none
在这里,someCommand
是你的函数,它接受 3 个参数,returns 是一些命令。
Maybe.map3
仅当所有 3 个变量都是 Just x
时才应用此函数,并将其包装在一个 Maybe
类型中。因此,如果所有 3 个都有值,则结果为 Just (someCommand apples bananas oranges)
。否则,函数 returns Nothing
.
这个结果然后"piped"变成Maybe.withDefault
。其中 returns 一个 Cmd.none
如果输入是 Nothing
,否则 returns 值(您的命令),没有 Just
.
如果您有一个长度未知的 Maybe
值 列表,您可以这样做:
keepOnlyJusts : List (Maybe a) -> List a
keepOnlyJusts listOfMaybes =
listOfMaybes
|> List.filterMap identity
newList = keepOnlyJusts [ Just 1, Nothing, Just 3 ] -- == [1,3]
结果是一个列表(可以是空的),其中只保留值。
Maybe.map3
解决了你的特殊情况,但这个答案是关于 链接的一般模式 可能使用 Maybe.andThen
的值。
commandIf a_ b_ c_ =
a_ |> Maybe.andThen (\a ->
b_ |> Maybe.andThen (\b ->
c_ |> Maybe.andThen (Just << someCommand a b)))
|> Maybe.withDefault Cmd.none