Skip to content
This repository was archived by the owner on Oct 4, 2020. It is now read-only.

Commit ec44e15

Browse files
authored
Merge pull request #49 from michal-jewel/master
Show instance for GenericSpine.
2 parents cdc538d + 189764e commit ec44e15

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/Data/Generic.purs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,26 @@ data GenericSpine
222222
| SArray (Array (Unit -> GenericSpine))
223223
| SUnit
224224

225+
instance showGenericSpine :: Show GenericSpine
226+
where
227+
show SUnit = "SUnit"
228+
show (SChar c) = "SChar " <> show c
229+
show (SString s) = "SString " <> show s
230+
show (SBoolean b) = "SBoolean " <> show b
231+
show (SNumber n) = "SNumber " <> show n
232+
show (SInt i) = "SInt " <> show i
233+
show (SArray arr) = "SArray " <> showArray showSuspended arr
234+
show (SProd lbl arr) = "SProd " <> show lbl <> " "
235+
<> showArray showSuspended arr
236+
show (SRecord arr) = "SRecord " <> showArray showElt arr
237+
where
238+
showElt { recLabel: label, recValue: value } =
239+
fold ["{ recLabel: ", show label, ", recValue: ", showSuspended value, "}"]
240+
241+
-- | Shows a lazily evaluated value under a function with `Unit` parameter.
242+
showSuspended :: forall a. Show a => (Unit -> a) -> String
243+
showSuspended e = "\\_->" <> show (e unit)
244+
225245
instance eqGenericSpine :: Eq GenericSpine where
226246
eq (SProd s1 arr1) (SProd s2 arr2) =
227247
s1 == s2 && length arr1 == length arr2 && zipAll eqThunk arr1 arr2
@@ -349,6 +369,7 @@ showSignature sig =
349369
-- We use this instead of the default Show Array instance to avoid escaping
350370
-- strings twice.
351371
showArray :: forall a. (a -> String) -> Array a -> String
372+
showArray _ [] = "[]"
352373
showArray f xs = "[ " <> intercalate ", " (map f xs) <> " ]"
353374

354375
showLabel
@@ -473,3 +494,4 @@ orderingToInt = case _ of
473494
EQ -> 0
474495
LT -> 1
475496
GT -> -1
497+

test/Main.purs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ main = do
6666
, Quux ["Hi","Dere"]
6767
, Baz {a : Just "yo", bq : 22.0} "oy"
6868
, Corge ['H', 'i', ' ', 'D', 'e', 'r', 'e'] ]
69+
logShow $ toSpine [
70+
Foo 12.0 "Hello"
71+
, Quux ["Hi","Dere"]
72+
, Baz {a : Just "yo", bq : 22.0} "oy"
73+
, Corge ['H', 'i', ' ', 'D', 'e', 'r', 'e'] ]
6974
logShow $ gCompare (Bar 12.3) (Foo 34.1 "hello")
7075
logShow $ gEq
7176
[ Foo 12.0 "Hello"

0 commit comments

Comments
 (0)