{"action":"created","issue":{"url":"https://api.github.com/repos/suzuki-hoge/dev-note/issues/13","repository_url":"https://api.github.com/repos/suzuki-hoge/dev-note","labels_url":"https://api.github.com/repos/suzuki-hoge/dev-note/issues/13/labels{/name}","comments_url":"https://api.github.com/repos/suzuki-hoge/dev-note/issues/13/comments","events_url":"https://api.github.com/repos/suzuki-hoge/dev-note/issues/13/events","html_url":"https://github.com/suzuki-hoge/dev-note/pull/13","id":193352719,"number":13,"title":"write draft haskell-ddd-business-and-system","user":{"login":"suzuki-hoge","id":18749992,"avatar_url":"https://avatars.githubusercontent.com/u/18749992?v=3","gravatar_id":"","url":"https://api.github.com/users/suzuki-hoge","html_url":"https://github.com/suzuki-hoge","followers_url":"https://api.github.com/users/suzuki-hoge/followers","following_url":"https://api.github.com/users/suzuki-hoge/following{/other_user}","gists_url":"https://api.github.com/users/suzuki-hoge/gists{/gist_id}","starred_url":"https://api.github.com/users/suzuki-hoge/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/suzuki-hoge/subscriptions","organizations_url":"https://api.github.com/users/suzuki-hoge/orgs","repos_url":"https://api.github.com/users/suzuki-hoge/repos","events_url":"https://api.github.com/users/suzuki-hoge/events{/privacy}","received_events_url":"https://api.github.com/users/suzuki-hoge/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":6,"created_at":"2016-12-04T14:07:56Z","updated_at":"2016-12-07T00:07:22Z","closed_at":"2016-12-06T13:40:40Z","pull_request":{"url":"https://api.github.com/repos/suzuki-hoge/dev-note/pulls/13","html_url":"https://github.com/suzuki-hoge/dev-note/pull/13","diff_url":"https://github.com/suzuki-hoge/dev-note/pull/13.diff","patch_url":"https://github.com/suzuki-hoge/dev-note/pull/13.patch"},"body":"[doc](https://github.com/suzuki-hoge/dev-note/blob/haskell-ddd-business-and-system/haskell-ddd-business-and-system)"},"comment":{"url":"https://api.github.com/repos/suzuki-hoge/dev-note/issues/comments/265314343","html_url":"https://github.com/suzuki-hoge/dev-note/pull/13#issuecomment-265314343","issue_url":"https://api.github.com/repos/suzuki-hoge/dev-note/issues/13","id":265314343,"user":{"login":"zzzkino","id":7077640,"avatar_url":"https://avatars.githubusercontent.com/u/7077640?v=3","gravatar_id":"","url":"https://api.github.com/users/zzzkino","html_url":"https://github.com/zzzkino","followers_url":"https://api.github.com/users/zzzkino/followers","following_url":"https://api.github.com/users/zzzkino/following{/other_user}","gists_url":"https://api.github.com/users/zzzkino/gists{/gist_id}","starred_url":"https://api.github.com/users/zzzkino/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/zzzkino/subscriptions","organizations_url":"https://api.github.com/users/zzzkino/orgs","repos_url":"https://api.github.com/users/zzzkino/repos","events_url":"https://api.github.com/users/zzzkino/events{/privacy}","received_events_url":"https://api.github.com/users/zzzkino/received_events","type":"User","site_admin":false},"created_at":"2016-12-07T00:07:22Z","updated_at":"2016-12-07T00:07:22Z","body":"> apply のところ、Either に揃えて do 使うと良いかも？\r\n\r\nもし IO がなかったら、 Either モナドでこんなふうに書けます。\r\n\r\n```haskell\r\nisExist :: UserId -> Bool\r\nisExist userId = True\r\n\r\ncheckCombination :: Item -> Maybe Option -> Maybe InvalidReason\r\ncheckCombination item option = case (item, option) of\r\n    (PersonalComputer, Just Replacement) -> Just PersonalComputerAndReplacement\r\n    (Keyboard,         Just Backup)      -> Just KeyboardAndBackup\r\n    _                                    -> Nothing\r\n\r\nsave :: UserId -> Item -> Maybe Option -> LicenseKey\r\nsave userId item option = LicenseKey \"license-key-123\"\r\n\r\nsendMail :: MailTitle -> ()\r\nsendMail title = ()\r\n\r\napply :: UserId -> Item -> Maybe Option -> IO (Either InvalidReason LicenseKey)\r\napply userId item option = return $ do\r\n    if isExist userId then Right () else Left NoUser\r\n    case checkCombination item option of\r\n      Just reason -> Left reason\r\n      Nothing     -> Right ()\r\n    let license = save userId item option\r\n    return $ sendMail $ mailTitle userId item option\r\n    return license\r\n```\r\n\r\napply のところ、成功した場合（Right の場合）に限り次の行に進んで行くという動作になり、\r\ndo 記法の効果でネストが少なく書けます。\r\n\r\nそれぞれの関数が Either を返すことにして良ければもっと見やすくなる。\r\n\r\n```haskell\r\ndata User = User { userId :: UserId } deriving Show\r\n\r\nfindUser :: UserId -> Either InvalidReason User\r\nfindUser = Right . User\r\n\r\ncheckCombination :: Item -> Maybe Option -> Either InvalidReason ()\r\ncheckCombination item option = case (item, option) of\r\n    (PersonalComputer, Just Replacement) -> Left PersonalComputerAndReplacement\r\n    (Keyboard,         Just Backup)      -> Left KeyboardAndBackup\r\n    _                                    -> Right ()\r\n\r\nsave :: UserId -> Item -> Maybe Option -> LicenseKey\r\nsave userId item option = LicenseKey \"license-key-123\"\r\n\r\nsendMail :: MailTitle -> ()\r\nsendMail title = ()\r\n\r\napply :: UserId -> Item -> Maybe Option -> IO (Either InvalidReason LicenseKey)\r\napply userId item option = return $ do\r\n    findUser userId\r\n    checkCombination item option\r\n    let license = save userId item option\r\n    return $ sendMail $ mailTitle userId item option\r\n    return license\r\n```\r\n\r\n最後に各関数が IO Either を返すようにして、 ExceptT モナドトランスフォーマを使うようにすると、\r\nIO が使えるし Either による失敗も do で構造化できるようになります。\r\n\r\n```haskell\r\nfindUser :: UserId -> IO (Either InvalidReason User)\r\nfindUser = return . Right . User\r\n\r\ncheckCombination :: Item -> Maybe Option -> Either InvalidReason ()\r\ncheckCombination item option = case (item, option) of\r\n    (PersonalComputer, Just Replacement) -> Left PersonalComputerAndReplacement\r\n    (Keyboard,         Just Backup)      -> Left KeyboardAndBackup\r\n    _                                    -> Right ()\r\n\r\nsave :: UserId -> Item -> Maybe Option -> IO LicenseKey\r\nsave userId item option = return $ LicenseKey \"license-key-123\"\r\n\r\nsendMail :: MailTitle -> IO ()\r\nsendMail title = print title\r\n\r\napply :: UserId -> Item -> Maybe Option -> IO (Either InvalidReason LicenseKey)\r\napply userId item option = runExceptT $ do\r\n    ExceptT $ findUser userId\r\n    ExceptT $ liftIO $ return $ checkCombination item option\r\n    license <- liftIO $ save userId item option\r\n    liftIO $ sendMail $ mailTitle userId item option\r\n    return license\r\n```\r\n\r\nという意味でした！"}}
{"actor":{"display_login":"zzzkino"}}