From d34973be4e1f1d8a0609a21116285ef3140f151d Mon Sep 17 00:00:00 2001 From: Irakli Safareli Date: Wed, 26 Apr 2017 18:39:29 +0400 Subject: [PATCH] add some and many for Lazy List --- src/Data/List/Lazy.purs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/Data/List/Lazy.purs b/src/Data/List/Lazy.purs index 44c5e9c..eb748dc 100644 --- a/src/Data/List/Lazy.purs +++ b/src/Data/List/Lazy.purs @@ -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 +-- | 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