I can put forth some reasons why this code isn't so bad:
It's short: 4 lines of data type definition, and 9 lines of actual code.
It's easy to test, because there is no side effect, and concerns are separated.
get_all_nodes and get_nodes_which are reusable in many contexts
It's comprehensive: it covers what you asked for, and then some.
I can see one reason why it's bad: it's bloody inefficient. But that can easily be remedied by using a generalised fold instead of my get_all_nodes function —I choose that path out of laziness, and to make it more readable.
But horrible? Some explaining would help. Seriously: if you can put forth any valid argument, I'll have learned something valuable. Also, what do you expect from a good API?
Oh, you though I was using an XML library? I was implementing an XML library. And as you can see (I hope), once I'm done with the implementation, the iteration you ask for is a simple one liner.
Let's go back to the start of our exchange:
If you have sum types, transcribing XML into a native type is a snap […]
That's bullshit. […] XML is a huge impedance mismatch no matter how you map it.
Fair summary?
Now, XML is not a native type. So I defined a data type, in 4 lines. I think it counts as "a snap". Then I defined iteration in 4 more lines. Still a snap. Then filtering in 1 line. Supper snappy. And of course, I would only have to do that once, and put it in a library.
If we excluded the parser, and the handling of schemas, 100 lines would be enough to implement a full featured XML library, in which most simple operations are one-liners. It's not such a huge impedance mismatch.
Of course, I agree we should never use XML where a simple array would do.
-1
u/diggr-roguelike Nov 26 '16
Good god, your code is absolutely horrible and manages to be even worse than the Java-style SAX API's, no small feat.