Replacing do by >>= for a scotty post

有些话、适合烂在心里 提交于 2020-01-03 17:01:11

问题


post "/introduceAnIdea"  $ do
        command <- jsonData
        json $ handle command

How would you remove the do and change it with >>= ?


回答1:


Here's how to rewrite do-notation as >>= and >>: (NB: a newline becomes ; in the c-like notation option, which I use here.)

do { a <- m; b... } = m >>= \a -> do { b... }

do { a; b... } = a >> do { b... }

do { a } = a

So this becomes:

post "/introduceAnIdea"  $ do { command <- jsonData; json $ handle command}
= post "/introduceAnIdea" $ jsonData >>= \command -> do {json $ handle command}
= post "/introduceAnIdea" $ jsonData >>= \c -> json $ handle c



回答2:


post "/introduceAnIdea" $ jsonData >>= (json . handle)

I don't think that's necessarily better in this case though.



来源:https://stackoverflow.com/questions/52147265/replacing-do-by-for-a-scotty-post

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!