From 6ce5de5a227267e359aa3c38344e3858f035287d Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Sun, 8 May 2016 01:05:00 +0200 Subject: command interpretation --- src/Utils.hs | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) (limited to 'src/Utils.hs') diff --git a/src/Utils.hs b/src/Utils.hs index 6c8f0c1..cac88ac 100644 --- a/src/Utils.hs +++ b/src/Utils.hs @@ -4,6 +4,7 @@ module Utils ( showEntity , apply, apply' , entities + , takeR ) where import Data.Text (Text) @@ -20,6 +21,9 @@ import Control.Monad.Writer.Class import Data.Set (Set) import qualified Data.Set as Set +import Data.Sequence (Seq, ViewR(..), (|>)) +import qualified Data.Sequence as Seq + import Types showEntity :: Entity -> Text @@ -27,14 +31,19 @@ showEntity (Entity name number) | (Just (show -> n)) <- number = name <> " № " <> Text.pack n | otherwise = name -apply' :: (MonadState Sequence m, MonadWriter History m) => Alteration -> m () -apply' alteration = do - (newSt, hist) <- apply alteration <$> get - tell hist - put newSt +apply' :: MonadState Context m => Alteration -> Comment -> m () +apply' alteration comment = modify $ onCtx (apply alteration comment) + where + onCtx f ctx = let (sequence', history') = f $ ctxSequence ctx in ctx { ctxSequence = sequence', ctxHistory = ctxHistory ctx <> history' } -apply :: Alteration -> Sequence -> (Sequence, History) -apply alteration seq = undefined +apply :: Alteration -> Comment -> Sequence -> (Sequence, History) +apply alteration comment seq = undefined entities :: MonadState Sequence m => m (Set Entity) entities = Set.fromList . MaxPQueue.elems <$> get + +takeR :: Integer -> Seq a -> Seq a +takeR _ (Seq.viewr -> EmptyR) = Seq.empty +takeR n (Seq.viewr -> (xs :> x)) + | n <= 0 = Seq.empty + | otherwise = takeR (n - 1) xs |> x -- cgit v1.2.3