Skip to content
Open
Show file tree
Hide file tree
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
18 changes: 8 additions & 10 deletions src/Data/Dependent/Map.hs
Original file line number Diff line number Diff line change
Expand Up @@ -767,11 +767,11 @@ isProperSubmapOfBy f t1 t2
--------------------------------------------------------------------}

-- | /O(n)/. Filter all values that satisfy the predicate.
filter :: GCompare k => (forall v. f v -> Bool) -> DMap k f -> DMap k f
filter :: (forall v. f v -> Bool) -> DMap k f -> DMap k f
filter f = filterWithKey (const f)

-- | /O(n)/. Filter all keys\/values that satisfy the predicate.
filterWithKey :: GCompare k => (forall v. k v -> f v -> Bool) -> DMap k f -> DMap k f
filterWithKey :: (forall v. k v -> f v -> Bool) -> DMap k f -> DMap k f
filterWithKey p = go
where
go Tip = Tip
Expand All @@ -786,10 +786,10 @@ filterWithKey p = go
-- | /O(n)/. Partition the map according to a predicate. The first
-- map contains all elements that satisfy the predicate, the second all
-- elements that fail the predicate. See also 'split'.
partitionWithKey :: GCompare k => (forall v. k v -> f v -> Bool) -> DMap k f -> (DMap k f, DMap k f)
partitionWithKey :: (forall v. k v -> f v -> Bool) -> DMap k f -> (DMap k f, DMap k f)
partitionWithKey p0 m0 = toPair (go p0 m0)
where
go :: GCompare k => (forall v. k v -> f v -> Bool) -> DMap k f -> (DMap k f :*: DMap k f)
go :: (forall v. k v -> f v -> Bool) -> DMap k f -> (DMap k f :*: DMap k f)
go _ Tip = (Tip :*: Tip)
go p (Bin _ kx x l r)
| p kx x = (combine kx x l1 r1 :*: merge l2 r2)
Expand All @@ -799,11 +799,11 @@ partitionWithKey p0 m0 = toPair (go p0 m0)
(r1 :*: r2) = go p r

-- | /O(n)/. Map values and collect the 'Just' results.
mapMaybe :: GCompare k => (forall v. f v -> Maybe (g v)) -> DMap k f -> DMap k g
mapMaybe :: (forall v. f v -> Maybe (g v)) -> DMap k f -> DMap k g
mapMaybe f = mapMaybeWithKey (const f)

-- | /O(n)/. Map keys\/values and collect the 'Just' results.
mapMaybeWithKey :: GCompare k => (forall v. k v -> f v -> Maybe (g v)) -> DMap k f -> DMap k g
mapMaybeWithKey :: (forall v. k v -> f v -> Maybe (g v)) -> DMap k f -> DMap k g
mapMaybeWithKey f = go
where
go Tip = Tip
Expand All @@ -812,12 +812,10 @@ mapMaybeWithKey f = go
Nothing -> merge (go l) (go r)

-- | /O(n)/. Map keys\/values and separate the 'Left' and 'Right' results.
mapEitherWithKey :: GCompare k =>
(forall v. k v -> f v -> Either (g v) (h v)) -> DMap k f -> (DMap k g, DMap k h)
mapEitherWithKey :: (forall v. k v -> f v -> Either (g v) (h v)) -> DMap k f -> (DMap k g, DMap k h)
mapEitherWithKey f0 = toPair . go f0
where
go :: GCompare k
=> (forall v. k v -> f v -> Either (g v) (h v))
go :: (forall v. k v -> f v -> Either (g v) (h v))
-> DMap k f -> (DMap k g :*: DMap k h)
go _ Tip = (Tip :*: Tip)
go f (Bin _ kx x l r) = case f kx x of
Expand Down
6 changes: 3 additions & 3 deletions src/Data/Dependent/Map/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ lookupAssoc sk = withSome sk $ \k ->
{--------------------------------------------------------------------
Combine
--------------------------------------------------------------------}
combine :: GCompare k => k v -> f v -> DMap k f -> DMap k f -> DMap k f
combine :: k v -> f v -> DMap k f -> DMap k f -> DMap k f
combine kx x Tip r = insertMin kx x r
combine kx x l Tip = insertMax kx x l
combine kx x l@(Bin sizeL ky y ly ry) r@(Bin sizeR kz z lz rz)
Expand Down Expand Up @@ -357,7 +357,7 @@ trimLookupLo lo cmphi t@(Bin _ kx x l r)
[filterGt k t] filter all keys >[k] from tree [t]
[filterLt k t] filter all keys <[k] from tree [t]
--------------------------------------------------------------------}
filterGt :: GCompare k => (Some k -> Ordering) -> DMap k f -> DMap k f
filterGt :: (Some k -> Ordering) -> DMap k f -> DMap k f
filterGt cmp = go
where
go Tip = Tip
Expand All @@ -366,7 +366,7 @@ filterGt cmp = go
GT -> go r
EQ -> r

filterLt :: GCompare k => (Some k -> Ordering) -> DMap k f -> DMap k f
filterLt :: (Some k -> Ordering) -> DMap k f -> DMap k f
filterLt cmp = go
where
go Tip = Tip
Expand Down