在 snap 中使用 reader monad(或者在 snap 中使用 monad 转换器)

发布于 2024-10-16 06:50:23 字数 224 浏览 1 评论 0原文

有人可以展示如何在 reader monad 中使用 snap monad 吗? Monad 转换器让我很困惑。 (或者,我很乐意接受关于 monad 转换器的教程建议,以及如何看到光明并最终理解它们。)

编辑:哎呀;忘记具体说明我实际上想要做什么,而不是寻求特定事情的帮助。战略,而不是战术。我特别想在所有处理程序之间共享数据库连接/池,而不必在指定路由时显式传递该数据库连接/池。读者单子似乎是实现这一目标的方法。

Can someone show how to use the snap monad inside the reader monad? Monad transformers confuse me. (Alternatively, I will gladly accept suggestions of tutorials about monad transformers, and ways to see the light and finally grok them.)

Edit: Oops; forgot to specify what I'm actually trying to do, instead of asking for help with a specific thing. Strategy, not tactics. I specifically want to share a database connection/pool amongst all handlers, without having to explicitly pass that database connection/pool when specifying routes. It seems that the reader monad would be the way to accomplish that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

软糯酥胸 2024-10-23 06:50:23

Snap 具有 ApplicationState 类型,允许您打包所需的任何应用程序范围资源(数据库连接、模板引擎等)。

它位于生成的文件 Application.hs 中,默认情况下,HeistState 和 TimerState 包含在 ApplicationState 中。您只需将数据库连接放在那里,就可以在 Snap 应用程序中的任何位置使用它。

Snap has ApplicationState type that allows you package whatever application wide resources you need (db connections, template engines etc.)

It is located in generated file Application.hs and by default has HeistState and TimerState included into ApplicationState. You can just put your db connection there and it will be available from anywhere in your Snap application.

一曲琵琶半遮面シ 2024-10-23 06:50:23

如果您不害怕使用 GHC 特定的扩展,这里有一个简单的 monad 转换器方法:

{-# LANGUAGE GeneralizedNewtypeDeriving #-}

import Control.Monad.Reader

data ReaderData = ...

newtype MyMonad a = MyMonad (ReaderT ReaderData Snap a)
  deriving (Monad, MonadReader ReaderData)

runMyMonad :: MyMonad a -> ReaderData -> Snap a
runMyMonad (MyMonad m) r = runReaderT m r

liftSnap :: Snap a -> MyMonad a
liftSnap act = MyMonad (lift act)

您现在可以使用 asklocal 来访问读取器数据。要在 Snap monad 中运行操作,您需要将其“提升”到新的 monad 中。

... r <- liftSnap $ ... snap action ...

不过,您可能更喜欢较短的名称。所以,也许只是snap

If you're not afraid to use GHC-specific extensions, here's the no-frills approach to monad transformers:

{-# LANGUAGE GeneralizedNewtypeDeriving #-}

import Control.Monad.Reader

data ReaderData = ...

newtype MyMonad a = MyMonad (ReaderT ReaderData Snap a)
  deriving (Monad, MonadReader ReaderData)

runMyMonad :: MyMonad a -> ReaderData -> Snap a
runMyMonad (MyMonad m) r = runReaderT m r

liftSnap :: Snap a -> MyMonad a
liftSnap act = MyMonad (lift act)

You can now use ask and local to access the reader data. To run an action in the Snap monad, you need to "lift" it into your new monad.

... r <- liftSnap $ ... snap action ...

You may prefer a shorter name, though. So, maybe just snap.

哭了丶谁疼 2024-10-23 06:50:23

假设 snap monad 来自 http ://hackage.haskell.org/packages/archive/snap-core/0.4.0/doc/html/Snap-Types.html... Snap 是一个 monad (不是 monad 转换器),所以你无法在任意 monad 内运行它。如果您需要的话,您可以使用 ReaderT 转换器将 Reader 功能嵌入到 Snap 中。

runSnap 的类型

runSnap :: Snap a -> (ByteString -> IO ()) -> (Int -> IO ()) -> Request -> Iteratee ByteString IO (Request, Response)

告诉我们它在 Iteratee ByteString IO monad 中运行。 Reader monad 不允许您执行 IO 或迭代输入流,因此您无法在 Reader monad 中运行 Snap 计算。

如果您解释您想要实现的目标,有人可能会提出实现它的方法。

Assuming the snap monad is from http://hackage.haskell.org/packages/archive/snap-core/0.4.0/doc/html/Snap-Types.html... Snap is a monad (not a monad transformer), so you cannot run it inside an arbitrary monad. You could use the ReaderT transformer to embed Reader functionality inside Snap, if that's what you want.

The type of runSnap is

runSnap :: Snap a -> (ByteString -> IO ()) -> (Int -> IO ()) -> Request -> Iteratee ByteString IO (Request, Response)

which tells us that it runs in an Iteratee ByteString IO monad. A Reader monad does not let you do IO or iterate over an input stream, so you cannot run a Snap computation in a Reader monad.

If you explain what you want to accomplish, someone may be able to suggest a way to achieve it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文