From d6b14b7a9b870cd8ebd98135f27f7c565c1ffe46 Mon Sep 17 00:00:00 2001 From: Gary Burgess Date: Mon, 4 Apr 2016 01:14:14 +0100 Subject: [PATCH] Add toUnfoldable --- bower.json | 3 ++- src/Data/Array.purs | 26 ++++++++++++++++++-------- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/bower.json b/bower.json index 3240844b..5d24813a 100644 --- a/bower.json +++ b/bower.json @@ -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", diff --git a/src/Data/Array.purs b/src/Data/Array.purs index d17a3172..622c66cf 100644 --- a/src/Data/Array.purs +++ b/src/Data/Array.purs @@ -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 @@ -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] @@ -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 ------------------------------------------------------------------ --------------------------------------------------------------------------------