blob: d5b3d355d358de8c5ca0db545b459bbe6adef47c (
plain)
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
|
# Format an array to a character matrix
{
F0←𝔽 # Function to format a scalar
lf←'
'
# Vertical padding for arrays of rank greater than 2
PadV ← {
# Leading shape
ls ← ¯1↓≢𝕩
# Empty lines after each row: 1 if it's at the end of a 2-cell, plus
# 1 if it's at the end of a 2-cell and a 3-cell, and so on
p ← ⥊ +⎉¯1‿∞´ ×⌜˜`⌾⌽ (-1⌈ls)↑¨1
# But none at the very end
p ↩ 0⌾(¯1⊸⊑) p
Pad ← {i←/1+𝕨 ⋄ (¯1¨⌾((¬∊i)⊸/)i) ⊏ 𝕩∾(¯1⊑≢𝕩)⥊" "}
p (⊑0∊ls)◶⟨Pad,+´⊸↑⟩ ((×´ls)∾¯1⊑≢𝕩) ⥊ 𝕩
}⍟(2 < =)
# Horizontal padding: just some spaces on either side
PadH ← {
(𝕨/" ") (∾⎉1∾⎉1⊣) 𝕩
}
Pad ← PadH⟜PadV
Enf0 ← { ≍"⟨"∾(¯1↓1↓⊏𝕩)∾"⟩" }
Enframe ← {(1≠𝕨)∨(1≠≠𝕩)∨⊑2∊+`-˝"⟨⟩"=⌜⊏𝕩}◶Enf0‿{
l ← ¯1 ⊑ ≢𝕩
∾ ⟨ # "┼╒╛╪"
1‿l↑∾⟨"┌",(5⊸<)◶⟨⥊"·─"⊏˜1⌊⊢,F0⟩𝕨⟩
((4⌊0⌈𝕨-1)⊑"·╵╎┆┊")⌾⊑ 𝕩
(1∾-l)↑"┘"
⟩
}
FmtEmpty ← (0‿0≢≢)◶("┌┐"≍"└┘")‿(((2≠=)∨0=≠)◶{
'┐'⌾(0‿¯1⊸⊑) 2 Enframe 1 PadH " "¨𝕩
}‿{
≍"⟨⟩"∾˜(1<≠)◶⟨"",'⥊'⌾(¯1⊸⊑)·∾·∾⟜"‿"¨F0¨⟩≢𝕩
})
PaddingJoin ← {1 PaddingJoin 𝕩}⊘{
s ← ≢¨ 𝕩
w ← (0<=)◶⟨⥊,⌈˝⍟(=-1˜)⟩1⊑¨s
h ← ⌈˝⎉1⍟(0<=) ⊑¨s
∾⎉2 ≍⍟(0⌈2-=) (h ∾⌜ 𝕨×w¬(-𝕨×≠w)↑1) ↑¨ 𝕩
}
FmtMixed ← {
(=𝕩) Enframe 2 Pad 𝕨 PaddingJoin F¨𝕩
}
F ← (2⌊≡)◶(≍≤⟜∞◶⟨"'"⊸(∾∾⊣),F0⟩)‿{
num ← 𝕩≤∞ ⋄ r ← =𝕩
((≠(0⊸<+≤)+´)⥊num)◶{
# All characters
k ← ∞⍟(0⊸=) -≠ c ← ¯1↓≢𝕩
q ← '"'
𝕩 ↩ (1+q=⊢)⊸/⍟(1==) 𝕩
(r Enframe 1 PadH PadV)⍟(1≠r) ≍ (c↑q) ∾⎉k 𝕩 ∾⎉k ⌽⌾⥊c↑q
}‿{
# Not homogeneous, or empty
(∨´0=≢)◶FmtMixed‿FmtEmpty 𝕩
}‿{
# All numbers
¯1 FmtMixed 𝕩
} 𝕩
}‿FmtMixed
¯1↓⥊ ∾⟜lf˘ F 𝕩
}
|