-
Notifications
You must be signed in to change notification settings - Fork 31
split #102
base: master
Are you sure you want to change the base?
split #102
Conversation
probably nlogn
I think this is good, but we should probably try to implement it by walking the tree, which would be more efficient. This is great for a first pass though, thanks! |
@@ -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 } |
There was a problem hiding this comment.
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.
where | ||
mapify {init: ls, rest: gs} = | ||
{less: fromFoldable ls, | ||
greater: fromFoldable $ LL.dropWhile (\(Tuple k' _) -> k' == k) gs} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 :)
addresses #26
Note that this probably has nlogn rather than logn performance, as in Haskell's collections. It can probably be improved very significantly. Would like to add some benchmarks (#101) before doing so.