在将两个案例语句嵌套在榆树中时,我可以避免重复代码吗?
我正在将可能的字符串
转换为date
。我已经将Aliased date
键入出生日期
以进行可读性。
birthdate_from_url : Url.Url -> Birthdate
birthdate_from_url url =
case url.query of
Just query ->
case Date.fromIsoString query of
Ok birthdate ->
birthdate
Err _ ->
defaultBirthdate
Nothing ->
defaultBirthdate
在此嵌套情况下,我必须调用DefaultBirthdate
两次可能的“失败”。
有或不使用案例
,是否有其他方法?
I'm converting a Maybe String
into a Date
. I've type aliased Date
as Birthdate
for readability.
birthdate_from_url : Url.Url -> Birthdate
birthdate_from_url url =
case url.query of
Just query ->
case Date.fromIsoString query of
Ok birthdate ->
birthdate
Err _ ->
defaultBirthdate
Nothing ->
defaultBirthdate
With this nested case I'm having to call defaultBirthdate
twice for each of the possible "failures".
Is there an alternative approach with or without the use of case
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(1)
您可以使用
< < < /a>:
You could use
Maybe.map
: