File tree Expand file tree Collapse file tree 2 files changed +13
-2
lines changed Expand file tree Collapse file tree 2 files changed +13
-2
lines changed Original file line number Diff line number Diff line change 18
18
],
19
19
"dependencies" : {
20
20
"purescript-partial" : " ^1.2.0" ,
21
- "purescript-tuples" : " ^4.0.0"
21
+ "purescript-tuples" : " ^4.0.0" ,
22
+ "purescript-control" : " ^3.3.1"
22
23
},
23
24
"devDependencies" : {
24
25
"purescript-assert" : " ^3.0.0" ,
Original file line number Diff line number Diff line change @@ -11,15 +11,16 @@ module Data.Unfoldable
11
11
, none
12
12
, singleton
13
13
, range
14
+ , nats
14
15
, fromMaybe
15
16
) where
16
17
17
18
import Prelude
18
19
20
+ import Control.Lazy (class Lazy )
19
21
import Data.Maybe (Maybe (..), isNothing , fromJust )
20
22
import Data.Traversable (class Traversable , sequence )
21
23
import Data.Tuple (Tuple (..), fst , snd )
22
-
23
24
import Partial.Unsafe (unsafePartial )
24
25
25
26
-- | This class identifies data structures which can be _unfolded_,
@@ -94,6 +95,15 @@ range :: forall f. Unfoldable f => Int -> Int -> f Int
94
95
range start end =
95
96
unfoldr (\i -> if i <= end then Just (Tuple i $ i + 1 ) else Nothing ) start
96
97
98
+ -- | Produce a Lazy Unfoldable structure representing the natural numbers.
99
+ --
100
+ -- The result of this function is infinite.
101
+ nats :: forall f a . Unfoldable f => Semiring a => Lazy (f a ) => f a
102
+ nats = unfoldr increment zero
103
+ where
104
+ increment :: a -> Maybe (Tuple a a )
105
+ increment a = Just (Tuple a $ add one a)
106
+
97
107
-- | Convert a Maybe to any Unfoldable like lists and arrays.
98
108
fromMaybe :: forall f a . Unfoldable f => Maybe a -> f a
99
109
fromMaybe = unfoldr (\b -> flip Tuple Nothing <$> b)
You can’t perform that action at this time.
0 commit comments