-insertNodeAt [] a b = a ⊕ b
-insertNodeAt (x:[]) a b = Branch (singleton (x, a)) ⊕ b
-insertNodeAt (x:xs) a b = insertNodeAt xs a (∅) ⊕ b
+insertNodeAt p a b
+ = case front p of
+ Nothing → a ⊕ b
+ Just (x, xs)
+ | null xs → Branch (singleton (x, a)) ⊕ b
+ | otherwise → insertNodeAt xs a (∅) ⊕ b
+
+instance Foldable ResourceTree (PathSegments, ResourceNode) where
+ foldMap f (Root n) = go (∅) n
+ where
+ go p (Greedy r m) = go' p m ⊕ f (p, Greedy r (∅))
+ go p (NonGreedy r m) = go' p m ⊕ f (p, NonGreedy r (∅))
+ go p (Branch m) = go' p m
+
+ go' p = foldMap $ \(s, n') → go (p `snoc` s) n'
+
+ null (Root (Greedy _ _)) = False
+ null (Root (NonGreedy _ _)) = False
+ null (Root (Branch m)) = null m
+
+instance Collection ResourceTree (PathSegments, ResourceNode) where
+ {-# INLINE filter #-}
+ filter f = fromList ∘ filter f ∘ fromFoldable