Created
August 22, 2012 15:24
-
-
Save hansonkd/3426748 to your computer and use it in GitHub Desktop.
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
{-# LANGUAGE NoImplicitPrelude #-} | |
module Console where | |
import Language.Fay.FFI | |
import Language.Fay.Prelude | |
data MyData = MyData { xVar :: Int, yVar :: Int } | |
instance Foreign MyData | |
myData :: MyData | |
myData = MyData { yVar = 3, xVar = 9 } | |
myFunction :: MyData -> MyData -> Int | |
myFunction a b = (xVar a) + (xVar b) | |
main = do | |
jsonSerialized <- toJSON myData | |
jsonDeserialized <- toMyData jsonSerialized | |
fromStringData <- toMyData "{\"xVar\":3,\"yVar\":-1}" | |
printInt $ myFunction jsonDeserialized fromStringData | |
-- | Print using console.log. | |
print :: String -> Fay () | |
print = ffi "console.log(%1)" | |
printInt :: Int -> Fay () | |
printInt = ffi "console.log(%1)" | |
-- | Convert | |
toMyData :: String -> Fay MyData | |
toMyData = ffi "JSON.parse(%1)" | |
toJSON :: MyData -> Fay String | |
toJSON = ffi "JSON.stringify(%1)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment