@@ -4,13 +4,19 @@ import Prelude
44
55import Control.Monad.Eff (Eff )
66import Control.Monad.Eff.Console (CONSOLE , log , logShow )
7-
87import Data.Maybe (Maybe (..))
9- import Data.Tuple (Tuple (..))
8+ import Data.Tuple (Tuple (..), uncurry )
109import Data.Unfoldable as U
11-
10+ import Data.Unfoldable1 as U1
1211import Test.Assert (ASSERT , assert )
1312
13+ data NonEmpty f a = NonEmpty a (f a )
14+
15+ derive instance eqNonEmpty :: (Eq (f a ), Eq a ) => Eq (NonEmpty f a )
16+
17+ instance unfoldable1NonEmpty :: U.Unfoldable f => U1.Unfoldable1 (NonEmpty f ) where
18+ unfoldr1 f = uncurry NonEmpty <<< map (U .unfoldr $ map f) <<< f
19+
1420collatz :: Int -> Array Int
1521collatz = U .unfoldr step
1622 where
@@ -32,21 +38,30 @@ main = do
3238
3339 log " Test singleton"
3440 assert $ U .singleton unit == [unit]
41+ assert $ U1 .singleton unit == NonEmpty unit []
3542
3643 log " Test replicate"
44+ assert $ U .replicate 0 " foo" == []
3745 assert $ U .replicate 3 " foo" == [" foo" , " foo" , " foo" ]
46+ assert $ U1 .replicate1 0 " foo" == NonEmpty " foo" []
47+ assert $ U1 .replicate1 3 " foo" == NonEmpty " foo" [" foo" , " foo" ]
3848
3949 log " Test replicateA"
4050 assert $ U .replicateA 3 [1 ,2 ] == [
4151 [1 ,1 ,1 ],[1 ,1 ,2 ], [1 ,2 ,1 ],[1 ,2 ,2 ],
4252 [2 ,1 ,1 ],[2 ,1 ,2 ], [2 ,2 ,1 ],[2 ,2 ,2 ]
4353 ]
4454
45- log " Test range"
55+ log " Test U. range"
4656 assert $ U .range 1 0 == []
4757 assert $ U .range 0 0 == [0 ]
4858 assert $ U .range 0 2 == [0 , 1 , 2 ]
4959
60+ log " Test U1.range"
61+ assert $ U1 .range 1 0 == NonEmpty 1 [0 ]
62+ assert $ U1 .range 0 0 == NonEmpty 0 []
63+ assert $ U1 .range 0 2 == NonEmpty 0 [1 , 2 ]
64+
5065 log " Test Maybe.toUnfoldable"
5166 assert $ U .fromMaybe (Just " a" ) == [" a" ]
5267 assert $ U .fromMaybe (Nothing :: Maybe String ) == []
0 commit comments