Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Golfing it for Haskell

    go :: (Show k, Eq k) => Map k v -> Map k v -> String
    go m1 m2 = map show >>> sort >>> intersperse ", " >>> mconcat >>> orNone
               $ keys m1 ++ keys m2
      where orNone [] = "<none>"
            orNone e  = e


Some quick tips. “intersperse ", " >>> mconcat” is “intercalate ", "”, and I would prefer “.” to “>>>”. Even though left-to-right composition reads better to me, right-to-left is the style.

Gratuitious pointfree version (imports omitted):

    go :: (Show k, Eq k) => Map k v -> Map k v -> String
    go = orNone . intercalate ", " . sort . map show .: (++) `on` keys
      where
      orNone [] = "<none>"
      orNone e = e
      f .: g = (f .) . g
      infixr 8 .:


Nice! I had (.) originally but reorganized it to compare better with the Clojure code. I really like (.:) and on though (which I'm always forgetting about).


That's missing a `nub` or something to get the unique keys. You can also combine the maps first and the results will come out sorted and unique already.... since we're golfing:

    go :: (Show k, Ord k) => Map k v -> Map k v -> String
    go m1 m2 = orNone . intercalate ", " . fmap show . keys $ m1 <> m2
      where orNone "" = "<none>"
            orNone x  = x


Bam! I didn't even think of taking advantage of the monoid.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: