@@ -67,6 +67,7 @@ module Data.Array
67
67
, concatMap
68
68
, filter
69
69
, partition
70
+ , filterA
70
71
, filterM
71
72
, mapMaybe
72
73
, catMaybes
@@ -121,7 +122,7 @@ import Data.Foldable (foldl, foldr, foldMap, fold, intercalate, elem, notElem, f
121
122
import Data.Maybe (Maybe (..), maybe , isJust , fromJust )
122
123
import Data.NonEmpty (NonEmpty , (:|))
123
124
import Data.Traversable (scanl , scanr ) as Exports
124
- import Data.Traversable (sequence )
125
+ import Data.Traversable (sequence , traverse )
125
126
import Data.Tuple (Tuple (..))
126
127
import Data.Unfoldable (class Unfoldable , unfoldr )
127
128
@@ -417,17 +418,20 @@ foreign import partition
417
418
-> Array a
418
419
-> { yes :: Array a , no :: Array a }
419
420
420
- -- | Filter where the predicate returns a monadic `Boolean`.
421
+ -- | Filter where the predicate returns a `Boolean` in some `Applicative `.
421
422
-- |
422
423
-- | ```purescript
423
- -- | powerSet :: forall a. [a] -> [[a]]
424
- -- | powerSet = filterM (const [true, false])
424
+ -- | powerSet :: forall a. Array a -> Array (Array a)
425
+ -- | powerSet = filterA (const [true, false])
425
426
-- | ```
427
+ filterA :: forall a f . Applicative f => (a -> f Boolean ) -> Array a -> f (Array a )
428
+ filterA p =
429
+ traverse (\x -> Tuple x <$> p x)
430
+ >>> map (mapMaybe (\(Tuple x b) -> if b then Just x else Nothing ))
431
+
432
+ -- | Deprecated alias for `filterA`.
426
433
filterM :: forall a m . Monad m => (a -> m Boolean ) -> Array a -> m (Array a )
427
- filterM p = uncons' (\_ -> pure [] ) \x xs -> do
428
- b <- p x
429
- xs' <- filterM p xs
430
- pure if b then x : xs' else xs'
434
+ filterM = filterA
431
435
432
436
-- | Apply a function to each element in an array, keeping only the results
433
437
-- | which contain a value, creating a new array.
0 commit comments