Skip to content

Commit 968a2a5

Browse files
committed
Merge pull request #65 from purescript/unfoldable
Add toUnfoldable
2 parents 791aaa7 + d6b14b7 commit 968a2a5

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

bower.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
],
1818
"dependencies": {
1919
"purescript-foldable-traversable": "^1.0.0-rc.1",
20+
"purescript-partial": "^1.1.0",
2021
"purescript-st": "^1.0.0-rc.1",
2122
"purescript-tuples": "^1.0.0-rc.1",
22-
"purescript-partial": "^1.1.0"
23+
"purescript-unfoldable": "^1.0.0-rc.2"
2324
},
2425
"devDependencies": {
2526
"purescript-assert": "^1.0.0-rc.1",

src/Data/Array.purs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,14 @@
2828
-- | allowing you to iterate over an array and accumulate effects.
2929
-- |
3030
module Data.Array
31-
( singleton
31+
( fromFoldable
32+
, toUnfoldable
33+
, singleton
3234
, (..), range
3335
, replicate
3436
, replicateM
3537
, some
3638
, many
37-
, fromFoldable
3839

3940
, null
4041
, length
@@ -112,9 +113,24 @@ import Data.Foldable (class Foldable, foldl, foldr)
112113
import Data.Maybe (Maybe(..), maybe, isJust, fromJust)
113114
import Data.Traversable (sequence)
114115
import Data.Tuple (Tuple(..))
116+
import Data.Unfoldable (class Unfoldable, unfoldr)
115117

116118
import Partial.Unsafe (unsafePartial)
117119

120+
-- | Convert an `Array` into an `Unfoldable` structure.
121+
toUnfoldable :: forall f a. Unfoldable f => Array a -> f a
122+
toUnfoldable = unfoldr $ uncons' (const Nothing) (\h t -> Just (Tuple h t))
123+
124+
-- | Convert a `Foldable` structure into an `Array`.
125+
fromFoldable :: forall f a. Foldable f => f a -> Array a
126+
fromFoldable = fromFoldableImpl foldr
127+
128+
foreign import fromFoldableImpl
129+
:: forall f a
130+
. (forall b. (a -> b -> b) -> b -> f a -> b)
131+
-> f a
132+
-> Array a
133+
118134
-- | Create an array of one element
119135
singleton :: forall a. a -> Array a
120136
singleton a = [a]
@@ -149,12 +165,6 @@ some v = (:) <$> v <*> defer (\_ -> many v)
149165
many :: forall f a. (Alternative f, Lazy (f (Array a))) => f a -> f (Array a)
150166
many v = some v <|> pure []
151167

152-
-- | Construct an `Array` from any `Foldable` structure.
153-
fromFoldable :: forall f a. (Foldable f) => f a -> Array a
154-
fromFoldable = fromFoldableImpl foldr
155-
156-
foreign import fromFoldableImpl :: forall f a. (forall b. (a -> b -> b) -> b -> f a -> b) -> f a -> Array a
157-
158168
--------------------------------------------------------------------------------
159169
-- Array size ------------------------------------------------------------------
160170
--------------------------------------------------------------------------------

0 commit comments

Comments
 (0)