aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMarshall Lochbaum <mwlochbaum@gmail.com>2020-10-26 13:06:45 -0400
committerMarshall Lochbaum <mwlochbaum@gmail.com>2020-10-26 13:06:45 -0400
commitb1e428165cd16f854bfc32b3467dfe915e2b762b (patch)
treea9976d32f445b9afdef8bc7cddd4f1a40b83831e /src
parent61594acdad5f3c2cb6472a6ec6d41b48b4266861 (diff)
Fix formatter on some mixed rank>2 arrays
Diffstat (limited to 'src')
-rw-r--r--src/fmt.bqn24
1 files changed, 14 insertions, 10 deletions
diff --git a/src/fmt.bqn b/src/fmt.bqn
index d1e593ed..3234d590 100644
--- a/src/fmt.bqn
+++ b/src/fmt.bqn
@@ -4,25 +4,28 @@
FmtAtom ← ≍ <⟜@◶⟨@⊸≠◶⟨"@","'"⊸(∾∾⊣)⟩,F0⟩
# Vertical padding for arrays of rank greater than 2
- PadV ← {
- # Leading shape
- ls ← ¯1↓≢𝕩
+ PadCount ← {
# 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
+ 0⌾(¯1⊸⊑) ⥊ 𝕨 +⎉¯1‿∞´ ×⌜˜`⌾⌽ (-𝕩)↑¨1
+ }
+ PadV ← {
+ # Leading shape and padding count
+ p ← PadCount 1 ⌈ ls ← ¯1↓≢𝕩
# If 𝕩 has cells, pad by selection; if it's empty, there are only
# pads but selection would try to get cells (1⌈ above), so overtake.
Pad ← {(»⊸<⊸×/0∾1+𝕨) ⊏ ' '¨∘⊏⊸∾𝕩}
p (0<≠∘⊢)◶⟨+´⊸↑,Pad⟩ ((×´ls)∾¯1↑≢𝕩) ⥊ 𝕩
}⍟(2 < =)
+ PadVMixed ← {
+ # PadV, but with 2-cells enclosed: they might have different lengths
+ ∾ (1 PadCount ≢𝕩) (⊢∾∾⟜(¯1⊑≢)⥊' '˙)¨ ⥊𝕩
+ }
# Horizontal padding: just some spaces on either side
PadH ← { s←⟨≠𝕩,𝕨⟩⥊' ' ⋄ ∾≍⟨s,𝕩,s⟩ }
- Pad ← PadH⟜PadV
-
# Add a frame to padded data
Enframe ← ∨○(1⊸≠)⟜≠◶{∨´2=+`-˝"⟨⟩"=⌜⊏𝕩}‿1◶{
# One-line version
@@ -43,15 +46,16 @@
≍"⟨⟩"∾˜(1<≠)◶⟨"",'⥊'⌾(¯1⊸⊑)·∾·∾⟜"‿"¨F0¨⟩≢𝕩
})
- PaddingJoin ← {1 PaddingJoin 𝕩}⊘{
+ PaddingJoin ← {
s ← ≢¨ 𝕩
w ← (0<=)◶⟨⥊,⌈˝⍟(=-1˙)⟩1⊑¨s
h ← ⌈˝⎉1⍟(0<=) ⊑¨s
- ∾⎉2 ≍⍟(0⌈2-=) (h ∾⌜ 𝕨×w¬(-𝕨×≠w)↑1) ↑¨ 𝕩
+ o ← <∘∾⎉2 ≍⍟(0⌈2-=) (h ∾⌜ 𝕨×w¬(-𝕨×≠w)↑1) ↑¨ 𝕩
+ 2 PadH (1⊸⌽⊸≡⥊h)◶⟨PadVMixed,PadV∘>⟩ o
}
FmtMixed ← {
- (=𝕩) Enframe 2 Pad 𝕨 PaddingJoin Fmt¨𝕩
+ (=𝕩) Enframe (𝕨⊣1) PaddingJoin Fmt¨𝕩
}
FmtSimple ← (≠(0⊸<+≤)+´)∘(⥊<@˙)◶{ # Depth 1