I'm trying to parse an XML, but I want to filter and extract only a determinate number of children from a given node. For example:
<root>
<node id="a" />
<node id="b" />
<node id="c" />
<node id="d" />
</root>
And then if I execute the arrow getChildren >>> myFilter 2
, I would get back only the nodes with id "a" and "b".
Intuition gives that I should use a State Arrow to keep track, but I don't know how to do that.
I tried to do it myself but it's not exactly what I want, doesn't look very elegant, and doesn't work. I try to run my chain of arrows with runSLA
and a integer parameter as initial state, and then defining:
takeOnly :: IOSLA Int XmlTree XmlTree
takeOnly = changeState (\s b -> s-1)
>>> accessState (\s b -> if s >= 0 then b else Nothing)
But of course I can't return Nothing
, I need to return a XmlTree. But I don't want to return anything at all!
There's probably a better way out there. Can you help me?
Thanks for your time and help!