A little comparison between R and Kap

5 points by Internet_Janitor


kcroarkin

R was one of my first languages and I've recently been obsessed with Dyalog APL. One thing I missed about R/tidyverse when picking up APL was accessing columns by name. It seems like for APL, you either set column indices as constant variables or have a vector of strings you get indices of with (Although I'm still new and there might be a better way). For instance, I do this pattern a lot with inverted tables:

cnames ← 'cx' 'cz' 'vb' 'wvb' 'idx_cnt' 'widx_cnt' 'dirty'
chunk_info ← (≢cnames)⍴⊂⍬
⍝ Chunk column index
Ci ← cnames∘⍳∘⊆

Because of this, it surprised me to see that Kap seems to support column labels here!

purchases ← (>1↑)«labels»(1↓) purchases
┌───────────┬──────┬────────┐
│    country│amount│discount│
├→──────────┴──────┴────────┤
↓      "USA" "2000"     "10"│
...
+/ purchases.amount

Very cool!

As an aside, it's always quite cool seeing what is going on with Kap. Elias does a lot of cool stuff with it and I enjoyed seeing stuff like this also.

slot

Very fun! Here's a version in BQN

CSV ← { [q,s,n]←(""","∾@+10)=⌜𝕩 ⋄ e←≠`q ⋄ sp←(¬e)∧n∨s ⋄ d←sp∨q∧e∨«⊸<q ⋄ (0∾sp/+`(¬e)∧n)⊔(1-˜(¬d)×1+`sp)⊔𝕩 }

[c,a,d]←•ParseFloat⎊⊢¨⍉>1↓CSV•FChars"./purchases.csv" ⋄ p←⊐c
+´a
(⍷c)≍+´¨p⊔a
(⍷c)≍+´¨p⊔a-d
(⍷c)≍+´¨(p⊔a)-○((p⊔{𝕩≤10×(∧⊑˜2⌊∘÷˜≠)𝕩}   a)/¨⊢)(p⊔d)
(⍷c)≍+´¨(p⊔a)-○((  {𝕩≤10×(∧⊑˜2⌊∘÷˜≠)𝕩}¨p⊔a)/¨⊢)(p⊔d)

I think, for these kinds of purposes, it definitely pays to have something like APL's key or K's group; it's not necessarily the most ergonomic with BQN's group (although for something like the CSV parser above I definitely prefer the BQN version above APL or K)