Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = function(grunt) {
src: ["<%=libFiles%>"]
},
tests: {
src: ["tests/Tests.purs", "<%=libFiles%>"]
src: ["tests/**/*.purs", "<%=libFiles%>"]
}
},

Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

instance applyListT :: (Monad f) => Apply (ListT f)

instance arbitraryListT :: (Monad f, Arbitrary a) => Arbitrary (ListT f a)

instance bindListT :: (Monad f) => Bind (ListT f)

instance functorListT :: (Functor f) => Functor (ListT f)
Expand Down Expand Up @@ -99,6 +101,8 @@

instance applyList :: Apply List

instance arbitraryList :: (Arbitrary a) => Arbitrary (List a)

instance bindList :: Bind List

instance eqList :: (Eq a) => Eq (List a)
Expand Down
22 changes: 22 additions & 0 deletions src/Control/Monad/ListT.purs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ module Control.Monad.ListT
, wrapEffect
, wrapLazy
, unfold
, iterate
, fromArray
, toArray
, take
, takeWhile
, drop
Expand All @@ -22,6 +24,7 @@ module Control.Monad.ListT
, head
, tail
, foldl
, foldl'
, scanl
, zipWith'
, zipWith
Expand All @@ -35,6 +38,9 @@ module Control.Monad.ListT
import Data.Tuple
import qualified Data.Array as A

import Test.QuickCheck
import Test.QuickCheck.LCG

data ListT f a = ListT (f (Step a (ListT f a)))

data Step a s =
Expand Down Expand Up @@ -96,6 +102,9 @@ module Control.Monad.ListT
instance monadTransListT :: MonadTrans ListT where
lift = fromEffect

instance arbitraryListT :: (Monad f, Arbitrary a) => Arbitrary (ListT f a) where
arbitrary = fromArray <$> arbitrary

singleton :: forall f a. (Applicative f) => a -> ListT f a
singleton a = prepend a nil

Expand All @@ -112,11 +121,18 @@ module Control.Monad.ListT
unfold f z = ListT $ g <$> f z where
g (Just (Tuple z a)) = Yield a (defer \_ -> (unfold f z))
g Nothing = Done

iterate :: forall f a. (Monad f) => (a -> a) -> a -> ListT f a
iterate f a = unfold g a where
g a = pure $ Just (Tuple (f a) a)

fromArray :: forall f a. (Monad f) => [a] -> ListT f a
fromArray xs = unfold f 0 where
f n = pure $ Tuple (n + 1) <$> (xs A.!! n)

toArray :: forall f a. (Monad f) => ListT f a -> f [a]
toArray = ((<$>) A.reverse) <<< foldl (flip (:)) []

take :: forall f a. (Applicative f) => Number -> ListT f a -> ListT f a
take 0 fa = nil
take n fa = stepMap f fa where
Expand Down Expand Up @@ -171,6 +187,12 @@ module Control.Monad.ListT
tail :: forall f a. (Monad f) => ListT f a -> f (Maybe (ListT f a))
tail l = ((<$>) snd) <$> uncons l

foldl' :: forall f a b. (Monad f) => (b -> a -> f b) -> b -> ListT f a -> f b
foldl' f = loop where
loop b l = uncons l >>= g where
g Nothing = pure b
g (Just (Tuple a as)) = (f b a) >>= (flip loop as)

foldl :: forall f a b. (Monad f) => (b -> a -> b) -> b -> ListT f a -> f b
foldl f = loop where
loop b l = uncons l >>= g where
Expand Down
6 changes: 6 additions & 0 deletions src/Data/List.purs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ import Control.Plus
import Control.Alternative
import Control.MonadPlus

import Test.QuickCheck
import Test.QuickCheck.LCG

data List a = Nil | Cons a (List a)

instance showList :: (Show a) => Show (List a) where
Expand Down Expand Up @@ -130,6 +133,9 @@ instance alternativeList :: Alternative List

instance monadPlusList :: MonadPlus List

instance arbitraryList :: (Arbitrary a) => Arbitrary (List a) where
arbitrary = fromArray <$> arbitrary

fromArray :: forall a. [a] -> List a
fromArray = foldr Cons Nil

Expand Down
39 changes: 39 additions & 0 deletions tests/Control/Monad/ListT.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module Tests.Data.ListT (runListTTests) where

import Debug.Trace

import Control.Monad.Identity

import Test.QuickCheck
import Test.QuickCheck.LCG
import Data.Tuple
import Data.Maybe

import Control.Monad.ListT
import qualified Data.Array as A

data ZeroToTen = ZeroToTen Number

runZeroToTen :: ZeroToTen -> Number
runZeroToTen (ZeroToTen n) = n

instance arbZeroToTen :: Arbitrary ZeroToTen where
arbitrary = ZeroToTen <$> chooseInt 0 10

checkFromToArray a =
(runIdentity $ (toArray <<< fromArray) a) == (a :: [Number]) <?> "toArray <<< fromArray == id"

checkTake (Tuple a (ZeroToTen n)) =
(runIdentity $ (toArray <<< (take n) <<< fromArray) a) == A.take n (a :: [Number]) <?> "take"

checkIterate (ZeroToTen n) =
(runIdentity $ head $ iterate ((+) 1) n) == Just n <?> "iterate"

runListTTests = do
trace "Running ListT tests"

quickCheck $ checkFromToArray

quickCheck $ checkTake

quickCheck $ checkIterate
11 changes: 11 additions & 0 deletions tests/Data/List/List.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Tests.Data.List where

import Debug.Trace

import Test.QuickCheck
import Test.QuickCheck.LCG

import Data.List

runListTests = do
trace "Running List tests"
9 changes: 6 additions & 3 deletions tests/Tests.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ module Main where

import Debug.Trace

import Data.List

import Test.QuickCheck
import Test.QuickCheck.LCG

import Tests.Data.List
import Tests.Data.ListT

main = do
trace "Done"
runListTests
runListTTests