@@ -6,8 +6,12 @@ module Data.Filterable
6
6
, filter
7
7
, eitherBool
8
8
, partitionDefault
9
+ , partitionDefaultFilter
10
+ , partitionDefaultFilterMap
9
11
, maybeBool
10
12
, filterDefault
13
+ , filterDefaultPartition
14
+ , filterDefaultPartitionMap
11
15
, partitioned
12
16
, filtered
13
17
, cleared
@@ -19,9 +23,10 @@ import Data.Array (partition, mapMaybe, filter) as Array
19
23
import Data.Either (Either (..))
20
24
import Data.Foldable (foldl , foldr )
21
25
import Data.Functor (class Functor )
26
+ import Data.HeytingAlgebra (not )
22
27
import Data.List (List (..), filter , mapMaybe ) as List
23
- import Data.Maybe (Maybe (..))
24
28
import Data.Map (Map , empty , insert , alter , toUnfoldable ) as Map
29
+ import Data.Maybe (Maybe (..))
25
30
import Data.Monoid (class Monoid , mempty )
26
31
import Data.Semigroup ((<>))
27
32
import Data.Tuple (Tuple (..))
@@ -60,23 +65,49 @@ eitherBool :: forall a.
60
65
(a -> Boolean ) -> a -> Either a a
61
66
eitherBool p x = if p x then Right x else Left x
62
67
68
+ -- | Upgrade a boolean-style predicate to a maybe-style predicate mapping.
69
+ maybeBool :: forall a .
70
+ (a -> Boolean ) -> a -> Maybe a
71
+ maybeBool p x = if p x then Just x else Nothing
72
+
63
73
-- | A default implementation of `partition` using `partitionMap`.
64
74
partitionDefault :: forall f a . Filterable f =>
65
75
(a -> Boolean ) -> f a -> { no :: f a , yes :: f a }
66
76
partitionDefault p xs =
67
77
let o = partitionMap (eitherBool p) xs
68
78
in {no: o.left, yes: o.right}
69
79
70
- -- | Upgrade a boolean-style predicate to a maybe-style predicate mapping.
71
- maybeBool :: forall a .
72
- (a -> Boolean ) -> a -> Maybe a
73
- maybeBool p x = if p x then Just x else Nothing
80
+ -- | A default implementation of `partition` using `filter`. Note that this is
81
+ -- | almost certainly going to be suboptimal compared to direct implementations.
82
+ partitionDefaultFilter :: forall f a . Filterable f =>
83
+ (a -> Boolean ) -> f a -> { no :: f a , yes :: f a }
84
+ partitionDefaultFilter p xs = { yes: filter p xs, no: filter (not p) xs }
85
+
86
+ -- | A default implementation of `partition` using `filterMap`. Note that this
87
+ -- | is almost certainly going to be suboptimal compared to direct
88
+ -- | implementations.
89
+ partitionDefaultFilterMap :: forall f a . Filterable f =>
90
+ (a -> Boolean ) -> f a -> { no :: f a , yes :: f a }
91
+ partitionDefaultFilterMap p xs =
92
+ { yes: filterMap (maybeBool p) xs
93
+ , no: filterMap (maybeBool (not p)) xs
94
+ }
74
95
75
96
-- | A default implementation of `filter` using `filterMap`.
76
97
filterDefault :: forall f a . Filterable f =>
77
98
(a -> Boolean ) -> f a -> f a
78
99
filterDefault = filterMap <<< maybeBool
79
100
101
+ -- | A default implementation of `filter` using `partition`.
102
+ filterDefaultPartition :: forall f a . Filterable f =>
103
+ (a -> Boolean ) -> f a -> f a
104
+ filterDefaultPartition p xs = (partition p xs).yes
105
+
106
+ -- | A default implementation of `filter` using `partitionMap`.
107
+ filterDefaultPartitionMap :: forall f a . Filterable f =>
108
+ (a -> Boolean ) -> f a -> f a
109
+ filterDefaultPartitionMap p xs = (partitionMap (eitherBool p) xs).right
110
+
80
111
partitioned :: forall f l r . Filterable f =>
81
112
f (Either l r ) -> { left :: f l , right :: f r }
82
113
partitioned = partitionMap id
@@ -174,4 +205,3 @@ instance filterableMap :: Ord k => Filterable (Map.Map k) where
174
205
select (Tuple k x) m = Map .alter (const (p x)) k m
175
206
176
207
filter p = filterDefault p
177
-
0 commit comments