Skip to content

Commit 88114ef

Browse files
committed
Revert "Updates for 0.7"
This reverts commit dfede46.
1 parent dfede46 commit 88114ef

File tree

8 files changed

+90
-15
lines changed

8 files changed

+90
-15
lines changed

src/Data/Array.js

+26
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,29 @@ exports.zipWith = function (f) {
211211
};
212212
};
213213
};
214+
215+
//------------------------------------------------------------------------------
216+
// Folding ---------------------------------------------------------------------
217+
//------------------------------------------------------------------------------
218+
219+
exports.foldrArray = function (f) {
220+
return function (init) {
221+
return function (xs) {
222+
/* jshint maxparams: 2 */
223+
return xs.reduceRight(function (acc, x) {
224+
return f(x)(acc);
225+
}, init);
226+
};
227+
};
228+
};
229+
230+
exports.foldlArray = function (f) {
231+
return function (init) {
232+
return function (xs) {
233+
/* jshint maxparams: 2 */
234+
return xs.reduce(function (acc, x) {
235+
return f(acc)(x);
236+
}, init);
237+
};
238+
};
239+
};

src/Data/Array.purs

+63-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ module Data.Array
99
( singleton
1010
, (..), range
1111
, replicate
12+
, replicateM
13+
, some
14+
, many
1215

1316
, null
1417
, length
@@ -36,6 +39,7 @@ module Data.Array
3639
, concat
3740
, concatMap
3841
, filter
42+
, filterM
3943
, mapMaybe
4044
, catMaybes
4145

@@ -65,9 +69,9 @@ module Data.Array
6569
, zipWithA
6670
, zip
6771
, unzip
68-
) where
6972

70-
import Prelude
73+
, foldM
74+
) where
7175

7276
import Control.Alt (Alt, (<|>))
7377
import Control.Alternative (Alternative)
@@ -101,6 +105,28 @@ infix 8 ..
101105
-- | Create an array with repeated instances of a value.
102106
foreign import replicate :: forall a. Int -> a -> Array a
103107

108+
-- | Perform a monadic action `n` times collecting all of the results.
109+
replicateM :: forall m a. (Monad m) => Int -> m a -> m (Array a)
110+
replicateM n m | n < 1 = return []
111+
| otherwise = do a <- m
112+
as <- replicateM (n - 1) m
113+
return (a : as)
114+
115+
-- | Attempt a computation multiple times, requiring at least one success.
116+
-- |
117+
-- | The `Lazy` constraint is used to generate the result lazily, to ensure
118+
-- | termination.
119+
some :: forall f a. (Alternative f, Lazy (f (Array a))) => f a -> f (Array a)
120+
some v = (:) <$> v <*> defer (\_ -> many v)
121+
122+
-- | Attempt a computation multiple times, returning as many successful results
123+
-- | as possible (possibly zero).
124+
-- |
125+
-- | The `Lazy` constraint is used to generate the result lazily, to ensure
126+
-- | termination.
127+
many :: forall f a. (Alternative f, Lazy (f (Array a))) => f a -> f (Array a)
128+
many v = some v <|> pure []
129+
104130
--------------------------------------------------------------------------------
105131
-- Array size ------------------------------------------------------------------
106132
--------------------------------------------------------------------------------
@@ -255,6 +281,20 @@ concatMap = flip bind
255281
-- | creating a new array.
256282
foreign import filter :: forall a. (a -> Boolean) -> Array a -> Array a
257283

284+
-- | Filter where the predicate returns a monadic `Boolean`.
285+
-- |
286+
-- | ```purescript
287+
-- | powerSet :: forall a. [a] -> [[a]]
288+
-- | powerSet = filterM (const [true, false])
289+
-- | ```
290+
filterM :: forall a m. (Monad m) => (a -> m Boolean) -> Array a -> m (Array a)
291+
filterM p = uncons' (\_ -> pure []) \x xs -> do
292+
b <- p x
293+
xs' <- filterM p xs
294+
return if b
295+
then x : xs'
296+
else xs'
297+
258298
-- | Apply a function to each element in an array, keeping only the results
259299
-- | which contain a value, creating a new array.
260300
mapMaybe :: forall a b. (a -> Maybe b) -> Array a -> Array b
@@ -430,6 +470,18 @@ unzip :: forall a b. Array (Tuple a b) -> Tuple (Array a) (Array b)
430470
unzip = uncons' (\_ -> Tuple [] []) \(Tuple a b) ts -> case unzip ts of
431471
Tuple as bs -> Tuple (a : as) (b : bs)
432472

