Simplify terminals, move towards using tries for commands

This commit is contained in:
2025-08-16 01:11:29 +01:00
parent e04d4fea31
commit 73cdf42e4a
12 changed files with 377 additions and 398 deletions
+23
View File
@@ -3,6 +3,7 @@
module SimpleTrie where
import Data.Monoid
import Control.Lens
import qualified Data.Map.Strict as M
@@ -30,6 +31,28 @@ lookupTrie :: Ord a => [a] -> Trie a b -> Maybe b
lookupTrie (k : ks) t = _trieChildren t M.!? k >>= lookupTrie ks
lookupTrie [] t = _trieMVal t
--nextTrie :: Ord a => [a] -> Trie a b -> Maybe (b, Trie a b)
nextTrie :: [a] -> Trie a b -> Maybe (b, Trie a b)
nextTrie (k:ks) t = undefined
nextTrie [] t = Nothing
splitLookupTrie :: Ord a => [a] -> Trie a b -> (Trie a b,Maybe b,Trie a b)
splitLookupTrie (k:ks) t@(Trie x xs) = case m of
Nothing -> (Trie x l,Nothing,Trie x r)
Just t' -> let (l',m',r') = splitLookupTrie ks t'
in (Trie x (M.insert k l' l),m', Trie x (M.insert k r' r))
where
(l,m,r) = M.splitLookup k xs
splitLookupTrie [] t@(Trie x xs) = (Trie Nothing mempty,x, Trie Nothing xs)
-- the above would be better if it removed empty trie strings; the left trie
-- will always be empty and the right trie might be empty.
-- I cannot think of a smart way to do this
firstTrie :: Ord a => Trie a b -> Maybe b
firstTrie t = case t ^. trieMVal of
Just x -> Just x
Nothing -> getFirst $ foldMap (First . firstTrie) (t ^. trieChildren)
-- # OPTIONS -Wno-incomplete-uni-patterns #-}
multiLookupTrie :: Ord a => [a] -> Trie a b -> [([a], b)]
multiLookupTrie xs t@(Trie my ch)