Skip to content
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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Breaking changes:
- Renamed `scanrLazy` to `scanlLazy` and fixed parameter ordering (#161)
- Renamed `group'` to `groupAll` (#182)
- Changed `Alt ZipList` to satisfy distributivity (#150)
- Updated the `Show` instances for (non empty) lazy lists (#181)

New features:
- Added `nubEq`/`nubByEq` (#179)
Expand Down
14 changes: 10 additions & 4 deletions src/Data/List/Lazy/Types.purs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <> ")"

-- | Unwrap a lazy linked list
step :: forall a. List a -> Step a
step = force <<< unwrap
Expand All @@ -59,10 +63,12 @@ infixr 6 cons as :
derive instance newtypeList :: Newtype (List a) _

instance showList :: Show a => Show (List a) where
show xs = "fromStrict (" <> go (step xs) <> ")"
where
go Nil = "Nil"
go (Cons x xs') = "(Cons " <> show x <> " " <> go (step xs') <> ")"
show xs = "(fromFoldable ["
<> case step xs of
Nil -> ""
Cons x xs' ->
show x <> foldl (\shown x' -> shown <> "," <> show x') "" xs'
<> "])"

instance eqList :: Eq a => Eq (List a) where
eq = eq1
Expand Down