Follow

It's now possible to write real code in KAP. I've implemented the 2D pretty-printer in KAP itself, and the main code that implemented the formatting is 4 lines of code (compared to at least 100 in Kotlin).

I think @snowyfox wanted to see what the final code looks like (well, this is the compressed version, the real one has comments).

Here's example output from the formatter:

output:prettyPrint 2 2 ⍴ 1 12 123 (2 2 ⍴ ⍳4)
┏━━━━━━━━━┓
┃ 1 12┃
┃123 ┏━━━┓┃
┃ ┃0 1┃┃
┃ ┃2 3┃┃
┃ ┗━━━┛┃
┗━━━━━━━━━┛

And here is the code that does the formatting:

∇ wrapBox (v) {
w ← (⍴v)[1]
{((⊂"┏") , (w ⍴ ⊂"━") , ⊂"┓"),[0]⍵,[0](⊂"┗") , (w ⍴ ⊂"━") , ⊂"┛"} (⊂"┃") , v , ⊂"┃"
}

∇ render2d (v) {
w ← ((⍴v) ⍴ ⌈/[0] {(⍴⍵)[1]}¨v) {((↑⍴⍵) (⍺-(⍴⍵)[1]) ⍴ (⊂" ")),⍵}¨ v
wrapBox ⊃ ,[0]/ {⍺,(⊂" "),⍵}/ (⍉ (⍴w) ⍴ ⌈/[1] {↑⍴⍵}¨w) {⍵ ,[0] ((⍺-↑⍴⍵) ((⍴⍵)[1])) ⍴ (⊂" ")}¨w
}

@loke ...I think I like the commented version better

Sign in to participate in the conversation
Functional Café

The social network of the future: No ads, no corporate surveillance, ethical design, and decentralization! Own your data with Mastodon!