Say
Send textual messages to a Handle in a thread-friendly way
Install / Use
/learn @fpco/SayREADME
say
Send textual messages to a Handle in a thread-friendly way.
The motivation for this package is described in a blog post on Haskell's
Missing Concurrency
Basics.
The simple explanation is, when writing a line of textual data to a Handle -
such as sending some messages t o ther terminal - we'd like to have the
following properties:
- Properly handle character encoding settings on the
Handle - For reasonably sized messages, ensure that the entire message is written in
one chunk to avoid interleaving data with other threads
- This includes the trailing newline character
- Avoid unnecessary memory allocations and copies
- Minimize locking
- Provide a simple API
On the last point: for the most part, you can make the following substitutions in your API usage:
- Replace
putStrLnwithsay - Replace
printwithsayShow - If you're using a
Stringinstead ofText, replaceputStrLnwithsayString
In addition, sayErr, sayErrString and sayErrShow work on
standard error instead, and hSay, hSayString and hSayShow work
on arbitrary Handles.
#!/usr/bin/env stack
-- stack --install-ghc --resolver lts-6.23 runghc --package async --package say
import Control.Concurrent.Async (mapConcurrently)
import Control.Monad (forM_, void)
import Say (sayString)
worker :: Int -> IO ()
worker ident = forM_ [1..1000] $ \msg -> sayString $ concat
[ "Hello, I am worker #"
, show ident
, ", and this is message #"
, show msg
]
main :: IO ()
main = void $ mapConcurrently worker [1..100]
Related Skills
node-connect
353.1kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
111.6kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
353.1kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
353.1kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
