Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 26th, 2012  |  syntax: None  |  size: 1.39 KB  |  hits: 20  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Using map with two lists rather than one. Can you nest?
  2. runParseTest :: String -> String -> IO()
  3.        
  4. -- Get list of files in libraries directory
  5. files <- getDirectoryContents "tests/libraries"
  6. -- Filter out ".." and "." and add path
  7. let names = filter (x -> head x /= '.') files
  8. let libs = ["tests/libraries/" ++ f | f <- names]
  9.        
  10. runParseTest "test1.js" "tests/libraries/test1.js"
  11. runParseTest "test2.js" "tests/libraries/test2.js"
  12. runParseTest "test3.js" "tests/libraries/test3.js"
  13.        
  14. mapM_ (runParseTest "test") libs
  15.        
  16. map ((a,b) -> runParseTest a b) $ zip names libs
  17.        
  18. map (uncurry runParseTest) $ zip names libs
  19.        
  20. zipWith runParseTest names libs
  21.        
  22. > :t zipWithM
  23. zipWithM :: Monad m => (a -> b -> m c) -> [a] -> [b] -> m [c]
  24. > :t zipWithM_
  25. zipWithM_ :: Monad m => (a -> b -> m c) -> [a] -> [b] -> m ()
  26.        
  27. import Data.List (isPrefixOf)
  28.  
  29. ...
  30.  
  31. -- I got rid of `head` because it's a partial function, and I prefer `map` to
  32. -- list comprehensions for simple things    
  33. do files <- getDirectoryContents "tests/libraries"
  34.    let names = filter (not . ("." `isPrefixOf`)) files
  35.        libs  = map ("tests/libraries/" ++) names
  36.    zipWithM_ runParseTest names libs
  37.        
  38. map2 :: (a -> b -> c) -> [a] -> [b] -> [c]
  39. map2 _ [] _          = []
  40. map2 _ _ []          = []
  41. map2 f (a:as) (b:bs) = f a b : map2 f as bs
  42.  
  43. map2M_ :: Monad m => (a -> b -> m c) -> [a] -> [b] -> m ()
  44. map2M_ f as bs =  sequence_ (map2 f as bs)