summaryrefslogtreecommitdiff
path: root/lec07/Buffer.hs
diff options
context:
space:
mode:
Diffstat (limited to 'lec07/Buffer.hs')
-rw-r--r--lec07/Buffer.hs28
1 files changed, 28 insertions, 0 deletions
diff --git a/lec07/Buffer.hs b/lec07/Buffer.hs
new file mode 100644
index 0000000..6eb55af
--- /dev/null
+++ b/lec07/Buffer.hs
@@ -0,0 +1,28 @@
+module Buffer where
+
+-- Type class for data structures that can represent the text buffer
+-- of an editor.
+
+class Buffer b where
+
+ -- | Convert a buffer to a String.
+ toString :: b -> String
+
+ -- | Create a buffer from a String.
+ fromString :: String -> b
+
+ -- | Extract the nth line (0-indexed) from a buffer. Return Nothing
+ -- for out-of-bounds indices.
+ line :: Int -> b -> Maybe String
+
+ -- | @replaceLine n ln buf@ returns a modified version of @buf@,
+ -- with the @n@th line replaced by @ln@. If the index is
+ -- out-of-bounds, the buffer should be returned unmodified.
+ replaceLine :: Int -> String -> b -> b
+
+ -- | Compute the number of lines in the buffer.
+ numLines :: b -> Int
+
+ -- | Compute the value of the buffer, i.e. the amount someone would
+ -- be paid for publishing the contents of the buffer.
+ value :: b -> Int