Skip to content

Instantly share code, notes, and snippets.

@jasonmhite
Last active December 11, 2015 23:33
Show Gist options
  • Save jasonmhite/1fa474351af5d14a66ff to your computer and use it in GitHub Desktop.
Save jasonmhite/1fa474351af5d14a66ff to your computer and use it in GitHub Desktop.
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ExistentialQuantification #-}
import Data.Generics
import Data.List
class Shape a where
name :: a -> String
-- The function that actually does the work
listArgs :: Data a => a -> [String]
listArgs = map showConstr . gmapQ toConstr
render :: Data a => a -> String
render s = intercalate "," (name s : listArgs s) ++ ";"
data Rectangle = Rectangle Float Float Float Float Float Float deriving (Show, Typeable, Data)
instance Shape RPP where
name = const "RPP"
...Which gives:
render $ Rectangle 1.0 2.0 1.0 2.0 1.0 2.0
>> "RPP,1.0,2.0,1.0,2.0,1.0,2.0;"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment