Skip to content

Commit

Permalink
Implement fold in Data/PSQ.hs
Browse files Browse the repository at this point in the history
  • Loading branch information
sfindeisen committed Jun 7, 2014
1 parent a5e54f7 commit c0eb16e
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion Data/PSQ.hs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ module Data.PSQ

-- * Traversals
-- , map
-- , fold
, fold
) where

import Prelude ()
Expand Down Expand Up @@ -246,6 +246,29 @@ toDescLists q = case tourView q of
Single (E k p v) -> singleSequ (k, p, v)
tl `Play` tr -> toDescLists tr <> toDescLists tl

------------------------------------------------------------------------
-- Traversals

fold :: (Ord p) => (k -> p -> v -> a -> a) -> a -> PSQ k p v -> a
fold f acc =
let
fold_elem a (E k p v) = f k p v a

fold_tree acc Start = acc
fold_tree acc (LLoser _ e lt _ rt) = fold_tree' acc (e, lt, rt)
fold_tree acc (RLoser _ e lt _ rt) = fold_tree' acc (e, lt, rt)

fold_tree' acc ((E k p v), lt, rt) =
let
lta = fold_tree acc lt
rta = fold_tree lta rt
in
f k p v rta
go Void = acc
go (Winner _ t _) = fold_tree acc t
in
go

------------------------------------------------------------------------
-- Min

Expand Down

0 comments on commit c0eb16e

Please sign in to comment.