Last active
December 11, 2015 23:33
-
-
Save jasonmhite/1fa474351af5d14a66ff to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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