so-dang-cool / dt

dt - duct tape for your unix pipes
https://dt.plumbing/
BSD 3-Clause "New" or "Revised" License
423 stars 12 forks source link

Rank Polymorphism #55

Open booniepepper opened 1 year ago

booniepepper commented 1 year ago

Why not?

Currently these would be errors, but they could easily be rank polymorphic:

» [ 1 2 3 ] 1 + pl
[ 2 3 4 ]
» [ "it's" "too" "quiet" ] upcase pl
[ "IT'S" "TOO" "QUIET" ]
cadrgtsecond commented 9 months ago

Technically this isn't rank polymorphism since dt arrays don't have rank i.e They cannot be multi-dimensional. However they do have depth(i.e nesting). Although rank could be useful for dealing with tabular data

Also this means you will need some kind of rank/depth modifiers as well functions to control it e.g

example ← 10‿10⥊↕100 # Assume this is something useful
┌─                               
╵  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 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 65 66 67 68 69  
  70 71 72 73 74 75 76 77 78 79  
  80 81 82 83 84 85 86 87 88 89  
  90 91 92 93 94 95 96 97 98 99  
                                ┘
# plus-insert
Sum ← +˝

Sum example # Columnwise
⟨ 450 460 470 480 490 500 510 520 530 540 ⟩

Sum⎉¯1 example # Rowwise
⟨ 45 145 245 345 445 545 645 745 845 945 ⟩

(⊑∘Sum∘⥊) example # The total sum
4950

I would recommend reading the J documentation on Rank or spending some time with an array language to get a feel for it

cadrgtsecond commented 9 months ago

IMO rank is perfectly suited for the language. It would finally allow shell invocations to mirror scripts. Shell invocations would just be Array -> Array

booniepepper commented 9 months ago

Thanks for chiming in, the rank vs depth distinction is good to call out. I guess I was thinking more of depth as a proxy for rank

I think a proper rank really only makes sense when getting into math operations, so I might save it for later. I don't know what rank would mean for text processing, for example, and that's more of dt's bread and butter