Last active
August 29, 2015 14:11
-
-
Save TrevorBasinger/14221a3d5219dc3c5991 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
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