Skip to content

Instantly share code, notes, and snippets.

@hansonkd
Created August 22, 2012 15:24
Show Gist options
  • Save hansonkd/3426748 to your computer and use it in GitHub Desktop.
Save hansonkd/3426748 to your computer and use it in GitHub Desktop.
{-# 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