⍝ defs:

    :ReturnIf 3≠ ⎕nc'⎕SE.Dyalog.Utils.repObj'           ⍝ required library fn

    ⎕ex'Alpha'                                          ⍝ remove alias for 1
    ⎕ex (~∘' '¨ ↓⎕nl 3 4)~'defs' 'disp' 'externs' 'in'  ⍝ declutter for speed

    dup←{⍵ ⍵}                                           ⍝ single-line defn
    tup←{                                               ⍝ multi-line defn
        ⍵ ⍵ ⍵
    }

     avg ← (sum←(plus←+)(fold←⌿)) ÷ (num←≢)             ⍝ more definitions ...
    davg ← {(sum÷num)⍵}                                 ⍝ dfn
    each ← {⍺←⊢ ⋄ ⍺ ⍺⍺¨⍵}                               ⍝ dop
    dups ← dup each                                     ⍝ derived fn
    tups ← tup each
    isns ← (ns←⎕ns'')∘≡
   isnum ← ∊∘(digs←'0123456789')                        ⍝ named array comp
   vowel ← ∊∘'AEIOU'                                    ⍝ unnamed array comp
    same ← (succ←one∘plus)∘(pred←-∘(one←1))
    that ← {this ⍵}                                     ⍝ mutually recursive
    this ← {that ⍵}                                     ⍝   dfns
       F ← 32∘plus∘(×∘1.8)                              ⍝ Fahrenheit
     inv ← ⍣¯1                                          ⍝ derived operator
       C ← F inv                                        ⍝ → derived fn

    defs'avg'                       ⍝ no-names listing
avg ← +⌿÷≢

⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝ Listing with names

    2 defs ''                       ⍝ all names / all defns
    C ← F inv          
    F ← 32∘plus∘(×∘1.8)
  avg ← sum÷num        
 davg ← {(sum÷num)⍵}   
 digs ← '0123456789'   
  dup ← {⍵ ⍵}          
 dups ← dup each       
 each ← {⍺←⊢ ⋄ ⍺ ⍺⍺¨⍵} 
 fold ← ⌿              
  inv ← ⍣¯1            
 isns ← ns∘≡           
isnum ← ∊∘digs         
  num ← ≢              
  one ← 1              
 plus ← +              
 pred ← -∘one          
 same ← succ∘pred      
 succ ← one∘plus       
  sum ← plus fold      
 that ← {this ⍵}       
 this ← {that ⍵}       
 tups ← tup each       
vowel ← ∊∘'AEIOU'      

    2 defs 'same' 'succ' 'pred'     ⍝ display of specific definitions
same ← succ∘pred
succ ← one∘plus 
pred ← -∘one    

⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝ Top-down indented display:

    1 1 defs 'avg'                  ⍝ top-down defns of "avg"
avg ← sum÷num    
  sum ← plus fold
    plus ← +     
    fold ← ⌿     
  num ← ≢        

    2 1 defs ''                     ⍝ all defns top-down
C ← F inv              
  F ← 32∘plus∘(×∘1.8)  
    plus ← +           
  inv ← ⍣¯1            
avg ← sum÷num          
  sum ← plus fold      
    fold ← ⌿           
  num ← ≢              
davg ← {(sum÷num)⍵}    
dups ← dup each        
  dup ← {⍵ ⍵}          
  each ← {⍺←⊢ ⋄ ⍺ ⍺⍺¨⍵}
isns ← ns∘≡            
isnum ← ∊∘digs         
  digs ← '0123456789'  
same ← succ∘pred       
  succ ← one∘plus      
    one ← 1            
  pred ← -∘one         
