-
Notifications
You must be signed in to change notification settings - Fork 49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Idiomatic Show instance for lazy lists #181
Idiomatic Show instance for lazy lists #181
Conversation
While this is technically code that does indeed work, I'm not sure how useful it is as a Show instance since it's very much not compact. It's very difficult to see what the values are in the midst of all the other noise (and parens, which will quickly get out of hand). I feel like it kind of misses the forest for the trees as far as helping someone see what's actually in the list. |
Seems like this would be less of a dilemma if we adopted debugged. |
src/Data/List/Lazy/Types.purs
Outdated
@@ -34,6 +34,10 @@ newtype List a = List (Lazy (Step a)) | |||
-- | `Cons` constructor). | |||
data Step a = Nil | Cons a (List a) | |||
|
|||
instance showStep :: Show a => Show (Step a) where | |||
show Nil = "Nil" | |||
show (Cons x xs) = show x <> " : " <> show xs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this not be wrapped in parens?
I personally think |
I still want to adopt debugged in core, but in any case I think we should continue providing Show instances for the foreseeable future. |
Let's use |
I'll come back to this once I'm done with the compiler releases notes. I'll never get them done if I find other ways to spend m'y PureScript budget 😬 |
Does this PR only need these two changes? -- adding parenthesis around `Step`'s `Cons` case
instance showStep :: Show a => Show (Step a) where
show Nil = "Nil"
show (Cons x xs) = "(" <> show x <> " : " <> show xs <> ")"
-- using the 'fromFoldable' approach:
instance showList :: Show a => Show (List a) where
show ls = "fromFoldable [" <> print (step ls) <> "]"
where
print Nil = ""
print (Cons x xs') = show x <> go (step xs')
go Nil = ""
go (Cons x xs') = ", " <> show x |
@kl0tl Mind if I push the changes I mentioned above here? If that's all we need to do, then we can get this PR merged. |
I pushed a patch based on your suggestion! Here’s the output of show for Data.List.Lazy.Types.List, Data.List.Lazy.Types.Step and Data.List.Lazy.Types.NonEmptyList: > Data.List.Lazy.fromFoldable [0, 1, 2]
(fromFoldable [0,1,2])
> Data.List.Lazy.NonEmpty.fromFoldable [0, 1, 2]
(Just (NonEmptyList (defer \_ -> (NonEmpty 0 (fromFoldable [1,2])))))
> Data.List.Lazy.step (Data.List.Lazy.fromFoldable [0, 1, 2])
(0 : (fromFoldable [1,2])) |
91f5f05
to
acfae4f
Compare
I also opened purescript/purescript-ordered-collections#46 for consistency, so that we display arrays instead of lists when showing (non empty) sets. |
Any other feedback before we merge this? |
This pull request changes the output of show for Data.List.Lazy.Types.List and Data.List.Lazy.Types.NonEmptyList from this:
to that:
Alternatively, we could show
(fromFoldable […])
but this makes the output of show less similar:Close #122.