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 @@ -10,15 +10,16 @@ module Data.Unfoldable
10
10
, replicateA
11
11
, none
12
12
, singleton
13
+ , nats
13
14
, fromMaybe
14
15
) where
15
16
16
17
import Prelude
17
18
19
+ import Control.Lazy (class Lazy )
18
20
import Data.Maybe (Maybe (..), isNothing , fromJust )
19
21
import Data.Traversable (class Traversable , sequence )
20
22
import Data.Tuple (Tuple (..), fst , snd )
21
-
22
23
import Partial.Unsafe (unsafePartial )
23
24
24
25
-- | This class identifies data structures which can be _unfolded_,
@@ -88,6 +89,15 @@ none = unfoldr (const Nothing) unit
88
89
singleton :: forall f a . Unfoldable f => a -> f a
89
90
singleton = replicate 1
90
91
92
+ -- | Produce a Lazy Unfoldable structure representing the natural numbers.
93
+ --
94
+ -- The result of this function is infinite.
95
+ nats :: forall f a . Unfoldable f => Semiring a => Lazy (f a ) => f a
96
+ nats = unfoldr increment zero
97
+ where
98
+ increment :: a -> Maybe (Tuple a a )
99
+ increment a = Just (Tuple a $ add one a)
100
+
91
101
-- | Convert a Maybe to any Unfoldable like lists and arrays.
92
102
fromMaybe :: forall f a . Unfoldable f => Maybe a -> f a
93
103
fromMaybe = unfoldr (\b -> flip Tuple Nothing <$> b)
You can’t perform that action at this time.
0 commit comments