Overengineering

replies 27 February 2008 | 0 Comments

Douglas, you’re not alone.

import Data.List (sortBy)
import Data.Function (on)
import Data.Maybe (mapMaybe)
import Control.Monad.Instances
 
gizzabuzz pairs combiner = zipWith ($) (cycle funcs) [1..]
	where 
	sortedPairs = sortBy (compare `on` fst) pairs
	funcs = map (\n -> display $ mapMaybe (filterOut n) sortedPairs) [1..foldr1 lcm $ map fst $ sortedPairs]
	display [] = show
	display xs = foldr1 combiner . sequence (map const xs)
	filterOut n (x,y)
		| n `mod` x == 0 = Just y
		| otherwise      = Nothing
 
fizzbuzz = gizzabuzz [(3,"Fizz"),(5,"Buzz")] (++)

Tagged in , , , , ,

Leave a Reply