Commits

Luke Plant  committed 3265733

Simplification - merged WordMap and WordList

  • Participants
  • Parent commits 750aa7e

Comments (0)

Files changed (1)

File phone2word.hs

 -- at each (position,length).
 type WordMap = [((Int,Int),[B.ByteString])]
 
--- (We might need a better structure than a simple association list for WordMap
--- in the future, but in some functions the association list will be right)
-type WordList = [((Int,Int),[B.ByteString])]
-
 findCombos :: B.ByteString -> WordTree -> [Combo]
 findCombos digits tree =
     let wordMap = makeWordMap digits tree
    where
      makeWordMap :: B.ByteString -> WordTree -> WordMap
      makeWordMap digits tree =
-         makeMap $ do
+         do
            pos <- [0..(totalLength - 1)] -- starting position
            len <- [1..totalLength - pos] -- length
            let words = findWords (substring pos len digits) tree
+           guard (not $ null words)
            return ((pos, len), words)
 
      addDigitWords :: B.ByteString -> WordMap -> WordMap
 
       - in doing the above, ensure that duplicate digit words are not added.
       -}
-         let wordPos = findWordPositions wordMap
+         let wordPos = getWordPositions wordMap
              endPositions = 0 : findEndPositions wordPos
-         in updateMap wordMap $ nub $ do
+         in (++) wordMap $ nub $ do
            pos <- endPositions
            let rightWords = wordsToRightOf pos wordPos
            let (n_pos,n_len) = head $ sort rightWords -- shortest of nearest words
 
      findRoutes :: WordMap -> [Combo]
      findRoutes wordMap =
-         let combos = findRoutes' (findWordEntries wordMap) 0 totalLength
+         let combos = findRoutes' wordMap 0 totalLength
          in
            -- it's possible for boring entries to slip through in the case
            -- where there are no words found at all
              [[seg]] | not $ hasWord seg -> []
              _ -> combos
 
-     findRoutes' :: WordList -> Int -> Int -> [Combo]
+     findRoutes' :: WordMap -> Int -> Int -> [Combo]
      findRoutes' words start end =
          do
            -- find words that start as position 'start':
 
      totalLength = B.length digits
 
-     -- The following functions may have trivial implementations, but they allow
-     -- us to change the datastructure WordMap with minimal changes to the rest.
-     makeMap :: WordList -> WordMap
-     -- We can convert straight back to the WordList by removing entries
-     -- with no words, saving work later.
-     makeMap wl = filter (not . null . snd) wl
-
-     updateMap :: WordMap -> WordList -> WordMap
-     updateMap wordMap additional = wordMap ++ additional
-
      -- get a list of all (position,length) which have words
-     findWordPositions :: WordMap -> [(Int,Int)]
-     findWordPositions wordMap = nub $ map fst $ wordMap
-
-     -- get a list of all entries which have words
-     findWordEntries :: WordMap -> WordList
-     findWordEntries = id
-
+     getWordPositions :: WordMap -> [(Int,Int)]
+     getWordPositions wordMap = nub $ map fst $ wordMap
 
 --
 -- Main