Community/Conference2012/Talks/Dyalog Adoption of J Ideas

From J Wiki
Jump to navigation Jump to search
Dyalog APL/W Conference 2011 Unicode Edition
Serial No : 000000
Tue Jul 24 16:22:29 2012
clear ws

c:\demos\jconf saved Tue Jul 24 14:32:02 2012

      ⍝ John Scholes - Dyalog Adoption of J ideas

      ⍝ Roger's influence!

      ⍝ Experiment: 3-trains (f g h) implement "forks":

      ⍝     (f g h) ⍵  →  (  f ⍵) g (  h ⍵)   ⍝ monadic case
      ⍝   ⍺ (f g h) ⍵  →  (⍺ f ⍵) g (⍺ h ⍵)   ⍝ dyadic case

      (+/ ÷ ⍴) 1 2 3 4        ⍝ monadic fork
2.5
      avg ← +/ ÷ ⍴            ⍝ naming the fork
      avg 1 2 3 4             ⍝ applying the fork
2.5
      6(+,×)2                 ⍝ dyadic fork
8 12
      avg dft 1               ⍝ display of fork
 ┌─┼─┐
 / ÷ ⍴
┌┘
+
      ⍝ Longer trains:

      (+ , - , × , ÷) dft 1   ⍝ display of function train
┌─┼───┐
+ , ┌─┼───┐
    - , ┌─┼─┐
        × , ÷

      6 (+,-,×,÷) 2           ⍝ A "vector of functions" !
8 4 12 3

      125 {⍺*÷⍵} 3            ⍝ {...} direct definition "D-fns"
5
      ⍝ "constant functions" can be handy:

      88 {10} 99              ⍝ {10} constant function,
10
      2({10}×⍴)3              ⍝ fgh:     10    × (2⍴3)
30 30
      2(10×⍴)3                ⍝ Agh: ⍺(A f h)⍵ → A f(⍺ h ⍵)
30 30
       (10×⍳)4                ⍝ Agh:  (A f h)⍵ → A f(  h ⍵)
10 20 30 40

      2(⌽10↑)⎕a               ⍝ fBh: ⍺(f B h)⍵ → ⍺ f(B h ⍵)
CDEFGHIJAB
      (⌽10↑)⎕a                ⍝ fBh:  (f B h)⍵ →   f(B h ⍵)
JIHGFEDCBA
      f10 ← ⊢10⊣              ⍝ fork returns 10
      f10¨ ⍳4
10 10 10 10
      d10←{10}                ⍝ constant dfn returns 10.

      cmpx'd10 99' 'f10 99'   ⍝ comparison of expr timings.
  d10 99 → 9.5E¯7 |   0% ⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕
  f10 99 → 4.0E¯7 | -58% ⎕⎕⎕⎕⎕⎕⎕⎕

      ⊃ (,'-',)/ 'hello'      ⍝ fBh / ...
h-e-l-l-o

      ⍝ Summary of 3-train definitions:
      ⍝
      ⍝ Monadic                   Dyadic
      ⍝ (f g h)⍵ → (f ⍵)g(h ⍵)    ⍺(f g h)⍵ → (⍺ f ⍵)g(⍺ h ⍵)
      ⍝ (A g h)⍵ → A g(h ⍵)       ⍺(A g h)⍵ → A g(⍺ h ⍵)
      ⍝ (f B h)⍵ → f(B h ⍵)       ⍺(f B h)⍵ → ⍺ f(B h ⍵)

      ⍝ How about 2-trains?
      ⍝ Dyalog already does hooks with relative ease:
      ⍝
      ⍝     ⍺ f∘g ⍵  → ⍺ f(g ⍵)   ⍝ Dyalog: dyadic hook
      ⍝       f∘g⍨ ⍵ → ⍵ f(g ⍵)   ⍝ Dyalog: monadic hook
      ⍝
      ⍝   f hook g  ←→  f∘g⍨⍨     ⍝ Dyalog: ambi-valent hook
      ⍝
      ⍝ So, in this experiment, ⍺(f g)⍵ means: f atop g:
      ⍝
      ⍝   ⍺(f g)⍵ → f(⍺ g ⍵)      ⍝ dyadic 2-train: atop
      ⍝    (f g)⍵ → f(  g ⍵)      ⍝ monadic 2-train: compose

      2(⍪⍴)3              ⍝ →  ⍪(2⍴3) "column atop reshape"
