-
Notifications
You must be signed in to change notification settings - Fork 50
add some and many for Lazy List #113
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,8 +16,8 @@ module Data.List.Lazy | |
, (..), range | ||
, replicate | ||
, replicateM | ||
-- , some | ||
-- , many | ||
, some | ||
, many | ||
, repeat | ||
, iterate | ||
, cycle | ||
|
@@ -91,6 +91,8 @@ module Data.List.Lazy | |
|
||
import Prelude | ||
|
||
import Control.Alt ((<|>)) | ||
import Control.Alternative (class Alternative) | ||
import Control.Lazy as Z | ||
|
||
import Data.Foldable (class Foldable, foldr, any, foldl) | ||
|
@@ -160,6 +162,21 @@ replicateM n m | |
as <- replicateM (n - one) m | ||
pure (cons a as) | ||
|
||
-- | Attempt a computation multiple times, requiring at least one success. | ||
-- | | ||
-- | The `Lazy` constraint is used to generate the result lazily, to ensure | ||
-- | termination. | ||
some :: forall f a. Alternative f => Z.Lazy (f (List a)) => f a -> f (List a) | ||
some v = cons <$> v <*> Z.defer (\_ -> many v) | ||
|
||
-- | Attempt a computation multiple times, returning as many successful results | ||
-- | as possible (possibly zero). | ||
-- | | ||
-- | The `Lazy` constraint is used to generate the result lazily, to ensure | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Copied it from List.purs. But even tho There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I misread it, please ignore my comment. |
||
-- | termination. | ||
many :: forall f a. Alternative f => Z.Lazy (f (List a)) => f a -> f (List a) | ||
many v = some v <|> pure nil | ||
|
||
-- | Create a list by repeating an element | ||
repeat :: forall a. a -> List a | ||
repeat x = Z.fix \xs -> cons x 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.
Is it possible to generate the tail lazily here?
Uh oh!
There was an error while loading. Please reload this page.
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.
That's what
Z.defer
is doing here.Or do you mean something else?
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.
Yes, I think this is fine. Sorry again :)
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.
It's fine :p