Skip to content

Instantly share code, notes, and snippets.

@natefaubion
Last active August 4, 2020 18:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save natefaubion/46a249bec56219fda59f8255d5b28c1d to your computer and use it in GitHub Desktop.
Save natefaubion/46a249bec56219fda59f8255d5b28c1d to your computer and use it in GitHub Desktop.
run with run
module Main where
import Prelude
import Effect
import Effect.Console
import Run
import Run.Except
import Data.Functor.Variant as VariantF
import Data.String as String
import Prim.Row as Row
import TryPureScript
data FileF a = GetFile String (String -> a) -- Given a filename, yield the file contents
derive instance functorFileF :: Functor FileF
type FILE = FProxy FileF
getFile path = lift (SProxy :: _ "file") $ GetFile path identity
myLines = do
contents <- getFile "myfile.txt" -- change this to something else
pure $ String.split (String.Pattern "\n") contents
runFiles
:: forall r rx a
. Run (file :: FILE, effect :: EFFECT, except :: EXCEPT String | r) a
-> Run (effect :: EFFECT, except :: EXCEPT String | r) a
runFiles =
run $ VariantF.on (SProxy :: _ "file")
do \(GetFile path next) -> do
liftEffect $ log $ "Get path: " <> path
if path == "myfile.txt" then
pure $ next "These \n are my contents \n for this file"
else
pure $ throw "Oh no!"
send
test :: Effect (Array String)
test =
runBaseEffect
$ catch (\err -> pure [err])
$ runFiles myLines
main = render =<< withConsole do
result <- test
logShow result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment