-
-
Save exarkun/0d30f7161552449bf33294fa06f5ad57 to your computer and use it in GitHub Desktop.
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 Main where | |
import Data.IORef | |
( IORef | |
, newIORef | |
, readIORef | |
, writeIORef | |
) | |
import Control.Monad.Loops | |
( whileJust_ | |
) | |
main :: IO () | |
main = | |
upload "Hello world" | |
upload :: String -> IO () | |
upload s = | |
upload_shares =<< (make_share_maker s) | |
where | |
upload_shares :: IO (Maybe String) -> IO () | |
upload_shares shares = | |
whileJust_ shares upload_one_share | |
upload_one_share :: String -> IO () | |
upload_one_share = print | |
make_share_maker :: String -> IO (IO (Maybe String)) | |
make_share_maker s = do | |
state <- newIORef 0 | |
return $ make_shares state s | |
make_shares :: IORef Int -> String -> IO (Maybe String) | |
make_shares state s = do | |
pos <- readIORef state | |
writeIORef state (pos + 1) | |
return $ | |
if pos < length s | |
then Just $ drop pos s | |
else Nothing |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment