Created
October 1, 2016 06:43
-
-
Save rtpg/d8195e682f6bcbcb588b96003754a603 to your computer and use it in GitHub Desktop.
Draw Components (Purescript)
This file contains hidden or 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
import Data.Exists (Exists, mkExists, runExists) | |
type Circle = {x:: Int, y:: Int, r:: Int} | |
type Rectangle = {x:: Int, y:: Int, w:: Int, h:: Int} | |
drawCircle :: Circle -> String | |
drawCircle c = "This is a circle!" | |
drawRectangle :: Rectangle -> String | |
drawRectangle r = "This is a rectangle!" | |
data Component o = Component { | |
data:: o, | |
draw:: o -> String | |
} | |
aCircle = Component { | |
data: {x: 100, y:100, r: 100}, | |
draw: drawCircle | |
} | |
aRectangle = Component { | |
data: {x:100, y:100, w:100, h:100}, | |
draw: drawRectangle | |
} | |
objects :: Array (Exists Component) | |
objects = [ | |
mkExists aCircle, | |
mkExists aRectangle | |
] | |
drawObject :: (Exists Component) -> String | |
drawObject = runExists (\(Component c) -> c.draw c.data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment