Skip to content

Instantly share code, notes, and snippets.

@exarkun
Last active December 26, 2020 14:51
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 exarkun/0d30f7161552449bf33294fa06f5ad57 to your computer and use it in GitHub Desktop.
Save exarkun/0d30f7161552449bf33294fa06f5ad57 to your computer and use it in GitHub Desktop.
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