Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass children to order function in treeSplice #2

Merged
merged 1 commit into from
Dec 27, 2022
Merged
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: 6 additions & 4 deletions src/Heist/Extra/Splices/Tree.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ treeSplice ::
forall a sortKey.
(Ord sortKey) =>
-- | How to sort children
(NonEmpty a -> sortKey) ->
(NonEmpty a -> [Tree a] -> sortKey) ->
-- | Input tree
[Tree a] ->
-- | How to render a (sub-)tree root
Expand All @@ -19,14 +19,16 @@ treeSplice ::
treeSplice =
go []
where
go :: [a] -> (NonEmpty a -> sortKey) -> [Tree a] -> (NonEmpty a -> [Tree a] -> H.Splices (HI.Splice Identity)) -> HI.Splice Identity
go :: [a] -> (NonEmpty a -> [Tree a] -> sortKey) -> [Tree a] -> (NonEmpty a -> [Tree a] -> H.Splices (HI.Splice Identity)) -> HI.Splice Identity
go pars sortKey trees childSplice = do
let extendPars x = maybe (one x) (<> one x) $ nonEmpty pars
flip foldMapM (sortOn (sortKey . extendPars . rootLabel) trees) $ \(Node lbl children) -> do
let sorter x = sortKey (extendPars $ rootLabel x) (subForest x)
flip foldMapM (sortOn sorter trees) $ \(Node lbl children) -> do
Comment on lines -25 to +26
Copy link
Owner

Choose a reason for hiding this comment

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

let sortKey' = flip sortKey (subForest x)? Then the existing code would require minimal changes here, and below.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But where would the x come from?

Copy link
Owner

Choose a reason for hiding this comment

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

Oh, right. Okay I'll review all your PRs by this weekend.

HI.runChildrenWith $ do
let herePath = extendPars lbl
childSplice herePath children
"has-children" ## Heist.ifElseISplice (not . null $ children)
let childrenSorted = sortOn (sortKey . (herePath <>) . one . rootLabel) children
let childrenSorter x = sortKey (herePath <> one (rootLabel x)) (subForest x)
let childrenSorted = sortOn childrenSorter children
"children"
## go (toList herePath) sortKey childrenSorted childSplice