haskell - Counting and filtering Arrow for HXT -
i'm trying parse xml, want filter , extract determinate number of children given node. example:
<root> <node id="a" /> <node id="b" /> <node id="c" /> <node id="d" /> </root>
and if execute arrow getchildren >>> myfilter 2
, nodes id "a" , "b".
intuition gives should use state arrow keep track, don't know how that.
i tried myself it's not want, doesn't elegant, , doesn't work. try run chain of arrows runsla
, integer parameter initial state, , defining:
takeonly :: iosla int xmltree xmltree takeonly = changestate (\s b -> s-1) >>> accessstate (\s b -> if s >= 0 b else nothing)
but of course can't return nothing
, need return xmltree. don't want return @ all!
there's better way out there. can me?
thanks time , help!
it more idiomatic use combinators in control.arrow.arrowlist handle kind of thing.
the package provides (>>.) :: b c -> ([c] -> [d]) -> b d
, "combinator converting result of list arrow list". allows use take
function have lists in context.
here's quick version of how might use it:
module main import text.xml.hxt.arrow takeonly :: (arrowxml a) => int -> xmltree xmltree takeonly n = getchildren >>. take n main = let xml = "<root><node id='a' /><node id='b' />\ \<node id='c' /><node id='d' /></root>" print =<< runx (readstring [] xml >>> getchildren >>> takeonly 2)
this believe approximately you're looking for:
travis@sidmouth% ./arrowtake [ntree (xtag (lp node) [ntree (xattr (lp id)) [ntree (xtext "a") []]]) [], ntree (xtag (lp node) [ntree (xattr (lp id)) [ntree (xtext "b") []]]) []]
no iosla
required. note i've changed function type little—this version seems nicer me, convert more type in version.
Comments
Post a Comment