mirror of
https://github.com/Noratrieb/idris-learning.git
synced 2026-01-16 14:05:02 +01:00
data store search
This commit is contained in:
parent
eda0cba251
commit
94ed1401fe
4 changed files with 20 additions and 1 deletions
BIN
chapter2/average
Normal file
BIN
chapter2/average
Normal file
Binary file not shown.
BIN
chapter2/hello
Normal file
BIN
chapter2/hello
Normal file
Binary file not shown.
|
|
@ -43,3 +43,12 @@ mutual
|
||||||
isOdd (S k) = isEven k
|
isOdd (S k) = isEven k
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
displayList : Show a => List a -> String
|
||||||
|
displayList xs = "[" ++ showItems xs ++ "]"
|
||||||
|
where
|
||||||
|
showItems : Show a => List a -> String
|
||||||
|
showItems [] = ""
|
||||||
|
showItems (x :: []) = show x
|
||||||
|
showItems (x :: next) = show x ++ "," ++ showItems next
|
||||||
|
|
|
||||||
|
|
@ -18,10 +18,16 @@ addToStore (MkData size items) newitem = MkData _ (addToData items)
|
||||||
addToData [] = [newitem]
|
addToData [] = [newitem]
|
||||||
addToData (x :: xs) = x :: addToData xs
|
addToData (x :: xs) = x :: addToData xs
|
||||||
|
|
||||||
|
search : DataStore -> String -> List String
|
||||||
|
search store query = let predicate = isInfixOf query
|
||||||
|
allItems = toList (items store) in -- can't be bothered to work with dependant pairs
|
||||||
|
filter predicate allItems
|
||||||
|
|
||||||
|
|
||||||
data Command = Add String
|
data Command = Add String
|
||||||
| Get Integer
|
| Get Integer
|
||||||
| Size
|
| Size
|
||||||
|
| Search String
|
||||||
| Quit
|
| Quit
|
||||||
|
|
||||||
parseCommand : (cmd : String) -> (args : String) -> Maybe Command
|
parseCommand : (cmd : String) -> (args : String) -> Maybe Command
|
||||||
|
|
@ -31,6 +37,7 @@ parseCommand "get" val = case all isDigit (unpack val) of
|
||||||
False => Nothing
|
False => Nothing
|
||||||
parseCommand "quit" args = Just Quit
|
parseCommand "quit" args = Just Quit
|
||||||
parseCommand "size" args = Just Size
|
parseCommand "size" args = Just Size
|
||||||
|
parseCommand "search" args = Just (Search args)
|
||||||
parseCommand _ _ = Nothing
|
parseCommand _ _ = Nothing
|
||||||
|
|
||||||
parse : (input : String) -> Maybe Command
|
parse : (input : String) -> Maybe Command
|
||||||
|
|
@ -52,6 +59,9 @@ processInput store inp = case parse inp of
|
||||||
Just ("ID " ++ show (size store) ++ "\n", addToStore store item)
|
Just ("ID " ++ show (size store) ++ "\n", addToStore store item)
|
||||||
Just (Get pos) => getEntry pos store
|
Just (Get pos) => getEntry pos store
|
||||||
Just Size => Just ("Size: " ++ show (size store) ++ "\n", store)
|
Just Size => Just ("Size: " ++ show (size store) ++ "\n", store)
|
||||||
|
Just (Search query) => case (search store query) of
|
||||||
|
[] => Just ("Item Not Found\n", store)
|
||||||
|
items => Just (show items ++ "\n", store)
|
||||||
Just Quit => Nothing
|
Just Quit => Nothing
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue