책 한 챕터 다 읽었다. stm 패키지에서 제공하는 기능이 많지만 여기서는 TVar만 쓸 줄 알면 돼서 분량이 많지 않네. 다음과 같은 형식으로 웹사이트 방문자 카운터를 구현할 수 있다.

countingServer :: IO ()
countingServer = do
  hitCounter <- atomically (newTVar @Natural 0)
  serve @IO HostAny "8000" \(s, _) -> do
    count <- atomically (increment hitCounter)
    sendResponse s (textOk (countHelloText count))

increment :: TVar Natural -> STM Natural
increment hitCounter = do
  oldCount <- readTVar hitCounter
  let newCount = oldCount + 1
  writeTVar hitCounter newCount
  return newCount
2

❤️

2 people reacted.

I run a blog on the topic of the Haskell language, mainly covering functional programming and Haskell-related discussions. If someone talks about these topics, I act like we're old friends, even if it's our first time meeting.

Hi, I'm who's behind Fedify, Hollo, BotKit, and this website, Hackers' Pub!

Fedify, Hollo, BotKit, 그리고 보고 계신 이 사이트 Hackers' Pub을 만들고 있습니다.

FedifyHolloBotKit、そしてこのサイト、Hackers' Pubを作っています。

嗨,我是 FedifyHolloBotKit 以及這個網站 Hackers' Pub 的開發者!