APL Glyph Typing Tutor

Thanks @abrudz , I don’t know enough about APL to extract the key/val pairs if they’re somehow in there. I tried :

⍴ keyboards
┌→────┐
│24263│
└~────┘

Which tells me it’s basically a string, so I need to do some magic to extract those glyph/key-binding pairs for my purposes.

Here’s how I did it:

'notes.keyboards'⎕CY'dfns'     ⍝ CopY in
matrix←⎕FMT keyboards          ⍝ convert string to matrix
begin←'┌'⍳⍨⊣/matrix            ⍝ find line with ┌ in first column
kbd←12↑begin↓matrix            ⍝ take next 12 lines (3 lines per key row)
pairs←'(\S) (\S)'⎕S'\1\2',kbd  ⍝ PCRE regex Search for space-delimited non-spaces
⎕←↑pairs                       ⍝ make vector of vectors into matrix

Try it online!

2 Likes

Wow! this is amazing! No wonder you guys don’t have a table, you can produce it on command!

I’m still trying to make sense of this magic by going expression by expression, but it’s really cool! Thanks for taking the time to share your knowledge! :smiley:

Let me know if there’s any of the code in particular that you want me to explain.

1 Like

I assigned matrix pairs to a variable and got one row to print! now only to loop through all the rows and print it, I have some searching to do :sweat_smile:

x ← ↑pairs
x[1;2],' → `',x[1;1]

┌→─────┐
│⌺ → `~│
└──────┘

No no, in APL land, we don’t loop! :wink:

Firstly, you can change the regex substitution pattern from \1\2 to '\2 → `\1'. Try it online!

A more APLy approach would be to use array operations on the entire matrix using (⊢/x),' → `',⍤1 0⊣/x where ,⍤1 0 concatenates the entire vector (rank-1 array) ' → `' to each scalar (rank-0 array) of the left column (⊣/) of x. Try it online!

3 Likes

I was able to modify the original to generate a table. Thanks again!

Glyph Keys
`~
`!
`@
`#
`$
`%
`^
`&
`*
`(
`)
! `_
`+
``
¨ `1
¯ `2
< `3
`4
= `5
`6
> `7
`8
`9
`0
× `-
÷ `=
`E
`T
`I
`O
`P
`{
`}
`
? `q
`w
`e
`r
~ `t
`y
`u
`i
`o
* `p
`[
`]
`\
`J
`K
`L
`:
`"
`a
`s
`d
_ `f
`g
`h
`j
`k
`l
`;
`’
`Z
`<
`>
`?
`z
`x
`c
`v
`b
`n
`,
`.
`/

Glyphs as code, proper keys, and avoid various issues with characters that need escaping (Try it online!):

Glyph Keys
`~
`!
`@
`#
`$
`%
`^
`&
`*
`(
`)
! `_
`+
``
¨ `1
¯ `2
< `3
`4
= `5
`6
> `7
`8
`9
`0
× `-
÷ `=
`E
`T
`I
`O
`P
`{
`}
`|
? `q
`w
`e
`r
~ `t
`y
`u
`i
`o
* `p
`[
`]
`\
`J
`K
`L
`:
`"
`a
`s
`d
_ `f
`g
`h
`j
' `k
`l
`;
`'
`Z
`<
`>
`?
`z
`x
`c
`v
`b
`n
| `m
`,
`.
`/
3 Likes

Any chance you could explain line by line, maybe with some sample input/output, what’s happening in this code?

1 Like

Hi Jeremy, it is all @abrudz code, I just added the markdown formatting for it to show nicely in the 2nd to last line of code. I have run each line one by one to get a sense of what is happening. One thing I’m still trying to understand is the line where the begin variable is being set. The comment explains what’s happening but I’m trying to work my way through how/why it is happening.

1 'notes.keyboards'⎕CY'dfns'     ⍝ CopY in
2 matrix←⎕FMT keyboards          ⍝ convert string to matrix
3 begin←'┌'⍳⍨⊣/matrix            ⍝ find line with ┌ in first column
4 kbd←12↑begin↓matrix            ⍝ take next 12 lines (3 lines per key row)
5 pairs←'(\S) (\S)'⎕S'| \2 | `\1 |',kbd  ⍝ PCRE regex Search for space-delimited non-spaces
6 ⎕←↑pairs                       ⍝ make vector of vectors into matrix

