-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.hs
39 lines (33 loc) · 895 Bytes
/
Server.hs
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
{-# LANGUAGE RecordWildCards #-}
module Server
( Server
, make
, apply
)
where
import Patch (Patch)
import qualified Patch
import Data.Text (Text)
import Data.Sequence (Seq)
import qualified Data.Sequence as Seq
import qualified Data.Foldable as Foldable
data Server = Server
{ document :: Text
, initialRevision :: Int
, patches :: Seq Patch
}
deriving (Show)
make :: Text -> Int -> Server
make document initialRevision = Server{..}
where
patches = mempty
apply :: Server -> Patch -> Int -> (Server, Patch)
apply server patch revision = (server', patch')
where
server' = server
{ patches = patches server Seq.|> patch'
, document = Patch.apply patch' (document server)
}
(_, patch') = Patch.transform delta patch
delta = Foldable.foldl' Patch.append Patch.empty deltaPatches
deltaPatches = Seq.drop (revision - initialRevision server) (patches server)