⍝ Remove comment from line of APL:
⎕ml←0
display ⎕cr'rmcm'
┌→──────────────────────────────────────────────────────────────────────┐
↓rmcm←{ ⍝ APL source with comments removed. │
│ cm←∨\(⍵='⍝')>≠\⍵='''' ⍝ mask of comments. │
│ ⎕UCS(cm×32)+(~cm)×⎕UCS ⍵ ⍝ blanks for comments in source matrix.│
│} │
└───────────────────────────────────────────────────────────────────────┘
display rmcm ⎕cr'rmcm'
┌→──────────────────────────────────────────────────────────────────────┐
↓rmcm←{ │
│ cm←∨\(⍵='⍝')>≠\⍵='''' │
│ ⎕UCS(cm×32)+(~cm)×⎕UCS ⍵ │
│} │
└───────────────────────────────────────────────────────────────────────┘
dup←{ ⍝ blah, blah,
⍝ blah, blah,
⍵ ⍵ ⍝ blah, blah,
⍝ blah, blah.
⍝ (c) C-A.S.
}
clean_fn←{⎕fx(dtb∘rmcm¨⎕nr ⍵)~⊂''} ⍝ remove white space from function
clean_fn'dup' ⍝ remove comments and blank lines
display ⎕cr'dup' ⍝ uncommented function
┌→──────────┐
↓dup←{ │
│ ⍵ ⍵│
│ } │
└───────────┘
one_liner←{ ⍝ Turn function into 1-liner.
⎕FX↑,↓{ ⍝ fix:
bd←'{⋄' '{' ⍝ brace-diamond → brace
db←'⋄}' '}' ⍝ diamond-brace → brace
↑subs/bd db ⍵ ⍝ substitution in
}↑{⍺,'⋄',⍵}/⎕NR ⍵ ⍝ diamond-joined lines.
}
display ⎕cr one_liner clean_fn'display'
┌→──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
↓display←{⎕IO ⎕ML←0 1 ⋄ box←{vrt hrz←(¯1+⍴⍵)⍴¨'│─' ⋄ top←'─⊖→'[¯1↑⍺],hrz ⋄ bot←(⊃⍺),hrz ⋄ rgt←'┐│',vrt,'┘' ⋄ lax←'│⌽↓'[¯1↓1↓⍺],¨⊂vrt ⋄ lft←⍉'┌',(↑lax),'└' ⋄ lft,(top⍪⍵⍪bot),rgt ⋄ } ⋄ deco←{⍺←type open ⍵ ⋄ ⍺,axes ⍵} ⋄ axes←{(-2⌈⍴⍴⍵)↑1+×⍴⍵} ⋄ open←{16::(1⌈⍴⍵)⍴⊂'[ref]' ⋄ (1⌈⍴⍵)⍴⍵} ⋄ trim←{(~1 1⍷∧⌿⍵=' ')/⍵} ⋄ type←{{(1=⍴⍵)⊃'+'⍵}∪,char¨⍵} ⋄ char←{⍬≡⍴⍵:'─' ⋄ (⊃⍵∊'¯',⎕D)⊃'#~'}∘⍕ ⋄ line←{(6≠10|⎕DR' '⍵)⊃' -'} ⋄ {0=≡⍵:' '⍪(open ⎕FMT ⍵)⍪line ⍵ ⋄ 1 ⍬≡(≡⍵)(⍴⍵):'∇' 0 0 box ⎕FMT ⍵ ⋄ 1=≡⍵:(deco ⍵)box open ⎕FMT open ⍵ ⋄ ('∊'deco ⍵)box trim ⎕FMT ∇¨open ⍵ ⋄ }⍵}│
└───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
⍝ JoHo's expression to remove comments from all trad- and dfns and operators:
⎕cr¨'gcd' 'osc' ⍝ before: commented fns
┌──────────────────────────────────────┬─────────────────────────────────────────────────────┐
│gcd←{ ⍝ Greatest common divisor.│osc←{ ⍝ Oscillate - probably returns 1.│
│ ⍵=0:|⍺ │ 1=⍵:1 │
│ ⍵ ∇ ⍵|⍺ │ 2|⍵:∇ 1+3×⍵ │
│} │ ∇ ⍵÷2 │
│ │} │
└──────────────────────────────────────┴─────────────────────────────────────────────────────┘
pco←⊢ ⍝ pco has a very large ⎕CR; zap it to reduce test time.
rmcm(refs ⎕this).{⍺⍺{0=⍴⍵: ⋄ ⎕fx∘⍺⍺∘⎕cr¨⍵}⎕nl-,⍵∘.+0.1 0.2}⊂3 4
⎕cr¨'gcd' 'osc' ⍝ after: uncommented fns
┌───────────┬───────────────┐
│gcd←{ │osc←{ │
│ ⍵=0:|⍺ │ 1=⍵:1 │
│ ⍵ ∇ ⍵|⍺│ 2|⍵:∇ 1+3×⍵│
│} │ ∇ ⍵÷2 │
│ │} │
└───────────┴───────────────┘
⍝∇ rmcm display gcd osc dtb subs refs
Back to: code
Back to: Workspaces