310 likes | 333 Views
APL and OO. Morten Kromberg Technical Director, Dyalog Ltd. Why OO in APL (MUST we)?. Everyone else is doing it Microsoft says we MUST Terrorists will attack my system unless it is running as ”managed” code. Good Reasons for Wanting OO.
E N D
APL and OO Morten Kromberg Technical Director, Dyalog Ltd. APL and OO
Why OO in APL (MUST we)? • Everyone else is doing it • Microsoft says we MUST • Terrorists will attack my system unless it is running as ”managed” code APL and OO
Good Reasons for Wanting OO • Because Object Orientation is a Valuable Tool of Thought! • We will be able to Share Tools and Components more Easily • It will help us Manage Complexity in all Project Phases APL and OO
Agenda • Introduce Key OO Concepts • Demonstrate the Benefits • Show that APL can Profit from OO ... without losing it’s Soul APL and OO
Some History... Back in the old days, in 1962, A feller named Ken Iverson decided what to do. He gathered all the papers he'd been writing for a spell And he put them in a little book and called it APL. (first verse of ”APL Blossom Time”) APL and OO
Meanwhile, in Oslo... 3 3 0 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 2 1 APL and OO
In Oslo... SIMULA represents an effort to [describe complex systems as] discrete-event networks, that is, where the flow may be thought of as being composed of discrete units demanding service at discrete service elements, and entering and leaving the elements at definite moments [sic] of time. Examples of such systems are ticket counter systems, production lines, production in development programs, neuron systems, and concurrent processing of programs on computers. Kristen Nygaard, Norwegian Computing Centre, ca. 1962 APL and OO
Sons of Norway... Arrays: SALES←10 11 12 13 15 (+/SALES)÷⍴SALES Functions: {(+/⍵)÷⍴⍵} SALES+/ % # SALES Kristen Nygaard1926-2002 Kenneth E. Iverson1920-2004 Objects: q←⎕NEW Queue q.Join ⎕NEW Customer 'Jill' q.Join ⎕NEW Customer 'Jack' q.Length 1 APL and OO
Stop Me Any Time! APL and OO
Queue Class :Class Queue :Field Public History←⍬ Customers←⍬ ⍝ Private Field ∇ r←Length :Access Public r←1⊃⍴Customers ∇ ∇ Join Customer :Access Public Customers←Customers,⊂Customer,(3⊃⎕AI),1+⍴Customers :If 1=⍴Customers ⋄ Serve&0 ⋄ :EndIf ⍝ Start Server ∇ ∇ Serve dummy;t;elapsed ⍝ Start serving queue in new thread when queue length grows to 1 ⍝ Stop when queue is empty. :Repeat ⎕DL 9+?11 ⍝ Processing takes between 10 and 20 seconds elapsed←(3⊃⎕AI)-1 2⊃Customers ⍝ Total time spent in the queue History,←⊂(elapsed,1⊃Customers)[2 1 4] ⍝ History records: Customer, Total Time, Queue Length Customers←1↓Customers ⍝ The customer has left the building :Until 0=⍴Customers ∇ :EndClass ⍝ Class Queue APL and OO
Queue Class aQueue←⎕NEW Queue :Class Queue :Field Public History←⍬ Customers←⍬ ∇ r←Length :Access Public r←1⊃⍴Customers ∇ ∇ Join Customer :Access Public Customers←Customers, ⊂Customer,(3⊃⎕AI),1+⍴Customers :If 1=⍴Customers ⋄ Serve&0 ⋄ :EndIf ∇ ∇ Serve dummy;t;elapsed ⍝ Start serving queue in new thread when queue length grows to 1 ⍝ Stop when queue is empty. :Repeat ⎕DL 9+?11 ⍝ Processing takes between 10 and 20 seconds elapsed←(3⊃⎕AI)-1 2⊃Customers ⍝ Since customer entered queue History,←⊂(elapsed,1⊃Customers)[2 1 4] ⍝ History records (Cust#, Total Time, Initial Queue Length) Customers←1↓Customers ⍝ Customer has left the building :Until 0=⍴Customers ∇ :EndClass ⍝ Class Queue APL and OO
Benefit:Object Orientation + No Type Declarations= Easy Prototyping: :Class Customer :Field Public CashRequired←60 ⍝ Amount of Cash Required :Field Public SkillLevel←5 ⍝ Average Skills (1-10) :Field Public Sneaky←0 ⍝ Will try to sneak ∇ make args :Access Public :Implements Constructor (CashRequired SkillLevel Sneaky)←args,(⍴,args)↓CashRequired SkillLevel Sneaky ⎕DF '[Customer wanting ',(⍕CashRequired),']' ⍝ Display Form ∇ :EndClass ⍝ Customer APL and OO
Information Hiding Specificationshave changed... A new kind of queue... Benefit: ”Information Hiding” = Easy Maintenence APL and OO
Key OO Concepts • ”Encapsulation” • Related code and data are together • ”Information Hiding” • Implementor decides what to make public • Only public behavior is guaranteed • User can rely on ”upwards compatibility” of public interface APL and OO
TimeSeries Class :Class TimeSeries :Field Public Shared DefaultDegree←3 ⍝ 3rd degree polynomials by default :Field Public Obs ⍝ Observed values :Field Public Degree ⍝ Degree of polynominals to fit ∇ r←PolyFit x;coeffs :Access Public ⍝ Fit polynomial to observed points to compute values at x coeffs←Obs⌹(⍳⍴Obs)∘.*0,⍳Degree ⍝ Find polynomial coeffs r←coeffs+.×⍉x∘.*0,⍳Degree ⍝ Compute fitted f(x) ∇ ∇ make args :Access Public :Implements Constructor Obs←args Degree←DefaultDegree ⍝ Pick up default from Class ∇ :EndClass ⍝ Class TimeSeries APL and OO
Inheritance Benefit: Easy & SafeCode Sharing APL and OO
Summary of Key Concepts • Method is the OO name for a function • Field is the OO name for a variable • Properties look like Fields, but are implemented by Get [and Set] functions • Constructors initialise instances • Destructors clean up APL and OO
Public vs. Private • Private members can only be seen by code which is inside the class • Public members can be seen from inside and outside APL and OO
Shared vs. Instance • Shared members are shared by all instances • They can be used directly from the class without first creating an instance • Instance members have different content from one instance to the next • Compiled languages often use the name Static for Shared APL and OO
Inheritance • One class can Extend another • This makes code sharing both easier and safer APL and OO
New ⎕names • ⎕NEW creates an instance of a class • ⎕CLASS returns the class of an instance • ⎕INSTANCES returns the list of instances of a class • ⎕THIS returns a reference to the current instance • ⎕BASE refers to a name in a base class APL and OO
When NOT to OO APL and OO
We Want: APL Arrays Functions Objects 3 3 2 1 APL and OO
DotNet • *IS* Important • C# has been our Design Guide. • Any v11 class can beused by .Net languages • Managed Code ”Event Horizon” ~4/5 years • But we want OO under Windows 32&64, Unix,Linux, and on Pocket PC APL and OO
To Do List • Unicode (including APL session) • High Precision Arithmetic (& Complex?) • ”Structs”: Classes with Fixed Structure :Class Employee :Field Public FirstName←'' :Field Public LastName←'’ :Field Public Salary←0 :EndClass • DotNet ”Managed Code” (”Interop” available since version 10) APL and OO
For More Information • See Dyalog APL version 11. • Tutorial ”Introduction to OO for APLers” will be ”serialised” in Vector • Watch http://www.vector.org.uk • Subscribe to Vector! • Google...! APL and OO
Thanks to... Kristen Pete Ken John Scholes Geoff Streeter ... And many, many more ... John Daintree APL and OO
Refactoring SQAPL/APL+LinkPro ∇ Test_Old [1] SQAConnect 'C1' 'MS Access Database' ('DriverOptions' 'DBQ=C:\temp\sqaoo.mdb') [2] SQAPrepare 'C1.S1' 'SELECT * FROM EMPLOYEES WHERE FirstName LIKE :<C10:' [3] SQAExec 'C1.S1' 'D%’ [4] SQAFetch 'C1.S1' ('Columnwise' 1) [5] SQAClose 'C1' ∇ ∇ Test_New;Connection;Sel [1] Connection←⎕NEW SQA.Connection ('MS Access Database' 'DBQ=C:\temp\sqaoo.mdb') [2] Sel←Connection.Prepare 'SELECT * FROM EMPLOYEES WHERE FirstName LIKE :<C10:' [3] Sel.Columnwise←1 [4] Sel.Execute 'D%‘ [5] Sel.Fetch 1 ∇ APL and OO
Refactoring SQASet fns ∇ TestSet_Old;z [1] SQAConnect 'C1' 'MS Access Database’ ('DriverOptions' 'DBQ=C:\temp\sqaoo.mdb') [2] z←2⊃SQASetPrepare 'C1.S1' 'SELECT ID,SALARY FROM EMPLOYEES' [3] z[;2]×←1+0.1×z[;2]>1000 ⍝ 10% Salary Increase for the Rich [4] SQASetExec'C1.S1' z [5] SQAClose'C1.S1' [6] SQAClose'C1' ∇ ∇ TestSet_New;Connection;Set;z [1] Connection←⎕NEW SQA.Connection ('MS Access Database' 'DBQ=C:\temp\sqaoo.mdb') [2] Set←⎕NEW SQA.Set (Connection 'Select ID,SALARY FROM EMPLOYEES') [3] z←Set.Data ⋄ z[;2]×←1+0.1×z[;2]>1000 ⍝ 10% Salary Increase ... [4] Set.Exec z ∇ APL and OO
∇ r←SetExec ctl;cur;⎕IO;c;d;i;m;n;s;t;rcds;data;table ;types;cols;keys;dk;rd [1] ⍝ Execute changes required to update Data Set [2] ⍝ Return 0 (#updates, #inserts, #deletes) [3] [4] ⎕IO←1 [5] cur rcds←ctl ⋄ n←0 0 0 [6] →((⍴d)<i←(d←1⊃⍙SQASets)⍳⊂cur)⍴e1 ⋄ t←(s←1+i)⊃⍙SQASets [7] table cols types keys data←t [8] [9] dk←⊂[2]data[;keys] ⋄ rd←⊂[2]rcds[;keys] [10] →(0=n[3]←+/m←~dk∊rd)⍴l1 [11] →(0≠1⊃r←SetDelete cur(m⌿data[;keys]))⍴0 [12] m←~m ⋄ data←m⌿data ⋄ dk←m/dk [13] [14] l1:→(0=n[2]←+/m←(1⊃⍴data)<d←dk⍳rd)⍴l2 [15] →(0≠1⊃r←SetInsert cur(m⌿rcds))⍴0 [16] m←~m ⋄ rcds←m⌿rcds ⋄ rd←m/rd [17] [18] l2:data←data[dk⍳rd;] [19] →(0=n[1]←+/m←~∧/rcds≡¨data)⍴l9 [20] →(0≠1⊃r←SetUpdate cur(m⌿rcds))⍴0 [21] l9:r←0 n ⋄ →0 [22] [23] e1:r←90003 'Set does not exist' ⋄ →0 ∇ ∇ r←Exec rcds;c;d;i;m;n;s;t;dk;rd;data [1] :Access Public [2] ⍝ Execute changes required to update Data Set [3] ⍝ Return 0 (#updates, #inserts, #deletes) [4] [5] n←0 0 0 [6] data←DATA [7] dk←⊂[2]data[;Keys] ⋄ rd←⊂[2]rcds[;Keys] [8] →(0=n[3]←+/m←~dk∊rd)⍴l1 [9] →(0≠1⊃r←Delete(m⌿data[;Keys]))⍴0 ⍝ Delete from set [10] m←~m ⋄ data←m⌿data ⋄ dk←m/dk [11] [12] l1:→(0=n[2]←+/m←(1⊃⍴data)<d←dk⍳rd)⍴l2 [13] →(0≠1⊃r←Insert(m⌿rcds))⍴0 [14] m←~m ⋄ rcds←m⌿rcds ⋄ rd←m/rd [15] [16] l2:data←data[dk⍳rd;] [17] →(0=n[1]←+/m←~∧/rcds≡¨data)⍴l9 [18] →(0≠1⊃r←Update(m⌿rcds))⍴0 [19] l9:r←0 n ⋄ →0 ∇ APL and OO
∇ r←SQASetUpdate ctl;cur;⎕IO;c;d;i;j;m;s;t;rcds;data ;table;cols;keys;data;stmt;z [1] ⍝ Update records in a Data Set [2] [3] ⎕IO←1 [4] cur rcds←ctl [5] →((⍴d)<i←(d←1⊃⍙SQASets)⍳⊂cur)⍴e1 ⋄ t←(s←1+i)⊃⍙SQASets [6] table cols types keys data←t [7] [8] j←(⍳⍴cols)~keys [9] →(∨/m←(1⊃⍴data)<d←(↓data[;keys])⍳↓rcds[;keys])⍴e2 [10] [11] t←cols,¨'=',¨types [12] stmt←5↓⊃,/(⊂' AND '),¨cols[keys],¨'=',¨types[keys] [13] stmt←'UPDATE ',table,' SET ',(1↓,⊃,/',',¨t[j]) [14] stmt←stmt,' WHERE ',stmt [15] [16] r←ExecDirect(c←cur,'Update')stmt(rcds[;j,keys])[17] z←Close c [18] →(0<1⊃r)⍴0 [19] [20] data[d;]←rcds [21] ((s,5)⊃⍙SQASets)←data [22] r←,0 ⋄ →0 [23] [24] e1:r←90003 'Set does not exist' ⋄ →0 [25] e2:r←90005('Keys not in set: ',,' ',⍕m⌿rcds[;keys]) ∇ ∇ r←SQA.Set.Update rcds;d;m;j;t;stmt [1] ⍝ Update records in a Data Set [2] [3] j←(⍳⍴Cols)~Keys [4] →(∨/m←(1⊃⍴DATA)<d←(⊂[2]DATA[;Keys])⍳⊂[2]rcds[;Keys])⍴e2 [5] [6] t←Cols,¨'=',¨Types [7] stmt←5↓⊃,/(⊂' AND '),¨Cols[Keys],¨'=',¨Types[Keys] [8] stmt←'UPDATE ',Table,' SET ',(1↓,⊃,/',',¨t[j]) [9] stmt←stmt,' WHERE ',stmt [10] [11] r←Connection.ExecDirect stmt(rcds[;j,Keys]) [12] →(r.State≡'Error')⍴e1 [13] DATA[d;]←rcds [14] r←,0 ⋄ →0 [15] [16] e1:(⍕Connection.GetWarning)⎕SIGNAL 90001 [17] e2:('Keys not in set: ',,' ',⍕m⌿rcds[;Keys])⎕SIGNAL ∇ APL and OO