Yesod处理程序,发布文件的内容 [英] Yesod handlers, content of POSTed files
问题描述
postImportR = do
fi< - lookupFiles文件
fc< - lift $ fileSource(fi !! 0)$$ consume
似乎可行至少我可以liftIO $ print fc),将它分解为一个迭代函数不会:
process :: [FileInfo] - > [String]
process [] = []
process(f:r)= do
fn < - fileName f
fc < - lift $ fileSource f $$消耗
([fn]:(process r))
postImportR = do
fi< - lookupFilesfile
process fi
甚至可以使用lambda函数:
<$ p (文件名f,提取$ fileSource f $$消耗))fi
在Handler中它给我一个类型错误,我不明白。
我的错在哪里 - - 喜欢从文件的行(和l生成数据库导入内容赚取更多的Haskell,当然)。
您有
fileName :: FileInfo - >文本
所以你不能直接使用 fileName
在一个do-block中,比如
fn < - fileName f
pre>
这需要是一个绑定的绑定
let fn = fileName f
接下来就是
process :: [FileInfo ] - > [String] 不可能(1), fileSource :: FileInfo - >源(ResourceT IO)ByteString
所以带有
fc< - lift $ fileSource f $$ consume
你在一个do块中,它的
Monad
是MonadIO
,你不能从Monad
,它可以包装任意IO
--actions,就像你不能脱离IO 本身。
您可以拥有的是
process ::(SomeFiendishConstraint m)=> [FileInfo] - > m [Text]
process [] = return []
process(f:r)= do
let fn = fileName f
lift $ fileSource f $$ consume
fs< - 进程r
返回(fn:fs)
或者更简洁,
process = mapM(\f - > lift $ fileSource f $$ consume>> return fileName f)
然后
postImportR = do
fi< - lookupFilesfile
process fi
(1)限制
unsafePerformIO
。while the following code:
postImportR = do fi <- lookupFiles "file" fc <- lift $ fileSource (fi !! 0) $$ consume
seems to work (at least can I "liftIO $ print fc), splitting it off to a function for iterating doesn't:
process :: [FileInfo] -> [String] process [] = [] process (f:r) = do fn <- fileName f fc <- lift $ fileSource f $$ consume ([fn] : (process r)) postImportR = do fi <- lookupFiles "file" process fi
or even with a lambda function:
files <- L.map (\f -> (fileName f, lift $ fileSource f $$ consume)) fi
in the Handler it gives me a type error I don't understand.
Where's my fault -- liked to generate content for database import from the file's lines (and to learn some more Haskell, of course).
解决方案You have
fileName :: FileInfo -> Text
so you can't directly use
fileName
in a do-block likefn <- fileName f
That would need to be a let-binding
let fn = fileName f
The next thing is what makes
process :: [FileInfo] -> [String]
impossible(1),fileSource :: FileInfo -> Source (ResourceT IO) ByteString
so with
fc <- lift $ fileSource f $$ consume
you are in a do-block whose
Monad
is aMonadIO
, and you can't get out of aMonad
that can wrap arbitraryIO
-actions, just like you can't get out ofIO
itself.What you can have is
process :: (SomeFiendishConstraint m) => [FileInfo] -> m [Text] process [] = return [] process (f:r) = do let fn = fileName f lift $ fileSource f $$ consume fs <- process r return (fn : fs)
or, more succinctly,
process = mapM (\f -> lift $ fileSource f $$ consume >> return fileName f)
and then
postImportR = do fi <- lookupFiles "file" process fi
(1) Barring
unsafePerformIO
.这篇关于Yesod处理程序,发布文件的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!