Create project

This commit is contained in:
Joscha 2020-02-29 20:51:27 +00:00
commit c32d5faefc
11 changed files with 196 additions and 0 deletions

40
src/Profold/LineTree.hs Normal file
View file

@ -0,0 +1,40 @@
module Profold.LineTree
( LineNode(..)
, Path
, onlyUnfolded
, flatten
, modify
, toggleFold
) where
import qualified Data.Text as T
import qualified Data.Vector as V
import Profold.Util
data LineNode = LineNode
{ lineText :: T.Text
, lineChildren :: V.Vector LineNode
, lineFolded :: Bool
} deriving (Show)
type Path = [Int]
onlyUnfolded :: LineNode -> LineNode
onlyUnfolded ln
| lineFolded ln = ln{lineChildren = V.empty}
| otherwise = ln
flatten :: LineNode -> V.Vector (Path, T.Text)
flatten ln =
V.cons ([], lineText ln) $
V.imap (\i (is, t) -> (i : is, t)) $
V.concatMap flatten $
lineChildren ln
modify :: (LineNode -> LineNode) -> Path -> LineNode -> LineNode
modify f [] ln = f ln
modify f (i:is) ln = ln{lineChildren = modifyAtIndex i (modify f is) $ lineChildren ln}
toggleFold :: Path -> LineNode -> LineNode
toggleFold = modify $ \ln -> ln{lineFolded = not $ lineFolded ln}

9
src/Profold/Util.hs Normal file
View file

@ -0,0 +1,9 @@
module Profold.Util
( modifyAtIndex
) where
import qualified Data.Vector as V
modifyAtIndex :: Int -> (a -> a) -> V.Vector a -> V.Vector a
-- Yes, this function looks ugly, but it's short enough that I don't care.
modifyAtIndex i f v = maybe v (\a -> v V.// [(i, f a)]) (v V.!? i)