Skip to content

Commit

Permalink
Add toUnfoldable
Browse files Browse the repository at this point in the history
  • Loading branch information
garyb committed Apr 4, 2016
1 parent 791aaa7 commit d6b14b7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
],
"dependencies": {
"purescript-foldable-traversable": "^1.0.0-rc.1",
"purescript-partial": "^1.1.0",
"purescript-st": "^1.0.0-rc.1",
"purescript-tuples": "^1.0.0-rc.1",
"purescript-partial": "^1.1.0"
"purescript-unfoldable": "^1.0.0-rc.2"
},
"devDependencies": {
"purescript-assert": "^1.0.0-rc.1",
Expand Down
26 changes: 18 additions & 8 deletions src/Data/Array.purs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@
-- | allowing you to iterate over an array and accumulate effects.
-- |
module Data.Array
( singleton
( fromFoldable
, toUnfoldable
, singleton
, (..), range
, replicate
, replicateM
, some
, many
, fromFoldable

, null
, length
Expand Down Expand Up @@ -112,9 +113,24 @@ import Data.Foldable (class Foldable, foldl, foldr)
import Data.Maybe (Maybe(..), maybe, isJust, fromJust)
import Data.Traversable (sequence)
import Data.Tuple (Tuple(..))
import Data.Unfoldable (class Unfoldable, unfoldr)

import Partial.Unsafe (unsafePartial)

-- | Convert an `Array` into an `Unfoldable` structure.
toUnfoldable :: forall f a. Unfoldable f => Array a -> f a
toUnfoldable = unfoldr $ uncons' (const Nothing) (\h t -> Just (Tuple h t))

-- | Convert a `Foldable` structure into an `Array`.
fromFoldable :: forall f a. Foldable f => f a -> Array a
fromFoldable = fromFoldableImpl foldr

foreign import fromFoldableImpl
:: forall f a
. (forall b. (a -> b -> b) -> b -> f a -> b)
-> f a
-> Array a

-- | Create an array of one element
singleton :: forall a. a -> Array a
singleton a = [a]
Expand Down Expand Up @@ -149,12 +165,6 @@ some v = (:) <$> v <*> defer (\_ -> many v)
many :: forall f a. (Alternative f, Lazy (f (Array a))) => f a -> f (Array a)
many v = some v <|> pure []

-- | Construct an `Array` from any `Foldable` structure.
fromFoldable :: forall f a. (Foldable f) => f a -> Array a
fromFoldable = fromFoldableImpl foldr

foreign import fromFoldableImpl :: forall f a. (forall b. (a -> b -> b) -> b -> f a -> b) -> f a -> Array a

--------------------------------------------------------------------------------
-- Array size ------------------------------------------------------------------
--------------------------------------------------------------------------------
Expand Down

0 comments on commit d6b14b7

Please sign in to comment.