Skip to content

Instantly share code, notes, and snippets.

@kkismd
Last active September 14, 2015 10:27
Show Gist options
  • Save kkismd/fae67c2efbe1042a625f to your computer and use it in GitHub Desktop.
Save kkismd/fae67c2efbe1042a625f to your computer and use it in GitHub Desktop.
Optionを返すfor式に失敗した場所の情報を埋め込む
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