- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试制作一个愚蠢的网络服务器,将数据存储为 State
.我正在使用 Web.Scotty
. I've used ReaderT before with scotty to access config ,但遵循相同的方法在这里不起作用。它重置每个请求的状态。
我想在程序启动时设置初始状态,然后在程序的整个生命周期中保持相同的状态。
我怎样才能使这项工作? (以下每个请求都会创建一个新状态)
{-# LANGUAGE OverloadedStrings #-}
import Web.Scotty.Trans
import Control.Monad.State (StateT, evalStateT, lift)
import qualified Control.Monad.State as S
import Data.Text.Lazy (Text)
main :: IO ()
main = do
let runner = flip evalStateT "message"
scottyT 3000 runner runner routes
routes :: ScottyT Text (StateT Text IO) ()
routes = do
get "/data" $ do
val <- lift S.get
text val
put "/data/:val" $ do
val <- param "val"
lift $ S.put val
text val
最佳答案
您看到的行为绝对是预期的行为:
注意 documentation for scottyT
中第三个参数的注释:
-> (m Response -> IO Response)
-- Run monadm
intoIO
, called at each action.
StateT
之外。 monad,以便您可以在每个操作的处理程序中恢复它。我能想到的最天真的方法是这样的:
main :: IO ()
main = do
let s0 = "message"
let transform = flip evalStateT s0
runner <- restartableStateT s0
scottyT 3000 transform runner routes
restartableStateT :: s -> IO (StateT s IO a -> IO a)
restartableStateT s0 = do
r <- newIORef s0
return $ \act -> do
s <- readIORef r
(x, s') <- runStateT act s
atomicModifyIORef' r $ const (s', x)
关于haskell - 在 Web.Scotty 中使用 StateT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27495381/
Monad 转换器很棘手,我不确定(=没有良好的直觉)哪一个应该放在最上面。 最佳答案 StateT s (ExceptT e m) 这说: 以m开头 添加异常(exception)情况 添加状态 现
我无法找出缩放效果(例如 StateT)的最简洁方法,该效果将值返回到矢量或 map 等索引容器中。 例如,假设我有一些纸牌游戏的结构: data Card = Card { cardValu
我正在编写一个作为守护进程运行的程序。 要创建守护进程,用户需要提供一组 每个所需类的实现(其中一个是数据库) 所有这些类都有功能StateT s IO a 形式的类型签名, 但是 s每个类(clas
为了熟悉 monad 转换器,我编写了以下代码: data GlobalState = GlobalState { rng :: StdGen } foo :: IO () foo =
我在玩Cont描述的单子(monad)技巧here在 this SO question . 这个函数让你“跳回”到计算的早期,接受一个参数,这样你就可以做不同的事情: import Control.M
不同的 IDE 都有其怪癖,因此能够知道您正在使用什么 IDE 来运行 R 有时会很有用。 您可以通过测试 RSTUDIO 来测试您是否正在运行 RStudio。环境变量。 is_rstudio <-
我想弄清楚如何实现 MonadBaseControl 的实例对于 Foo 类型,它是 StateT 实例的新型包装器。你会认为它会像 this 一样实现但情况似乎并非如此。我假设状态块是导致这里问题的
我不明白为什么这段代码只循环一次然后退出? 在 Ghci 中,我只能回答第一个循环,然后似乎变量 cont 设置为 false 并且我没有提示回答。 结果是: *Main> testLoop1 td1
我们如何让用户传递一个 eventHandler,它使用 stateMonad 但在单独的线程中调用?比如下面的例子中,应该如何调用forkIO,以便eventHandler可以调用操作呢?我是 Ha
我确定我一定遗漏了什么。 我是 Haskell 的新手,曲线非常陡峭。在我的玩具项目中,我真的很想使用 State monad 来避免在任何地方传递一千个参数。我无法理解如何将 State monad
我在一个 Haskell 应用程序中需要跟踪两个或多个独立状态。 我使用 声明两个新类型类 type MonadTuple m = MonadState (Int, Int) m type Monad
我正在尝试在 "scalaz-core"%"7.2.14" StateT monad 的组合中创建某种故障转移行为。 StateT monad 包裹 EitherT,因此它是一个 monad 转换器:
这个问题有人问过before ,但没有真正的答案。事实上,公认的答案表明这是不可能的,尽管 StateT 是一个 Monad,因此是 Applicative 的超集。因此,标准库只使用 () = ap
我正在学习 Monad Transformers,其中一个练习要求为 StateT 实现 Monad 实例。我想使用 validity 测试我的实现是否符合 Monad 法则包,类似于 checker
我正在尝试制作一个愚蠢的网络服务器,将数据存储为 State .我正在使用 Web.Scotty . I've used ReaderT before with scotty to access co
我正在尝试使用管道和构建在它上面的各种库来编写一个基本的网络服务器。预期的流程是: 从套接字获取字节串 -> 使用二进制解码 -> 服务器逻辑在这里 -> 向套接字发送响应 我认为会是这样的: fro
具体来说,假设我有这个 monadT 堆栈: type MHeap e ret = MaybeT ( StateT [e] Identity ) ret 和一个方便的 runMheap 函数: run
通过保持 IORef 来通过异常来维护状态似乎要容易得多。而不是尝试使用 State Monad。下面我们有 2 个替代的 State Monad。一用StateT和另一个 ReaderT IORef
可以说我有一个功能 f :: State [Int] Int 和一个功能: g :: StateT [Int] IO Int 我想使用 f在 g并传递它们之间的状态。是否有库函数StateT (ret
有人知道是否有任何(简单的)方法可以让 StatEt 使用 R 2.15 吗?当前 StatEt 版本卡住为 R 2.14 版本。 最佳答案 我使用 StatET 3.0 的测试版本和 RJ 1.1,
我是一名优秀的程序员,十分优秀!