blob: cac88ac53c84aed4f2e4f23048677d2801e08a4d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
{-# LANGUAGE ViewPatterns, OverloadedStrings, FlexibleContexts #-}
module Utils
( showEntity
, apply, apply'
, entities
, takeR
) where
import Data.Text (Text)
import qualified Data.Text as Text
import Data.PQueue.Prio.Max (MaxPQueue)
import qualified Data.PQueue.Prio.Max as MaxPQueue
import Data.Monoid (Monoid(..), (<>))
import Control.Monad.State.Class
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
showEntity (Entity name number)
| (Just (show -> n)) <- number = name <> " № " <> Text.pack n
| otherwise = name
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 -> 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
|