Skip to content

Add extra defaults for partition and filter #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 9, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 36 additions & 6 deletions src/Data/Filterable.purs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ module Data.Filterable
, filter
, eitherBool
, partitionDefault
, partitionDefaultFilter
, partitionDefaultFilterMap
, maybeBool
, filterDefault
, filterDefaultPartition
, filterDefaultPartitionMap
, partitioned
, filtered
, cleared
Expand All @@ -19,9 +23,10 @@ import Data.Array (partition, mapMaybe, filter) as Array
import Data.Either (Either(..))
import Data.Foldable (foldl, foldr)
import Data.Functor (class Functor)
import Data.HeytingAlgebra (not)
import Data.List (List(..), filter, mapMaybe) as List
import Data.Maybe (Maybe(..))
import Data.Map (Map, empty, insert, alter, toUnfoldable) as Map
import Data.Maybe (Maybe(..))
import Data.Monoid (class Monoid, mempty)
import Data.Semigroup ((<>))
import Data.Tuple (Tuple(..))
Expand Down Expand Up @@ -60,23 +65,49 @@ eitherBool :: forall a.
(a -> Boolean) -> a -> Either a a
eitherBool p x = if p x then Right x else Left x

-- | Upgrade a boolean-style predicate to a maybe-style predicate mapping.
maybeBool :: forall a.
(a -> Boolean) -> a -> Maybe a
maybeBool p x = if p x then Just x else Nothing

-- | A default implementation of `partition` using `partitionMap`.
partitionDefault :: forall f a. Filterable f =>
(a -> Boolean) -> f a -> { no :: f a, yes :: f a }
partitionDefault p xs =
let o = partitionMap (eitherBool p) xs
in {no: o.left, yes: o.right}

-- | Upgrade a boolean-style predicate to a maybe-style predicate mapping.
maybeBool :: forall a.
(a -> Boolean) -> a -> Maybe a
maybeBool p x = if p x then Just x else Nothing
-- | A default implementation of `partition` using `filter`. Note that this is
-- | almost certainly going to be suboptimal compared to direct implementations.
partitionDefaultFilter :: forall f a. Filterable f =>
(a -> Boolean) -> f a -> { no :: f a, yes :: f a }
partitionDefaultFilter p xs = { yes: filter p xs, no: filter (not p) xs }

-- | A default implementation of `partition` using `filterMap`. Note that this
-- | is almost certainly going to be suboptimal compared to direct
-- | implementations.
partitionDefaultFilterMap :: forall f a. Filterable f =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you move this somewhere under maybeBool? I try not to use declarations until after they have been introduced.

(a -> Boolean) -> f a -> { no :: f a, yes :: f a }
partitionDefaultFilterMap p xs =
{ yes: filterMap (maybeBool p) xs
, no: filterMap (maybeBool (not p)) xs
}

-- | A default implementation of `filter` using `filterMap`.
filterDefault :: forall f a. Filterable f =>
(a -> Boolean) -> f a -> f a
filterDefault = filterMap <<< maybeBool

-- | A default implementation of `filter` using `partition`.
filterDefaultPartition :: forall f a. Filterable f =>
(a -> Boolean) -> f a -> f a
filterDefaultPartition p xs = (partition p xs).yes

-- | A default implementation of `filter` using `partitionMap`.
filterDefaultPartitionMap :: forall f a. Filterable f =>
(a -> Boolean) -> f a -> f a
filterDefaultPartitionMap p xs = (partitionMap (eitherBool p) xs).right

partitioned :: forall f l r. Filterable f =>
f (Either l r) -> { left :: f l, right :: f r }
partitioned = partitionMap id
Expand Down Expand Up @@ -174,4 +205,3 @@ instance filterableMap :: Ord k => Filterable (Map.Map k) where
select (Tuple k x) m = Map.alter (const (p x)) k m

filter p = filterDefault p