...no? If there is functionality I need, and no library implements it, I will implement it myself. That goes for any language. Otherwise, the job of a programmer would simply be to string together existing libraries, not writing anything meaningful.
...no? If there is functionality I need, and no library implements it, I will implement it myself. That goes for any language. Otherwise, the job of a programmer would simply be to string together existing libraries, not writing anything meaningful.
Are you saying in the scenario described, your solution would have been to write an XML parser from scratch?
It can't be that hard.™
XML is particularly simple though, dealing with something like JPEG would be an entirely different experience.
# https://www.intel.com/content/dam/develop/public/us/en/include/intrinsics-guide/data-3-6-1.xml
xml ← •FChars •wdpath •file.At ⊑•args
#⌜
# An xml parser good enough for our use case
# Accept xml; return (as three lists):
# - Parent index of each tag
# - Contents of open tag
# - Text after last child tag
E ← +`⊸×⟜¬-⊢
ParseXml ← {
text‿tags ← ('?'=·⊑·⊑1⊸⊑)⊸(↓¨) <˘⍉⌊‿2⥊((+⟜»E∨)˝"<>"=⌜⊢)⊸⊔𝕩
d←+`tt←1-(+˜⊸+´'/'=0‿¯1⊸⊏)¨tags # Tag type: ¯1 close, 0 void, 1 open
tp←(⍋⊏⟜d)⊸⊏∘/˘ 1‿¯1=⌜tt # Tag pairs
! (∧`' '⊸≠)⊸/¨⊸≡⟜(1⊸↓¨)˝tp⊏tags # Tag matching
oi←(0<tt)(⌈`↕∘≠⊸×)⊸⊏⌾((⍋d)⊸⊏)↕≠tt # Open index, for closed and void tags
ci←⍋⊸⊏○(∾⟜(/0=tt))˝tp
pi←(/0≤tt)(1-˜⍋)¯1⌾⊑ci⊏oi # Parent index
⟨pi,(0≤tt)/tags,ci⊏text⟩
}
I don't ...EDIT: Apparently https://mlochbaum.github.io/BQN/doc/primitive.html is a list of functions. Interesting.
After I learned to read Haskell, I liked to believe I could learn to read anything. But I think I'll hang up my hat on this one.
d←+`tt←1-(+˜⊸+´'/'=0‿¯1⊸⊏)¨tags # Tag type: ¯1 close, 0 void, 1 open
(I'll handwave the parsing a bit, but the rules are simple, with only 5 or so levels of precedence. Modifiers bind tighter than functions: superscripts like ´ are 1-modifiers and apply to one operand on the left, and glyphs with an unbroken circle like ⊸ are 2-modifiers and apply to an operand on each side, left-associative. The ligature ‿ is one way to write lists and has higher precedence than modifiers.)BQN evaluates functions right to left, so the first thing that's done to evaluate this code is (+˜⊸+´'/'=0‿¯1⊸⊏)¨tags. And ¨ is each, so we apply +˜⊸+´'/'=0‿¯1⊸⊏ to each tag, which is a string of whatever's between < and >. This is a 4-component train (+˜⊸+´)('/')(=)(0‿¯1⊸⊏), and the rules of train evaluation say that every other component starting from the end ('/' and 0‿¯1⊸⊏) apply to the argument and then the rest apply to those results. '/' is the slash character and applies as a constant function returning itself, and 0‿¯1⊸⊏ is the array 0‿¯1 bound to the function ⊏, which performs selection. Applied to a tag t, it gives what might be written t[[0,-1]] in more conventional notation, taking the first and last element. After that, '/'= compares to slash. This automatically maps over arrays, so now we have a two-element array where the first entry is 1 (BQN booleans are numbers) if there's a slash after the < and the second is 1 if there's a slash after the >.
Finally the train applies its first function +˜⊸+´ to the result. ´ is a fold, so we apply +˜⊸+ as an infix function between the two booleans. We can write (a +˜⊸+ b), which is (+˜a) + b by the definition of ⊸, which I think as adding +˜ as a preprocessing step on the left argument before the +. Then +˜a is a+a, so we have twice a plus b. That is, 2 if the tag starts with / and 1 if it ends with /. The case with both doesn't occur in my xml.
The rest of the expression is d←+`tt←1-numbers. The 1- transforms 0, 1, and 2 to -1, 0, and 1 as mentioned in the comment. This is given the name tt. Then +` is a prefix sum, since ` is scan. Taking this sum gives a list that goes up on each open tag and down on each closed tag, that is, the depth of tag nesting.
That went through a lot of concepts, but if you can parse that line then you can parse nearly every other one, since they're all made up of trains, modifiers, functions, and constants. The only other syntax in ParseXml is ⟨,⟩, which is just a normal array literal using angle brackets, and the braces {} for a function, with implicit argument 𝕩 in this case. The other knowledge required is what all these functions do. That's the hard part, but at this point it's no different from learning any other language. The functionality is just indicated with symbols instead of names.
Who the hell wants to be a programmer, dismal profession.