51

J can look like APL or English

 4 years ago
source link: https://www.tuicool.com/articles/iy2MNzZ
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

APL, in contrast with J, uses unique, distinctive symbols with less overloading. The visual relationship between APL symbols and concepts is somewhat clearer than that of J.

Many J primitive functions map directly to APL primitive functions. It is therefore relatively easy to naively render J primitives as the corresponding APL symbol using extensions such as pretty-mode and/or prettify-symbols-mode in Emacs. For J primitives which don't have an APL equivalent, we have to get a bit more creative.

Here is an excerpt from my Emacs init file showing an example set of J to APL primitive mappings.

pretty-mode and j-mode need to be installed; prettify-symbols-mode comes with Emacs by default.

(require 'pretty-mode)

(setq j-symbols
  '(("=\\."     . ?←)
    ("=:"       . ?⤆)
    ("_:"       . ?∞)
    ("<:"       . ?≤)
    (">:"       . ?≥)
    (">\\."     . ?⌈)
    ("<\\."     . ?⌊)
    ("\\*"      . ?×)
    ("%"        . ?÷)
    ("\\+\\."   . ?∨)
    ("-\\."     . ?¬)
    ("*\\."     . ?∧)
    ("%\\."     . ?⌹)
    ("\\+:"     . ?⍱)
    ("-:"       . ?≡)
    ("*:"       . ?⍲)
    ("%:"       . ?√)
    ("\\^\\."   . ?⍟)
    ("\\^:"     . ?⍣)
    ("\\$"      . ?⍴)
    ("\\$\\."   . ?ς)
    ("\\$:"     . ?∇)
    ("~"        . ?⍨)
    ("~\\."     . ?∪)
    ("~:"       . ?≠)
    ("|\\."     . ?⌽)
    ("|:"       . ?⍉)
    (",\\."     . ?⍪)
    (",:"       . ?⍿)
    (";:"       . ?⍧)
    ("#\\."     . ?⊥)
    ("#:"       . ?⊤)
    ("/\\."     . ?⍁)
    ("\\\\\\."  . ?⍂)
    ("/:"       . ?⍋)
    ("\\\\:"    . ?⍒)
    ("\\]"      . ?⊢)
    ("\\["      . ?⊣)
    ("\\[:"     . ?⍅)
    ("{\\."     . ?↑)
    ("}\\."     . ?↓)
    ("{:"       . ?⍏)
    ("}:"       . ?⍖)
    ("\""       . ?⍤)
    ("\"\\."    . ?⍎)
    ("\":"      . ?⍕)
    ("`"        . ?⍮)
    ("@"        . ?⍛)
    ("@\\."     . ?⌼)
    ("@:"       . ?⍜)
    ("\\&"      . ?∘)
    ("\\&\\."   . ?↺)
    ("\\&\\.>"  . ?¨)
    ("\\&:"     . ?⌾)
    ("\\?\\."   . ?⍰)
    ))

(defun prettify-j ()
  (progn
    (push '("a." . ?æ) prettify-symbols-alist)
    (push '("a:" . ?⍬) prettify-symbols-alist)
    (push '("A:" . ?⅍) prettify-symbols-alist)
    (push '("C." . ?ℂ) prettify-symbols-alist)
    (push '("d." . ?δ) prettify-symbols-alist)
    (push '("D." . ?Δ) prettify-symbols-alist)
    (push '("D:" . ?⌳) prettify-symbols-alist)
    (push '("e." . ?∊) prettify-symbols-alist)
    (push '("E." . ?⍷) prettify-symbols-alist)
    (push '("f." . ?ℱ) prettify-symbols-alist)
    (push '("i." . ?⍳) prettify-symbols-alist)
    (push '("i:" . ?ᵼ) prettify-symbols-alist)
    (push '("I." . ?⍸) prettify-symbols-alist)
    (push '("j." . ?ⅉ) prettify-symbols-alist)
    (push '("L." . ?ℒ) prettify-symbols-alist)
    (push '("NB." . ?⍝) prettify-symbols-alist)
    (push '("p." . ?ℙ) prettify-symbols-alist)
    (push '("p:" . ?⅌) prettify-symbols-alist)
    (push '("q:" . ?ℚ) prettify-symbols-alist)
    (push '("r." . ?∡) prettify-symbols-alist)
    (push '("y" . ?ω) prettify-symbols-alist)
    (push '("x" . ?α) prettify-symbols-alist)
    (push '("u" . ?⍶) prettify-symbols-alist)
    (push '("v" . ?⍹) prettify-symbols-alist)
    (prettify-symbols-mode)))

(pretty-add-keywords 'inferior-j-mode j-symbols)
(pretty-add-keywords 'j-mode j-symbols)

(add-hook 'inferior-j-mode-hook 'turn-on-pretty-mode)
(add-hook 'inferior-j-mode-hook 'turn-off-smartparens-mode)
(add-hook 'inferior-j-mode-hook 'prettify-j)
(add-hook 'j-mode-hook 'turn-on-pretty-mode)
(add-hook 'j-mode-hook 'turn-off-smartparens-mode)
(add-hook 'j-mode-hook 'prettify-j)

Here is a naive k-means clustering implementation in plain J:

init =. ([ ? #@:]) { ]
alloc =. {.@:I.@:(= <./)@:(+/@:*:@:-"1)
step =. (+/ % #)&>@:(alloc"1 2 </. [)
kmc =. ] step^:_ init

Here is how it looks like in Emacs with pretty-mode replacing many of the symbols:

vq6fUrm.png!web

The original J:

quicksort=: (($:@(<#[) , (=#[) , $:@(>#[)) ({~ ?@#)) ^: (1<#)

And J displayed with APL-like glyphs:

raAjMjq.png!web

This is probably equally unreadable if you are not already familiar with APL, but I do find it somewhat easier to read than the plain ASCII.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK