Last active
August 4, 2020 18:20
-
-
Save natefaubion/46a249bec56219fda59f8255d5b28c1d to your computer and use it in GitHub Desktop.
run with run
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 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