-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatastore.idr
66 lines (53 loc) · 2.37 KB
/
datastore.idr
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
module Main
import Data.Vect
data DataStore : Type where
MkData : (size : Nat) -> (items : Vect size String) -> DataStore
size : DataStore -> Nat
size (MkData size' items') = size'
items : (store : DataStore) -> Vect (size store) String
items (MkData size' items') = items'
addToStore : DataStore -> String -> DataStore
addToStore (MkData size items) newitem = MkData _ (addToData items)
where
addToData : Vect old String -> Vect (S old) String
addToData [] = [newitem]
addToData (x :: xs) = x :: addToData xs
data Command = Add String
| Get Integer
| Quit
| Size
| Search String
parseCommand : List String -> Maybe Command
parseCommand ("add" :: str) = Just (Add (unwords str))
parseCommand ["get", val] = case all isDigit (unpack val) of
False => Nothing
True => Just (Get (cast val))
parseCommand ["quit"] = Just Quit
parseCommand ["size"] = Just Size
parseCommand ["search", str] = Just (Search str)
parseCommand _ = Nothing
parse : (input : String) -> Maybe Command
parse input = parseCommand (words input)
getEntry : (pos : Integer) -> (store: DataStore) -> Maybe (String, DataStore)
getEntry pos store = let store_items = items store
in case integerToFin pos (size store) of
Nothing => Just ("Out of range\n", store)
Just id => Just (index id store_items ++ "\n", store)
search_string : (n: Nat) -> (str:String) -> (store:Vect m String) -> String
search_string n str [] = ""
search_string n str (x :: xs) =
let rest = search_string (n + 1) str xs
in case isInfixOf str x of
True => show n ++ ": " ++ x ++ "\n" ++ rest
False => rest
processInput : DataStore -> String -> Maybe (String, DataStore)
processInput store inp = case parse inp of
Nothing => Just ("Invalid command\n", store)
Just (Add item) =>
Just ("ID: " ++ show (size store) ++ "\n", addToStore store item)
Just (Get pos) => getEntry pos store
Just Size => Just ("Current Size: " ++ show (size store) ++ "\n", store)
Just (Search str) => Just (search_string 0 str (items store), store)
Just Quit => Nothing
main : IO ()
main = replWith (MkData _ []) "Command: " processInput