[Haskell-beginners] Comments on Map/Reduce Code

Thomas Bach thbach at students.uni-mainz.de
Fri Jul 13 18:02:02 CEST 2012

Hi Brent,

thanks for your suggestions. I got further suggestions as a private
e-mail and finally came up with the following solution – suggestions

==================== mapper.hs ====================
import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.IO as TLIO

postFix :: TL.Text
postFix = TL.pack "\t1"

formatter :: TL.Text -> TL.Text
formatter x = TL.append x postFix

main = TLIO.interact pipeline
  where words = TL.words
        format = map formatter
        pipeline = TL.unlines . format . words

==================== reducer.hs ===================
import qualified Data.Text.Lazy.IO as TLIO
import qualified Data.Text.Lazy as TL
import qualified Data.List as L
import Data.Function (on)

tuppleize :: TL.Text -> (TL.Text, Int)
tuppleize line = (word, num)
  where words = TL.words line
        word = head words
        num = read . TL.unpack $ last words

group :: Eq a => [(a, b)] -> [[(a, b)]]
group = L.groupBy ((==) `on` fst)

summation :: Num b => [(a, b)] -> (a, b)
summation ((x, y):xs) = (x, y + sum (map snd xs))

formatter :: (TL.Text, Int) -> TL.Text
formatter (w, i) = TL.append w (TL.pack ('\t':show i))

main = TLIO.interact pipeline
  where tuples = TL.lines
        group_tuples = group . map tuppleize
        sum_tuples = map (formatter . summation)
        pipeline = TL.unlines . sum_tuples . group_tuples . tuples

The reducer still throws an error when piping in an empty
newline. But, I'm not sure, what a proper solution for this could be.

On Tue, Jul 10, 2012 at 10:19:48AM -0400, Brent Yorgey wrote:
> On Thu, Jul 05, 2012 at 03:50:32PM +0200, Thomas Bach wrote:
> Instead of using a chain of ($), it's generally considered better
> style to use a chain of (.) with a single $ at the end, like

I often find myself trying different combinations of ($), (.) or
putting things in brackets, when the compiler throws an error on
me. Without really knowing why things sometimes work out and sometimes
don't. I find this part especially confusing!



More information about the Beginners mailing list