Skip to content

Commit a949279

Browse files
authored
Idiomatic Show instance for lazy lists (#181)
* Idiomatic Show instance for lazy lists * Update CHANGELOG.md
1 parent f5b771e commit a949279

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Breaking changes:
99
- Renamed `scanrLazy` to `scanlLazy` and fixed parameter ordering (#161)
1010
- Renamed `group'` to `groupAll` (#182)
1111
- Changed `Alt ZipList` to satisfy distributivity (#150)
12+
- Updated the `Show` instances for (non empty) lazy lists (#181)
1213

1314
New features:
1415
- Added `nubEq`/`nubByEq` (#179)

src/Data/List/Lazy/Types.purs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ newtype List a = List (Lazy (Step a))
3434
-- | `Cons` constructor).
3535
data Step a = Nil | Cons a (List a)
3636

37+
instance showStep :: Show a => Show (Step a) where
38+
show Nil = "Nil"
39+
show (Cons x xs) = "(" <> show x <> " : " <> show xs <> ")"
40+
3741
-- | Unwrap a lazy linked list
3842
step :: forall a. List a -> Step a
3943
step = force <<< unwrap
@@ -59,10 +63,12 @@ infixr 6 cons as :
5963
derive instance newtypeList :: Newtype (List a) _
6064

6165
instance showList :: Show a => Show (List a) where
62-
show xs = "fromStrict (" <> go (step xs) <> ")"
63-
where
64-
go Nil = "Nil"
65-
go (Cons x xs') = "(Cons " <> show x <> " " <> go (step xs') <> ")"
66+
show xs = "(fromFoldable ["
67+
<> case step xs of
68+
Nil -> ""
69+
Cons x xs' ->
70+
show x <> foldl (\shown x' -> shown <> "," <> show x') "" xs'
71+
<> "])"
6672

6773
instance eqList :: Eq a => Eq (List a) where
6874
eq = eq1

0 commit comments

Comments
 (0)