我正在嘗試使用現有的MapReduce實現(Real World Haskell)編寫一個簡單的程序。Haskell:使用MapReduce來搜索子串?
作爲使用框架的例子,這裏是一些代碼來計算文件中的單詞數:
module Main where
import Control.Monad (forM_)
import Data.Int (Int64)
import qualified Data.ByteString.Lazy.Char8 as LB
import System.Environment (getArgs)
import LineChunks (chunkedReadWith)
import MapReduce (mapReduce, rdeepseq)
wordCount :: [LB.ByteString] -> Int
wordCount = mapReduce rdeepseq (length . LB.words)
rdeepseq sum
main :: IO()
main = do
args <- getArgs
forM_ args $ \path -> do
numWords <- chunkedReadWith wordCount path
putStrLn $ "Words: " ++ show numWords
我需要使用相同的MapReduce框架編寫一個程序,搜索一些字符串(如「il」),並返回找到它們的行號。例如,輸出可能是:
verILy: found on lines 34, 67, 23
ILlinois: found on lines 1, 2, 56
vILla: found on lines 4, 5, 6
(的「IL」的資本不是必需的。)
我是一個Haskell初學者,沒有任何具體的想法呢。我確實注意到Data.ByteString.Lazy.Char8類有一個成員函數findIndices
。有沒有可能使用這個?
任何代碼或提示正確的方向將不勝感激。
我首先嚐試使用列表http://www.haskell.org/ghc/docs/latest/html/libraries/base/Data-List.html來解決它,然後查看mapreduce是否可用於這個。 – 2013-03-04 13:56:50
謝謝。但是,我需要專門使用MapReduce,所以如果有人能幫助我,那將會很棒! – 2013-03-04 15:51:12