Created
July 23, 2017 21:01
-
-
Save myme/2c6bc3ee67c66b31e97226fc59f5cd8b to your computer and use it in GitHub Desktop.
Purescript Node Streams
This file contains 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
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec hendrerit tempor | |
tellus. Donec pretium posuere tellus. Proin quam nisl, tincidunt et, mattis | |
eget, convallis nec, purus. Cum sociis natoque penatibus et magnis dis | |
parturient montes, nascetur ridiculus mus. Nulla posuere. Donec vitae dolor. | |
Nullam tristique diam non turpis. Cras placerat accumsan nulla. Nullam rutrum. | |
Nam vestibulum accumsan nisl. | |
This file contains 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
╭─mmyrseth@set ~/projects/capture ‹master*› | |
╰─$ pulp run -m StreamTest | |
* Building project in /home/mmyrseth/projects/capture | |
* Build successful. | |
log: Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec hendrerit tempor | |
tellus. Donec pretium posuere tellus. Proin quam nisl, tincidunt et, mattis | |
eget, convallis nec, purus. Cum sociis natoque penatibus et magnis dis | |
parturient montes, nascetur ridiculus mus. Nulla posuere. Donec vitae dolor. | |
Nullam tristique diam non turpis. Cras placerat accumsan nulla. Nullam rutrum. | |
Nam vestibulum accumsan nisl. | |
Done! |
This file contains 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 StreamTest where | |
import Prelude | |
import Control.Monad.Eff (Eff) | |
import Control.Monad.Eff.Console (CONSOLE, log) | |
import Control.Monad.Eff.Exception (EXCEPTION, message) | |
import Node.Encoding (Encoding(..)) | |
import Node.FS (FS) | |
import Node.FS.Stream (createReadStream) | |
import Node.Stream (Readable, onDataString, onEnd, onError) | |
readStream :: forall eff | |
. Readable () (console :: CONSOLE, exception :: EXCEPTION | eff) | |
-> Eff (console :: CONSOLE, exception :: EXCEPTION | eff) Unit | |
readStream stream = do | |
onDataString stream UTF8 handleOutput | |
onEnd stream handleEnd | |
onError stream handleError | |
where | |
handleError err = log $ "error: " <> (message err) | |
handleOutput out = log $ "log: " <> out | |
handleEnd = log "Done!" | |
main :: forall eff. Eff (console :: CONSOLE, exception :: EXCEPTION, fs :: FS | eff) Unit | |
main = do | |
stream <- createReadStream "foo.txt" | |
readStream stream |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment