Skip to content
This repository has been archived by the owner on Oct 4, 2020. It is now read-only.

split #102

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

split #102

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
10 changes: 10 additions & 0 deletions src/Data/Map.purs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ module Data.Map
, unions
, size
, mapWithKey
, split
) where

import Prelude
import Data.Eq (class Eq1)
import Data.Foldable (foldl, foldMap, foldr, class Foldable)
import Data.List (List(..), (:), length, nub)
import Data.List.Lazy as LL
import Data.Maybe (Maybe(..), maybe, isJust, fromMaybe)
import Data.Monoid (class Monoid)
import Data.Ord (class Ord1)
Expand Down Expand Up @@ -451,3 +453,11 @@ mapWithKey :: forall k v v'. (k -> v -> v') -> Map k v -> Map k v'
mapWithKey _ Leaf = Leaf
mapWithKey f (Two left k v right) = Two (mapWithKey f left) k (f k v) (mapWithKey f right)
mapWithKey f (Three left k1 v1 mid k2 v2 right) = Three (mapWithKey f left) k1 (f k1 v1) (mapWithKey f mid) k2 (f k2 v2) (mapWithKey f right)

-- | Divide into two maps of keys less and greater than the provided argument.
split :: forall k v. Ord k => k -> Map k v -> { less :: Map k v, greater :: Map k v }
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't the right be "greater or equal"? Maybe lt and ge would be good label names.

split k = mapify <<< LL.span (\(Tuple k' _) -> k' < k) <<< toAscUnfoldable
where
mapify {init: ls, rest: gs} =
{less: fromFoldable ls,
greater: fromFoldable $ LL.dropWhile (\(Tuple k' _) -> k' == k) gs}
Copy link
Contributor

Choose a reason for hiding this comment

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

Or instead of dropping equal elements, why not add an eq key too?

Copy link
Contributor Author

@matthewleon matthewleon Jun 4, 2017

Choose a reason for hiding this comment

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

That makes good sense to me. Going to wait on it until I write a proper tree-traversing version, though, which should take us from nlogn to logn :)

6 changes: 6 additions & 0 deletions test/Test/Data/Map.purs
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,9 @@ mapTests = do
toList = M.toUnfoldable :: forall k v. M.Map k v -> List (Tuple k v)
resultViaLists = m # toList # map (\(Tuple k v) → Tuple k (f k v)) # M.fromFoldable
in resultViaMapWithKey === resultViaLists

log "split is correct"
quickCheck $ \(TestMap m :: TestMap String Int) s -> let
{less: ls, greater: gs} = M.split s m
in (all (_ < s) $ M.keys ls) && (all (_ > s) $ M.keys gs) &&
M.size ls + M.size gs == M.size (M.delete s m)