@@ -67,6 +67,7 @@ module Data.Array
6767 , concatMap
6868 , filter
6969 , partition
70+ , filterA
7071 , filterM
7172 , mapMaybe
7273 , catMaybes
@@ -121,7 +122,7 @@ import Data.Foldable (foldl, foldr, foldMap, fold, intercalate, elem, notElem, f
121122import Data.Maybe (Maybe (..), maybe , isJust , fromJust )
122123import Data.NonEmpty (NonEmpty , (:|))
123124import Data.Traversable (scanl , scanr ) as Exports
124- import Data.Traversable (sequence )
125+ import Data.Traversable (sequence , traverse )
125126import Data.Tuple (Tuple (..))
126127import Data.Unfoldable (class Unfoldable , unfoldr )
127128
@@ -417,17 +418,20 @@ foreign import partition
417418 -> Array a
418419 -> { yes :: Array a , no :: Array a }
419420
420- -- | Filter where the predicate returns a monadic `Boolean`.
421+ -- | Filter where the predicate returns a `Boolean` in some `Applicative `.
421422-- |
422423-- | ```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])
425426-- | ```
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`.
426433filterM :: 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
431435
432436-- | Apply a function to each element in an array, keeping only the results
433437-- | which contain a value, creating a new array.
0 commit comments