Skip to content

Instantly share code, notes, and snippets.

@TrevorBasinger
Last active August 29, 2015 14:11
Show Gist options
  • Save TrevorBasinger/14221a3d5219dc3c5991 to your computer and use it in GitHub Desktop.
Save TrevorBasinger/14221a3d5219dc3c5991 to your computer and use it in GitHub Desktop.
module Main where
import Control.Monad.Eff
import Data.Either
import Data.Foreign
import Data.Function
import Debug.Trace
type ErrorCode = String
type FilePath = String
type M eff = Eff (fs :: FS | eff)
foreign import data FS :: !
foreign import readFileImpl
"""
function readFileImpl (path, onSuccess, onFailure) {
return function () {
require ('fs').readFile (path,
{ encoding: 'utf-8' },
function (error, data) {
if (error) {
onFailure (error.code);
}
else {
onSuccess (data);
}
});
};
};
""" :: forall eff. Fn3 FilePath
(String -> M eff Unit)
(ErrorCode -> M eff Unit)
(M eff Unit)
readFile :: forall eff. FilePath ->
(Either ErrorCode String -> M eff Unit) ->
M eff Unit
readFile path cb = runFn3 readFileImpl path (cb <<< Right) (cb <<< Left)
main = readFile "/tmp/1.txt" print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment