APL Wiki logo: Difference between revisions

Jump to navigation Jump to search
2,916 bytes added ,  15:17, 5 November 2019
Miraheze>Adám Brudzewsky
Miraheze>Adám Brudzewsky
Line 203: Line 203:
</source>
</source>
=== Making <code><circle/></code> tags ===
=== Making <code><circle/></code> tags ===
To help us create our SVG <code><circle/></code> tags, well set up a couple of helper functions. The first function will help us create tag attributes.
==== Formatting attributes ====
APL uses a [[high minus]] (<source lang=apl inline>¯</source>), to indicate that a number is negative. This avoids confusion with the [[negate]] function <source lang=apl inline>-</source>. However, SVG uses a regular dash, so we need to change our numeric arrays into character representations (using the [[format]] function, <source lang=apl inline>⍕</source>), and [[replace]] all occurrences of the symbol:
<source lang=apl>
      '¯'⎕R'-'⍕3 ¯1 2 ¯7
3 -1 2 -7
</source>
The attribute value needs to be quoted, so we [[concatenate|prepend]] (<source lang=apl inline>,</source>) two quotation marks, and then we [[rotate]] the text one step (left), thereby pushing the first one to the end:
<source lang=apl>
      1⌽'""','¯'⎕R'-'⍕3 ¯1 2 ¯7
"3 -1 2 -7"
</source>
Finally, create the full attribute phrase:
<source lang=apl>
      ' ','test','=',1⌽'""','¯'⎕R'-'⍕3 ¯1 2 ¯7
test="3 -1 2 -7"
</source>
==== Our first function ====
In the most basic form, a [[dfn]] ("dee fun") is just an expression in curly braces with <source lang=apl inline>⍺</source> and <source lang=apl inline>⍵</source> representing the left and right arguments, just like they are the leftmost and rightmost letters of the [[wikipedia:Greek alphabet]]:
<source lang=apl>
      Attr←{' ',⍺,'=',1⌽'""','¯'⎕R'-'⍕⍵}
      'test' Attr 3 ¯1 2 ¯7
test="3 -1 2 -7"
</source>
Notice that assignment works for functions too! Remember the [[#Automatic_mapping|automatic mapping]]? It doesn't apply to user-defined functions, but we can use the [[Each]] operator (<source lang=apl inline>¨</source>) instead:
<source lang=apl>
      'test' 'foo' Attr¨ 3 ¯1
┌─────────┬─────────┐
│ test="3"│ foo="-1"│
└─────────┴─────────┘
</source>
==== Flattening ====
Next, we make this list of strings (each of which is actually just a character list) into a simple list with the [[enlist]] function (<source lang=apl inline>∊</source>), and use above same concatenation and rotation techniques to finalise our tag:
<source lang=apl>
      2⌽'/><tag',∊'test' 'foo'Attr¨ 3 ¯1
<tag test="3" foo="-1"/>
</source>
Let's create a dfn for that too:
<source lang=apl>
      Circle←{⊂2⌽'/><circle',∊'cx' 'cy' 'r'Attr¨⍵}
      Circle 3 1 4
┌─────────────────────────────┐
│<circle cx="3" cy="1" r="4"/>│
└─────────────────────────────┘
</source>
Notice that a put in <source lang=apl inline>⊂</source>) which [[enclose]]s the result. This is because I want to deal with these tags a [[scalar]] elements.
<source lang=apl>
<source lang=apl>
</source>
</source>
<source lang=apl>
<source lang=apl>
</source>
</source>
<source lang=apl>
<source lang=apl>
</source>
</source>

Navigation menu