In my understanding, line 3 assigns the position of the char being searched (30) to variable begin then it counts 12 rows down the matrix starting from begin.

I was able to establish that I can do this (in a much less glamourous way) using grep like so:

grep ^┌ APL_keyboards.txt -m 1 -A 12

I saved the output of line 1 into a text file and then used grep against it. So the above grep is mimicking the behaviour of lines 3 and 4.

I don’t quite understand in line 5 as to how ⎕S is doing its magic, but it seems to me it’s dyadic and LHS is the regex and the RHS is what it outputs, however, somehow the comma operator makes this whole thing operate on the input which is the kbd variable which was assigned on line 4.

OK, this is complete insanity, but here it is. Using my limited knowledge of Unix bash commands and the pipe operator, I can get something sort of similar to the extremely elegant solution in APL.

cat APL_keyboards.txt | grep ^┌  -m 1 -A 12 | grep  -E '(\S) (\S)' | cut -d"│" -f 2-13 | sed -e "s/│/,/g" | sed -e 's/Shift/ /g'  -e 's/Lock//g' -e 's/Tab//g' -e 's/Caps//g' | tr -d '\n' | tr -s " "
~ ⌺ ,! ⌶ ,@ ⍫ ,# ⍒ ,$ ⍋ ,% ⌽ ,^ ⍉ ,& ⊖ ,* ⍟ ,( ⍱ ,) ⍲ ,_ ! ` ⋄ ,1 ¨ ,2 ¯ ,3 < ,4 ≤ ,5 = ,6 ≥ ,7 > ,8 ≠ ,9 ∨ ,0 ∧ ,- × ,Q ,W ,E ⍷ ,R ,T ⍨ ,Y ,U ,I ⍸ ,O ⍥ ,P ⍣ ,{ ⍞ ,q ? ,w ⍵ ,e ∊ ,r ⍴ ,t ~ ,y ↑ ,u ↓ ,i ⍳ ,o ○ ,p * ,[ ← ,A ,S ,D ,F ,G ,H ,J ⍤ ,K ⌸ ,L ⌷ ,: ≡ ," ≢ ,a ⍺ ,s ⌈ ,d ⌊ ,f _ ,g ∇ ,h ∆ ,j ∘ ,k ' ,l ⎕ ,; ⍎ ,' ⍕ ,Z ⊆ ,X ,C ,V ,B ,N ,M ,< ⍪ ,> ⍙ ,? ⍠ , ,z ⊂ ,x ⊃ ,c ∩ ,v ∪ ,b ⊥ ,n ⊤ ,m | ,, ⍝ ,. ⍀ ,/ ⌿ , %

⎕S is a dyadic operator like but both operands are (here) arrays, not functions. The left operand is the PCRE regex to match, and for each match it returns the transformed match described by the right operand where \1 and \2 refer to the parenthesised capturing groups (\S).

1 Like

I understood that to some extent and in the examples the there’s the <REGEX> OP <OUTPUT Pattern> [Vector being operated on] but in the code there’s a comma and then the variable kbd :

pairs←'(\S) (\S)'⎕S'| \2 | `\1 |',kbd 

What is that comma doing in this context? My limited understand is that I can concat stuff using comma?

Oh, I forgot to explain that. ⎕S can only work on vectors (and vectors of vectors), not on e.g. matrices. So monadic , ravels kbd into a vector as preparation for the regex search.

1 Like

Thank you! that’s the piece I was missing!

How did you do that?

1 Like

I just did

      'notes.keyboards'⎕CY'dfns'
      )ed keyboards

in Dyalog commandline and that opened up the variable in an editor window

1 Like
keyboards⎕NPUT'/tmp/kbds.txt'
2 Likes

These Glyphs don’t seem to align with 18.2 Documentation e.g. there is no L as defined in primitive functions and seems to contradict the up arrow

I don’t follow. L (letter “ell”) is not a primitive, but possibly confusable for the downstile . Not sure what you mean by a contradiction on

Historical anecdote: APL used to be written with italic letters but upright numbers and symbols. This fits better with traditional mathematical notation for variables, and eliminates confusables, e.g. L vs ⌊, O vs 0, U vs ∪. You can see this in certain APL fonts, like SAX2.