473+
--------------------------------------------------------------------------------
474+
-- Folding ---------------------------------------------------------------------
475+
--------------------------------------------------------------------------------
476+
477+
-- | Perform a fold using a monadic step function.
478+
foldM :: forall m a b. (Monad m) => (a -> b -> m a) -> a -> Array b -> m a
479+
foldM f a = uncons' (\_ -> return a) (\b bs -> f a b >>= \a' -> foldM f a' bs)
480+
481+
foreign import foldrArray :: forall a b. (a -> b -> b) -> b -> Array a -> b
482+
483+
foreign import foldlArray :: forall a b. (b -> a -> b) -> b -> Array a -> b
484+
433485
--------------------------------------------------------------------------------
434486
-- Non-Prelude instances -------------------------------------------------------
435487
--------------------------------------------------------------------------------
@@ -444,5 +496,14 @@ instance alternativeArray :: Alternative Array
444496

445497
instance monadPlusArray :: MonadPlus Array
446498

499+
instance foldableArray :: Foldable Array where
500+
foldr f z xs = foldrArray f z xs
501+
foldl f z xs = foldlArray f z xs
502+
foldMap f xs = foldr (\x acc -> f x <> acc) mempty xs
503+
504+
instance traversableArray :: Traversable Array where
505+
traverse f = uncons' (\_ -> pure []) (\x xs -> (:) <$> (f x) <*> traverse f xs)
506+
sequence = uncons' (\_ -> pure []) (\x xs -> (:) <$> x <*> sequence xs)
507+
447508
instance invariantArray :: Invariant Array where
448509
imap = imapF

src/Data/Array/ST.purs

+1-3
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ module Data.Array.ST
1616
, toAssocArray
1717
) where
1818

19-
import Prelude
20-
2119
import Control.Monad.Eff (Eff())
2220
import Control.Monad.ST (ST())
2321
import Data.Maybe (Maybe(..))
@@ -27,7 +25,7 @@ import Data.Maybe (Maybe(..))
2725
-- | The first type parameter represents the memory region which the array belongs to.
2826
-- | The second type parameter defines the type of elements of the mutable array.
2927
-- |
30-
-- | The runtime representation of a value of type `STArray h a` is the same as that of `Array a`,
28+
-- | The runtime representation of a value of type `STArray h a` is the same as that of `[a]`,
3129
-- | except that mutation is allowed.
3230
foreign import data STArray :: * -> * -> *
3331

src/Data/Array/Unsafe.purs

-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
module Data.Array.Unsafe where
77

8-
import Prelude
9-
108
import Data.Array (length, slice)
119

1210
-- | Find the element of an array at the specified index.

test/Test/Data/Array.purs

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
module Test.Data.Array (testArray) where
22

3-
import Prelude
4-
53
import Console (log)
64
import Data.Array
75
import Data.Maybe (Maybe(..), isNothing)

test/Test/Data/Array/ST.purs

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
module Test.Data.Array.ST (testArrayST) where
22

3-
import Prelude
4-
53
import Console (log, print)
64
import Control.Monad.Eff (runPure)
75
import Control.Monad.ST (runST)

test/Test/Data/Array/Unsafe.purs

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
module Test.Data.Array.Unsafe (testArrayUnsafe) where
22

3-
import Prelude
4-
53
import Console (log)
64
import Data.Array.Unsafe
75
import Test.Assert (assert)

test/Test/Main.purs

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
module Test.Main where
22

3-
import Prelude
4-
53
import Test.Data.Array
64
import Test.Data.Array.ST
75
import Test.Data.Array.Unsafe

0 commit comments

Comments
 (0)