return
. Bunun yerine, return
'un yaptığı, bir monoya bir değer enjekte etmektir (bu durumda IO
). Sen seçenekler
en basit bir çift
scrapePage :: String -> IO()
scrapePage url = do
doc <- fromUrl url
title <- liftM headMay $ runX $ doc >>> css "head.title" >>> getText
if (isNothing title) then return() else do
date <- liftM headMay $ runX $ doc >>> css "span.dateTime" ! "data-utc"
if (isNothing date) then return() else do
-- etc
-- make page object and send it to db
return()
başka bir seçenek IO
monad yok ki burada unless
scrapePage url = do
doc <- fromUrl url
title <- liftM headMay $ runX $ doc >>> css "head.title" >>> getText
unless (isNothing title) do
date <- liftM headMay $ runX $ doc >>> css "span.dateTime" ! "data-utc"
unless (isNothing date) do
-- etc
-- make page object and send it to db
return()
genel sorun kullanmak istiyorsa kullanmaktır var kontrol etkileri (istisnalar hariç). Yukarıdaki kod hiçbiri olmuştur: Eğer gerçekten ContT
scrapePage :: String -> IO()
scrapePage url = runContT return $ do
doc <- fromUrl url
title <- liftM headMay $ runX $ doc >>> css "head.title" >>> getText
when (isNothing title) $ callCC ($())
date <- liftM headMay $ runX $ doc >>> css "span.dateTime" ! "data-utc"
when (isNothing date) $ callCC ($())
-- etc
-- make page object and send it to db
return()
UYARI kullanmak gerekir tam şişmiş kontrol etkileri almak istiyorsanız Öte yandan, belki monad trafo
scrapePage url = liftM (maybe() id) . runMaybeT $ do
doc <- liftIO $ fromUrl url
title <- liftIO $ liftM headMay $ runX $ doc >>> css "head.title" >>> getText
guard (isJust title)
date <- liftIO $ liftM headMay $ runX $ doc >>> css "span.dateTime" ! "data-utc"
guard (isJust date)
-- etc
-- make page object and send it to db
return()
kullanabilirsiniz test edildi, hatta kontrol edildi!
İstediğiniz bu mu? http://www.haskellforall.com/2012/07/breaking-from-loop.html –