tups ← tup each        
vowel ← ∊∘'AEIOU'      
that ← {this ⍵}        
  this ← {that ⍵}      

    avl←{(⍳×/1↓⍴⍵)~⍵×⊃⍺⌷⍺⍺}         ⍝ some more dfn/op defns
    box←{⍵⌿⍵/⍵ ⍵⍴⍳⍵×⍵}
    cmap←{⊂[⍳⍴⍴⍵]1∊¨⍵∘.=⍵}
    emt←{(,⍵=0)/,⍳⍴⍵}
    nxt←{(⍺(⍺⍺ avl)⍵)⊣@(⊂⍺)¨⊂⍵}
    nxtv←{⊃,/⍺∘(⍺⍺ nxt)¨⍵}
    rcb←{(⍳⍵),¨box⊃⍵*÷2}
    sfmt←{⊂[3 4]1 3 2 4⍉(2/(⍴⍵)*÷2)⍴⍵}
    sudoku←{sfmt⊃cmap∘rcb svec ⍵}
    svec←{⊃(⍺⍺⍴⍵)nxtv/(emt ⍵),⊂⊂⍵}

    1 1 defs 'sudoku'               ⍝ top-down display
sudoku ← {sfmt⊃cmap∘rcb svec ⍵}       
  sfmt ← {⊂[3 4]1 3 2 4⍉(2/(⍴⍵)*÷2)⍴⍵}
  cmap ← {⊂[⍳⍴⍴⍵]1∊¨⍵∘.=⍵}            
  rcb ← {(⍳⍵),¨box⊃⍵*÷2}              
    box ← {⍵⌿⍵/⍵ ⍵⍴⍳⍵×⍵}              
  svec ← {⊃(⍺⍺⍴⍵)nxtv/(emt ⍵),⊂⊂⍵}    
    nxtv ← {⊃,/⍺∘(⍺⍺ nxt)¨⍵}          
      nxt ← {(⍺(⍺⍺ avl)⍵)⊣@(⊂⍺)¨⊂⍵}   
        avl ← {(⍳×/1↓⍴⍵)~⍵×⊃⍺⌷⍺⍺}     
    emt ← {(,⍵=0)/,⍳⍴⍵}               

⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝ Bottom-up dependencies:

    2 ¯1 defs 'plus'                ⍝ defns dependent on "plus"
plus ← +             
  F ← 32∘plus∘(×∘1.8)
    C ← F inv        
  succ ← one∘plus    
    same ← succ∘pred 
  sum ← plus fold    
    avg ← sum÷num    

    2 ¯1 defs 'one' 'plus'          ⍝ defns dependent on "one" and "plus"
one ← 1              
  pred ← -∘one       
plus ← +             
  F ← 32∘plus∘(×∘1.8)
    C ← F inv        
  succ ← one∘plus    
    same ← succ∘pred 
  sum ← plus fold    
    avg ← sum÷num    

⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝ changing definitions:

    defs 'plus ← -∘-'               ⍝ redefinition of "plus"

    0 defs 'same'                   ⍝ change to "plus" has changed "same"
same ← 1∘(-∘-)∘(-∘1)

    0 defs defs 'div←÷'             ⍝ new definition "div"
div ← ÷

    1 defs 'avg'                    ⍝ incorporates new defn
avg ← sum div num

⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝ -ive names: tree only

    ¯1 defs ⊂⎕cr'succ'              ⍝ ]box -names=fns
┌─┬────────┐
│1│┌─┬────┐│
│ ││∘│plus││
│ │└─┴────┘│
└─┴────────┘

    ¯2 defs ⊂⎕cr'succ'              ⍝ ]box -names=all
┌───┬────────┐
│one│┌─┬────┐│
│   ││∘│plus││
│   │└─┴────┘│
└───┴────────┘

⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝ edge cases

    ∧/{(0 0⍴'')≡ ⍵ defs'undef'}¨ ,0 1∘., 0 ¯1 1     ⍝ undefined name
1
    avg ← {sum←+⌿⍵ ⋄ sum÷≢⍵}        ⍝ avg with local name "sum"
    1 1 defs 'avg'                  ⍝ top-down display does not include "sum"
avg ← {sum←+⌿⍵ ⋄ sum÷≢⍵}

    (0 0⍴'')≡ 1 1 (⎕ns'')defs ''    ⍝ empty namespace
1

⍝∇ defs in externs

Back to: code

Back to: Workspaces