抱歉新手的问题(以及我的英文):)
我尝试编写以下函数:
我知道如何执行HTTP请求。我有一个函数来解析来自 URL1 的请求。但我不知道如何:
答案 0 :(得分:1)
发送Http意味着与外界交谈,其中涉及Signal
在榆树。因此, URL2 的最终结果将包含在Signal
中。只要您对此感到满意,您就可以使用if来返回Maybe
中Signal
的内容。例如:
import Maybe
import Http
-- helper functions
isSuccess : Http.Response a -> Bool
isSuccess response = case response of
Http.Success _ -> True
_ -> False
responseToMaybe : Http.Response a -> Maybe.Maybe a
responseToMaybe response = case response of
Http.Success a -> Just a
_ -> Nothing
parseContentAndExtractUrl : String -> String
parseContentAndExtractUrl = identity -- this still requires your implementation
-- URL1
startUrl : String
startUrl = "www.example.com" -- replace this with your URL1
result1 : Signal (Http.Response String)
result1 = Http.sendGet <| constant startUrl
-- URL2
secondUrl : Signal String
secondUrl = result1
|> keepIf isSuccess (Http.Success "")
|> lift (\(Http.Success s) -> s)
|> lift parseContentAndExtractUrl
result2 : Signal (Maybe String)
result2 = secondUrl
|> Http.sendGet
|> lift responseToMaybe
请注意,有计划让所有这些更容易使用:https://groups.google.com/d/topic/elm-discuss/BI0D2b-9Fig/discussion
答案 1 :(得分:0)
我原则上你想要这样的东西:
import Maybe
import Http
type Url = String
getContentFromUrl : Maybe Url -> Maybe String
getContentFromUrl url = --your implementation
extractUrlFromContent : Maybe String -> Maybe Url
extractUrlFromContent content = --your implementation
content = getContentFromUrl (Just "http://example.com")
|> extractUrlFromContent
|> getContentFromUrl