-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathUnfoldable.purs
More file actions
69 lines (62 loc) · 1.84 KB
/
Unfoldable.purs
File metadata and controls
69 lines (62 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
module Test.Unfoldable where
import Prelude
import Data.Eq (class Eq1)
import Data.Maybe (Maybe(..))
import Data.Tuple (Tuple(..), uncurry)
import Data.Unfoldable as U
import Data.Unfoldable1 as U1
import Effect (Effect)
import Effect.Console (log, logShow)
import Test.Assert (assert)
data NonEmpty f a
= NonEmpty a (f a)
derive instance eqNonEmpty :: (Eq1 f, Eq a) => Eq (NonEmpty f a)
instance unfoldable1NonEmpty :: U.Unfoldable f => U1.Unfoldable1 (NonEmpty f) where
unfoldr1 f = uncurry NonEmpty <<< map (U.unfoldr $ map f) <<< f
collatz :: Int -> Array Int
collatz = U.unfoldr step
where
step 1 = Nothing
step n =
Just
$ Tuple n
$ if n `mod` 2 == 0 then
n / 2
else
n * 3 + 1
testUnfoldable :: Effect Unit
testUnfoldable = do
log "Collatz 1000"
logShow $ collatz 1000
log "Test none"
assert $ U.none == ([] :: Array Unit)
log "Test singleton"
assert $ U.singleton unit == [ unit ]
assert $ U1.singleton unit == NonEmpty unit []
log "Test replicate"
assert $ U.replicate 0 "foo" == []
assert $ U.replicate 3 "foo" == [ "foo", "foo", "foo" ]
assert $ U1.replicate1 0 "foo" == NonEmpty "foo" []
assert $ U1.replicate1 3 "foo" == NonEmpty "foo" [ "foo", "foo" ]
log "Test replicateA"
assert $ U.replicateA 3 [ 1, 2 ]
== [ [ 1, 1, 1 ]
, [ 1, 1, 2 ]
, [ 1, 2, 1 ]
, [ 1, 2, 2 ]
, [ 2, 1, 1 ]
, [ 2, 1, 2 ]
, [ 2, 2, 1 ]
, [ 2, 2, 2 ]
]
log "Test range"
assert $ U1.range 1 0 == [ 1, 0 ]
assert $ U1.range 0 0 == [ 0 ]
assert $ U1.range 0 2 == [ 0, 1, 2 ]
assert $ U1.range 1 0 == NonEmpty 1 [ 0 ]
assert $ U1.range 0 0 == NonEmpty 0 []
assert $ U1.range 0 2 == NonEmpty 0 [ 1, 2 ]
log "Test Maybe.toUnfoldable"
assert $ U.fromMaybe (Just "a") == [ "a" ]
assert $ U.fromMaybe (Nothing :: Maybe String) == []
log "All done!"