diff options
author | Mikolaj Konarski <mikolaj.konarski@gmail.com> | 2025-04-26 00:25:28 +0200 |
---|---|---|
committer | Mikolaj Konarski <mikolaj.konarski@gmail.com> | 2025-04-26 00:25:35 +0200 |
commit | 25d337c1b34192fedcd2496fe179acbd2051dd30 (patch) | |
tree | fd467d1d0d36153721dc865e31088abee028eda1 /src/Data/Array/Nested/Internal/Mixed.hs | |
parent | 4087d405b51cf32363cb7507df6ffe1a170c0f7f (diff) |
Show concisely arrays replicated from a single element (scalar or not)
Diffstat (limited to 'src/Data/Array/Nested/Internal/Mixed.hs')
-rw-r--r-- | src/Data/Array/Nested/Internal/Mixed.hs | 20 |
1 files changed, 17 insertions, 3 deletions
diff --git a/src/Data/Array/Nested/Internal/Mixed.hs b/src/Data/Array/Nested/Internal/Mixed.hs index 5730354..53b4f05 100644 --- a/src/Data/Array/Nested/Internal/Mixed.hs +++ b/src/Data/Array/Nested/Internal/Mixed.hs @@ -197,9 +197,17 @@ newtype ShowViaToListLinear sh a = ShowViaToListLinear (Mixed sh a) instance (Show a, Elt a) => Show (ShowViaToListLinear sh a) where showsPrec d (ShowViaToListLinear arr) = showParen (d > 10) $ - -- TODO: to avoid ambiguity, this should type-apply the shape to mfromListLinear - showString "mfromListLinear " . shows (shxToList (mshape arr)) . showString " " - . shows (mtoListLinear arr) + let defaultResult = + -- TODO: to avoid ambiguity, this should type-apply the shape to mfromListLinear + showString "mfromListLinear " . shows (shxToList (mshape arr)) . showString " " + . shows (mtoListLinear arr) + in if stridesAreZero (shxLength $ mshape arr) (mstrideTree arr) + then case mtoListLinear arr of + [] -> defaultResult + [_] -> defaultResult + hd : _ -> showString "mreplicate " . shows (shxToList (mshape arr)) . showString " " + . showsPrec 11 hd + else defaultResult newtype ShowViaPrimitive sh a = ShowViaPrimitive (Mixed sh (Primitive a)) @@ -279,6 +287,12 @@ data StrideTree = StrideLeaf [Int] | StrideNode StrideTree StrideTree +stridesAreZero :: Int -> StrideTree -> Bool +stridesAreZero prefixLen (StrideLeaf ss) = + all (== 0) (take prefixLen ss) +stridesAreZero prefixLen (StrideNode ss1 ss2) = + stridesAreZero prefixLen ss1 && stridesAreZero prefixLen ss2 + -- | Allowable element types in a mixed array, and by extension in a 'Ranked' or -- 'Shaped' array. Note the polymorphic instance for 'Elt' of @'Primitive' -- a@; see the documentation for 'Primitive' for more details. |