3
3
      6(  +,-,×,÷)2       ⍝ "vector of functions" from above
8 4 12 3
      6(⌽ +,-,×,÷)2       ⍝ reverse atop vector of functions
3 12 4 8

      ⍝ ... but Roger has since talked me out of this,
      ⍝ so 2-trains will initially be a SYNTAX ERROR.

      ⍝ atop _can_ be expressed as a (nasty) fork:

      2(⍪⍴)3              ⍝ 2-train: col atop reshape
3
3
      2(⊢ ⊢∘⍪ ⍴)3         ⍝    fork: col atop reshape
3
3
      ⍝ hook _can_ be expressed as a (nasty) fork:

       (⊣ + ⊢∘÷)4         ⍝   (f hook g) ⍵ → ⍵ f (g ⍵)
4.25
      2(⊣ + ⊢∘÷)4         ⍝ ⍺ (f hook g) ⍵ → ⍺ f (g ⍵)
2.25

      ⍝ Syntactically possible 3-train forms: fgh Agh fBh
      ⍝ Syntactically possible 2-train forms: fg  Ag
      ⍝ Define the Ag form to be "left-argument currying"
      ⍝
      ⍝   (A g)⍵ → A g ⍵          ⍝ left argument currying

      next ← 1+                   ⍝ successor function
      next 3
4
      ⍝ "Right-operand currying" for dyadic operators:

      inv ← ⍣¯1                   ⍝ ... to the power ¯1

      next inv 3                  ⍝ predecessor function
2
      fixpt ← ⍣≡                  ⍝ ... until match

      1 +∘÷ fixpt 1               ⍝ Phi ("Golden Mean")
1.618033989

      ⍝ Combinations and more examples

      f ← 32 + 1.8 ×              ⍝ Fahrenheit from Celsius
      f dft 1                     ⍝ parse of train
┌─┴──┐
32 ┌─┼───┐
   + 1.8 ×

      f ¯273 ¯40 0 100            ⍝ some familiar temps.
¯459.4 ¯40 32 212

      c ← f inv                   ⍝ Celsius from Fahrenheit
      c ¯459.4 ¯40 32 212
¯273 ¯40 0 100

      ⎕fx'z←tavg v' 'z←(+/v)÷⍴v'      ⍝ Tradfn average
      davg ← {(+/⍵)÷⍴⍵}               ⍝ Dfn average
      favg ← +/÷⍴                     ⍝ Fork average

      cmpx'tavg 1 2' 'davg 1 2' 'favg 1 2'  ⍝ time comparison
  tavg 1 2 → 3.4E¯6 |   0% ⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕
  davg 1 2 → 2.7E¯6 | -22% ⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕
  favg 1 2 → 1.7E¯6 | -52% ⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕

      ⍝ Other primitive operators ...
      ⍝ for which we have only models at present.
      ⍝ http://dfns.dyalog.com

      ⍝ Rank ⍤
      )copy dfns rank
c:\Home\dfns saved Mon Jul 16 13:20:36 2012

      (2 2⍴1 2 2 1) ⍉rank 1 2 ⊢ 2 3 4⍴⍳99
 1  2  3  4
 5  6  7  8
 9 10 11 12
 0  0  0  0

13 17 21  0
14 18 22  0
15 19 23  0
16 20 24  0

      ⍝ Key ⌸
      )copy dfns key
c:\Home\dfns saved Mon Jul 16 13:20:36 2012

      'abracadabra' +/ key 1
5 2 2 1 1
      {(⊃⍵),⍴⍵}key ⍨ 'abracadabra'
 a 5  b 2  r 2  c 1  d 1

      ⍝ Merge ⍈
      )copy dfns merge
c:\Home\dfns saved Mon Jul 16 13:20:36 2012

      mat←10⊥¨⍳4 5                    ⍝ matrix of numbers.

      0 ((2 3)(2 4)merge'∘') mat      ⍝ 0s merged at specific coords
11 12 13 14 15
21  0 23  0 25
31  0 33  0 35
41 42 43 44 45

      0 {0=3|⍵}merge'∘' ⊢ mat         ⍝ 0s merged every 3 items
11  0 13 14  0
 0 22 23  0 25
31 32  0 34 35
41  0 43 44  0

      ⍝ Oh, and tally:  ≢⍵ → length of leading axis (scalar).

      ⍝ That's All Folks ...
      )off