Last active
September 14, 2015 10:27
-
-
Save kkismd/fae67c2efbe1042a625f to your computer and use it in GitHub Desktop.
Optionを返すfor式に失敗した場所の情報を埋め込む
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val dict = Map( | |
"AAA" -> "BBB", | |
"BBB" -> "CCC", | |
"CCC" -> "DDD" | |
) | |
// Optionで返す場合 | |
for { | |
bbb <- dict.get("AAA") | |
ccc <- dict.get(bbb) | |
ddd <- dict.get(ccc) | |
} yield ddd | |
// Eitherで返す場合 | |
for { | |
bbb <- dict.get("AAA").toRight("最初の選択肢がありません").right | |
ccc <- dict.get(bbb).toRight("2番目の選択肢がありません").right | |
ddd <- dict.get(ccc).toRight("3番目の選択肢がありません").right | |
} yield ddd |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment