Note that there are some explanatory texts on larger screens.

plurals
  1. POI need a function that operates on child nodes
    text
    copied!<p>I'm working on the project I was working on here:</p> <p><a href="https://stackoverflow.com/questions/15441956/how-do-i-make-a-do-block-return-early">How do I make a do block return early?</a></p> <p>I went with the monad transformer method at my function looks something like this:</p> <pre><code>scrapePost :: String -&gt; IO () scrapePost url = liftM (fromMaybe ()) . runMaybeT $ do doc &lt;- lift $ fromUrl url -- get a bunch of stuff from the page -- send it to the db replies &lt;- lift . runX $ doc &gt;&gt;&gt; css ".post.reply" -- here is the problem mapM_ (parseReply url (fromJust page_id)) replies -- here is the problem </code></pre> <p><code>parseReply</code> is the function I need, but I can't seem to get it right. </p> <p>Here is my feeble attempt to start that function:</p> <pre><code>parseReply :: String -&gt; String -&gt; XNode -&gt; Maybe () parseReply url op_id reply = do reply_id &lt;- runX $ reply ! "id" return () </code></pre> <p>BTW, I am using HandsomeSoup</p> <p>I will operate just like the <code>scrapePost</code> function with a set css rules to scrape, drop replies that don't have all the values, and send them to the db. </p> <p>I want to use <code>mapM</code> because my hope is to replace all the <code>mapM</code> with <code>liftIO</code> and see the performance difference. </p> <p><strong>[UPDATE]</strong></p> <p>So it turns out I didn't need to do any type acrobatics, I just needed a way to turn the reply node into a root node which I found <a href="https://stackoverflow.com/a/9435726/334632">here</a>.</p> <p>Since <code>parseReply</code> is only used in the <code>MaybeT IO ()</code> context, its type doesn't need to change and <code>scrapePost</code> can stay the same.</p> <p><code>parseReply</code> becomes:</p> <pre><code>toRoot :: ArrowXml a =&gt; XmlTree -&gt; a n XmlTree toRoot node = root [] [constA node] parseReply :: String -&gt; String -&gt; XmlTree -&gt; MaybeT IO () parseReply url op_id reply = do let node = toRoot reply reply_id &lt;- lift . liftM (`atMay` 0) $ runX $ node &gt;&gt;&gt; css "div" ! "id" guard (isJust reply_id) return () </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload