-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lazy.dyalog
64 lines (59 loc) · 2.66 KB
/
Lazy.dyalog
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
:Namespace Lazy ⍝ V 1.03
⍝ True lazy (late-binding) editable, SALTable, ⎕FIXable, Linkable definition
⍝ Allows defintion of arrays and tacit functions
⍝ 2018 06 07 Adam: Initial definition
⍝ 2023 03 24 Adam: Link integration
⎕IO←1 ⋄ ⎕ML←1
∇ r←List
(r←⎕NS ⍬).(Name Group Desc Parse)←'Lazy' 'Tools' 'Lazily evaluated (late-binding) definition' ''
∇
∇ r←Run(cmd input);T;nl;∆NR;id;rx;a;nr
T←##.THIS
∆NR←']',cmd,' ',∘⊃1↓T.⎕NR
id←a/⍨¯1≠⎕NC⍪a←⎕UCS⍳255+1113856×{6::80=⎕DR'' ⋄ ⎕SE.SALTUtils.UNICODE}⍬
⍝ ∇ name ← name ⋄ [1] name ←def ⋄ [2] SALT tag? ⋄ ∇
rx←'^ *∇ *([',id,'][',id,'\d]*) *← *\1 *\r\[1\] *\1 *←.*\r(\[2\] *⍝∇.*\r)? *∇ *$'
LooksRight←0∊rx ⎕S 0⍠'Mode' 'D'∘T.⎕VR
:If ' '∧.=input ⍝ show all defs
nl←T.⎕NL ¯3.1
nl/⍨←1 0 0∘≡¨⊣/T.⎕AT nl
nl/⍨←LooksRight¨nl
r←↑∆NR¨nl
:Else
:Select T.⎕NC⊂,input
:Case 0 ⍝ open editor
r←'* Undefined!'
:CaseList 3.1 ⍝ show definition
:If LooksRight input
r←∆NR input
:Else
r←'* Not a lazy definition!'
:EndIf
:Case ¯1 ⍝ define
nr←,⊂(⊃,'←',⊃)'[^←]+'⎕S'&'⊢input
nr,←⊂{6::⍵ ⋄ (⍳∘' '↓⊢)##.##.msg}input
:If ∨/input⍷⍨T{0::⍺.⎕FX ⍵ ⋄ ∊⍺ ⎕SE.Link.Fix ⍵}nr ⍝ success
r←⍬⊤⍬
:Else ⍝ failure
r←'* Invalid definition!'
:EndIf
:Else ⍝ fail
r←'* Already not a lazy definition!'
:EndSelect
:EndIf
∇
∇ r←level Help cmd;c
c←' ]',cmd,' '
r←,⊂'Creates and reports lazily evaluated (late-binding) definitions.'
r,←⊂'This allows SALTing, ⎕FIXing, and commenting both arrays and tacit functions. Note that operators are not supported.'
r,←⊂c,' ⍝ list all lazy definitions'
r,←⊂c,'fn ⍝ show lazy definition'
r,←⊂c,'fn←code ⍝ define lazily'
:If ×level
r,←'' 'Examples:'
r,←⊂c,'Avg←Sum÷1⌈≢ ⍝ allowed even though Sum is not defined yet'
r,←⊂c,'Sum←+⌿'
r,←⊂c,'matrix←3 4⍴⍳12 ⍝ formula will be kept rather than executed, so this array is dynamically ⎕IO-dependent'
:EndIf
∇
:EndNamespace