44-- | This allows us to unify various operations on arrays, lists,
55-- | sequences, etc.
66
7- module Data.Unfoldable where
7+ module Data.Unfoldable
8+ ( class Unfoldable , unfoldr
9+ , replicate
10+ , replicateA
11+ , none
12+ , singleton
13+ ) where
814
915import Prelude
1016
11- import Control.Monad.Eff (untilE , runPure )
12- import Control.Monad.ST (writeSTRef , readSTRef , newSTRef )
13-
14- import Data.Array.ST (pushSTArray , emptySTArray , runSTArray )
15- import Data.Maybe (Maybe (..))
17+ import Data.Maybe (Maybe (..), isNothing , fromJust )
1618import Data.Traversable (class Traversable , sequence )
17- import Data.Tuple (Tuple (..))
19+ import Data.Tuple (Tuple (..), fst , snd )
20+
21+ import Partial.Unsafe (unsafePartial )
1822
1923-- | This class identifies data structures which can be _unfolded_,
2024-- | generalizing `unfoldr` on arrays.
@@ -28,18 +32,17 @@ class Unfoldable t where
2832 unfoldr :: forall a b . (b -> Maybe (Tuple a b )) -> b -> t a
2933
3034instance unfoldableArray :: Unfoldable Array where
31- unfoldr f b = runPure (runSTArray (do
32- arr <- emptySTArray
33- seed <- newSTRef b
34- untilE $ do
35- b1 <- readSTRef seed
36- case f b1 of
37- Nothing -> pure true
38- Just (Tuple a b2) -> do
39- pushSTArray arr a
40- writeSTRef seed b2
41- pure false
42- pure arr))
35+ unfoldr = unfoldrArrayImpl isNothing (unsafePartial fromJust) fst snd
36+
37+ foreign import unfoldrArrayImpl
38+ :: forall a b
39+ . (forall x . Maybe x -> Boolean )
40+ -> (forall x . Maybe x -> x )
41+ -> (forall x y . Tuple x y -> x )
42+ -> (forall x y . Tuple x y -> y )
43+ -> (b -> Maybe (Tuple a b ))
44+ -> b
45+ -> Array a
4346
4447-- | Replicate a value some natural number of times.
4548-- | For example:
0 commit comments