Skip to content
Merged
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
21 changes: 19 additions & 2 deletions src/Data/List/Lazy.purs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ module Data.List.Lazy
, (..), range
, replicate
, replicateM
-- , some
-- , many
, some
, many
, repeat
, iterate
, cycle
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Copy link
Contributor

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?

Copy link
Contributor Author

@safareli safareli Apr 26, 2017

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?

Copy link
Contributor

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 :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's fine :p


-- | 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no Lazy constraint here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copied it from List.purs. But even tho Lazy constraint isn't used in many, it's used in some, which is used by many, so it's technically true. If want better wording here please let me know and i'll update it in both places

Copy link
Contributor

Choose a reason for hiding this comment

The 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
Expand Down