Commit 83dbc9c2 authored by Georgi Angelov's avatar Georgi Angelov
Browse files

Refactored helper functions into Utils.hs

parent 7d5cd2f4
import Happstack.Server import Happstack.Server
import Solver import Solver
import Utils
import Control.Monad (msum) import Control.Monad (msum)
myConf = Conf serverConf = Conf
{ port = 5001 { port = 5001
, validator = Nothing , validator = Nothing
, logAccess = Just logMAccess , logAccess = Just logMAccess
...@@ -11,7 +11,7 @@ myConf = Conf ...@@ -11,7 +11,7 @@ myConf = Conf
, threadGroup = threadGroup nullConf , threadGroup = threadGroup nullConf
} }
main = simpleHTTP myConf $ msum [ dir "solve" $ path $ \clue -> path $ \wordLength main = simpleHTTP serverConf $ msum [ dir "solve" $ path $ \clue -> path $ \wordLength
-> ok $ show (solveReturn (clue, wordLength)) -> ok $ show (solveReturn (clue, wordLength))
, dir "solveAll" $ path $ \clue -> path $ \wordLength , dir "solveAll" $ path $ \clue -> path $ \wordLength
-> ok $ show (solveAllReturn (clue, wordLength)) -> ok $ show (solveAllReturn (clue, wordLength))
...@@ -20,19 +20,3 @@ main = simpleHTTP myConf $ msum [ dir "solve" $ path $ \clue -> path $ \wordLeng ...@@ -20,19 +20,3 @@ main = simpleHTTP myConf $ msum [ dir "solve" $ path $ \clue -> path $ \wordLeng
, dir "solveWithAnswers" $ path $ \clue -> path $ \wordLength -> path $ \answers , dir "solveWithAnswers" $ path $ \clue -> path $ \wordLength -> path $ \answers
-> ok $ show (solveAnswers clue wordLength answers) -> ok $ show (solveAnswers clue wordLength answers)
] ]
solveAnswers clue wordLength answers
= solveEach clue wordLength (wordsWhen (==',') answers)
where
solveEach _ _ [] = []
solveEach clue wordLength (a:as)
| null (solveWithAnswerReturn (clue, wordLength) a)
= solveEach clue wordLength as
| otherwise
= head (solveWithAnswerReturn (clue, wordLength) a) : solveEach clue wordLength as
wordsWhen :: (Char -> Bool) -> String -> [String]
wordsWhen p s = case dropWhile p s of
"" -> []
s' -> w : wordsWhen p s''
where (w, s'') = break p s'
import Solver
-- Receives a list of possible answers to a clue and returns a list of all valid answers
solveAnswers clue wordLength answers
= solveEach clue wordLength (wordsWhen (==',') answers)
where
solveEach _ _ [] = []
solveEach clue wordLength (a:as)
| null (solveWithAnswerReturn (clue, wordLength) a)
= solveEach clue wordLength as
| otherwise
= head (solveWithAnswerReturn (clue, wordLength) a) : solveEach clue wordLength as
-- Helper function to split string by character into a list of strings
wordsWhen :: (Char -> Bool) -> String -> [String]
wordsWhen p s = case dropWhile p s of
"" -> []
s' -> w : wordsWhen p s''
where (w, s'') = break p s'
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment