Skip to content

Commit d34973b

Browse files
committed
add some and many for Lazy List
1 parent 03a480b commit d34973b

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

src/Data/List/Lazy.purs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ module Data.List.Lazy
1616
, (..), range
1717
, replicate
1818
, replicateM
19-
-- , some
20-
-- , many
19+
, some
20+
, many
2121
, repeat
2222
, iterate
2323
, cycle
@@ -91,6 +91,8 @@ module Data.List.Lazy
9191

9292
import Prelude
9393

94+
import Control.Alt ((<|>))
95+
import Control.Alternative (class Alternative)
9496
import Control.Lazy as Z
9597

9698
import Data.Foldable (class Foldable, foldr, any, foldl)
@@ -160,6 +162,21 @@ replicateM n m
160162
as <- replicateM (n - one) m
161163
pure (cons a as)
162164

165+
-- | Attempt a computation multiple times, requiring at least one success.
166+
-- |
167+
-- | The `Lazy` constraint is used to generate the result lazily, to ensure
168+
-- | termination.
169+
some :: forall f a. Alternative f => Z.Lazy (f (List a)) => f a -> f (List a)
170+
some v = cons <$> v <*> Z.defer (\_ -> many v)
171+
172+
-- | Attempt a computation multiple times, returning as many successful results
173+
-- | as possible (possibly zero).
174+
-- |
175+
-- | The `Lazy` constraint is used to generate the result lazily, to ensure
176+
-- | termination.
177+
many :: forall f a. Alternative f => Z.Lazy (f (List a)) => f a -> f (List a)
178+
many v = some v <|> pure nil
179+
163180
-- | Create a list by repeating an element
164181
repeat :: forall a. a -> List a
165182
repeat x = Z.fix \xs -> cons x xs

0 commit comments

Comments
 (0)