我希望下一个会在一秒内打印"()"10次.但它在一秒钟之后就会挂起.为什么?
adaptE $ fmap print $ filterE (const True) $ atTimes [0.1, 0.2 ..]
Run Code Online (Sandbox Code Playgroud)
我发现它与filterE中使用的liftM有关:
filterE :: (Ord t, Bounded t) => (a -> Bool) -> EventG t a -> EventG t a
filterE p m = justE (liftM f m)
where
f a | p a = Just a
| otherwise = Nothing
Run Code Online (Sandbox Code Playgroud)
我试图重新实现filterE使用fmap它似乎工作.为什么?该标准filterE是如何设计使用的?
我发现自己重新实现很多通过所提供的标准功能reactive包(例如diffE,integrate).这是否意味着包装有错误或我以错误的方式使用它?
谢谢!
我对Haskell中不可变变量的概念很困惑.看来我们无法改变Haskell中变量的值.但是当我尝试在GHCI中使用代码时,似乎变量的值确实发生了变化:
Prelude> foo x=x+1
Prelude> a=1
Prelude> a
1
Prelude> foo a
2
Prelude> a=2
Prelude> a
2
Prelude> foo a
3
Run Code Online (Sandbox Code Playgroud)
这与不可变变量的想法有冲突吗?
非常感谢!
以下代码意外地(至少对我而言)产生了一个中间向量:
import qualified Data.Vector as Vector
main :: IO ()
main =
print (test n)
n :: Int
n = 1000000
test :: Int -> Int
test n = Vector.length (Vector.replicate n (0 :: Int))
Run Code Online (Sandbox Code Playgroud)
Core的相关部分在这里(注意newArray# 1000000电话):
Main.main4
:: forall s_a38t.
GHC.Prim.State# s_a38t
-> (# GHC.Prim.State# s_a38t, Vector.Vector Int #)
[GblId,
Arity=1,
Str=DmdType,
Unf=Unf{Src=<vanilla>, TopLvl=True, Value=True, ConLike=True,
WorkFree=True, Expandable=True, Guidance=IF_ARGS [0] 399 30}]
Main.main4 =
\ (@ s_a38t) (s1_a38u [OS=OneShot] :: GHC.Prim.State# s_a38t) ->
case GHC.Prim.newArray#
@ Int …Run Code Online (Sandbox Code Playgroud) 考虑这个例子:
import qualified Data.Vector.Unboxed as Vector
import Data.Vector.Unboxed (Vector)
import qualified Data.Vector.Unboxed.Mutable as MVector
process :: [Int] -> Vector Int -> Vector Int
process [] v = v
process (x:xs) v = process xs $ Vector.modify modify v
where
modify mv = do
old <- MVector.read mv x
MVector.write mv x (old + 1)
main :: IO ()
main = do
print $ process [1, 1, 3, 1] $ Vector.replicate 10 0
Run Code Online (Sandbox Code Playgroud)
在core每次迭代我看到的序列newByteArray#,copyByteArray#,readIntArray# …
我有一个简单的基于attoparsec的pdf解析器.它一直正常工作,直到与iteratee一起使用.当输入的大小超过缓冲区大小时.
import qualified Data.ByteString as BS
import qualified Data.Iteratee as I
import qualified Data.Attoparsec as P
import qualified Data.Attoparsec.Iteratee as P
import System.Environment (getArgs)
import Control.Monad
import Pdf.Parser.Value
main :: IO ()
main = do
[i] <- getArgs
liftM (P.parseOnly parseValue) (BS.readFile i) >>= print -- works
I.fileDriverRandomVBuf 2048 (P.parserToIteratee parseValue) i >>= print -- works
I.fileDriverRandomVBuf 1024 (P.parserToIteratee parseValue) i >>= print -- DOES NOT works!!!
Run Code Online (Sandbox Code Playgroud)
输入:
<< /Annots [ 404 0 R 547 0 R ] …Run Code Online (Sandbox Code Playgroud) 我有下一个monad变换器:
newtype Pdf' m a = Pdf' {
unPdf' :: StateT St (Iteratee ByteString m) a
}
type Pdf m = ErrorT String (Pdf' m)
Run Code Online (Sandbox Code Playgroud)
基本上,它使用底层 Iteratee来读取和处理pdf文档(需要随机访问源,因此它不会一直将文档保存在内存中).
我需要实现一个保存pdf文档的函数,我希望它是懒惰的,应该可以将文档保存在常量内存中.
我可以生产懒惰ByteString:
import Data.ByteString.Lazy (ByteString)
import qualified Data.ByteString.Lazy as BS
save :: Monad m => Pdf m ByteString
save = do
-- actually it is a loop
str1 <- serializeTheFirstObject
storeOffsetForTheFirstObject (BS.length str1)
str2 <- serializeTheSecondObject
storeOffsetForTheSecondObject (BS.length str2)
...
strn <- serializeTheNthObject
storeOffsetForTheNthObject (BS.length strn)
table <- dumpRefTable
return …Run Code Online (Sandbox Code Playgroud) 我在努力acid-state.该文件指出,Update st是的一个实例MonadState st.我尝试不同的东西,但我的编译器不希望看到的是:(我试过HelloWorld.hs从实例,但得到了同样的问题:
HelloWorld.hs:26:7:
No instance for (MonadState
HelloWorldState (Update HelloWorldState))
arising from a use of `put'
Possible fix:
add an instance declaration for
(MonadState HelloWorldState (Update HelloWorldState))
In the expression: put (HelloWorldState newValue)
In an equation for `writeState':
writeState newValue = put (HelloWorldState newValue)
HelloWorld.hs:29:43:
No instance for (MonadReader
HelloWorldState (Query HelloWorldState))
arising from a use of `ask'
Possible fix:
add an instance declaration for
(MonadReader HelloWorldState …Run Code Online (Sandbox Code Playgroud) 以下代码打印出类似的内容 °Ð½Ð´Ð¸Ñ-ÐÑпаниÑ
getDirectoryContents "path/to/directory/that/contains/files/with/nonASCII/names"
>>= mapM_ putStrLn
Run Code Online (Sandbox Code Playgroud)
看起来它是一个ghc bug,它已经在存储库中修复了.但是在每个人升级ghc之前该怎么办?
我最后一次遇到这样的问题(就在几年前,顺便说一下),我用utf8-string包来转换字符串,但我不记得我是怎么做到的,并且ghc unicode支持在去年明显改变了.
那么,获得完整unicode支持的目录内容的最佳(或至少是工作)方式是什么?
ghc版本7.0.4 locale en_US.UTF-8
我需要对内部的一些代码进行基准测试IO,并且标准支持非常好.但我想执行一些初始化步骤(每个基准测试都不同).天真的方法:
main = defaultMain
[ bench "the first" $ do
initTheFirst
theFirst
cleanUpTheFirst
, bench "the second" $ do
initTheSecond
theSecond
cleanUpTheSecond
]
Run Code Online (Sandbox Code Playgroud)
但它会为每个基准测试运行执行初始化和清理(默认情况下为100次),并包括最终结果的初始化时间.是否可以排除初始化时间?
ADDED:代码使用全局状态(实际上是mongodb),所以我不能同时准备两个初始状态.
该文件约状态SourceCompletionProvider:
You must implement this interface to provide proposals to SourceCompletion
Run Code Online (Sandbox Code Playgroud)
"实现此接口"在上下文中意味着什么?
我应该GObject使用这个功能创建新的吗?有什么例子吗?
当我有新的时候GObject,我该如何添加功能(如sourceCompletionProviderGetName)呢?
谢谢
我有下一个定义:
data Nat : Set where
zero : Nat
succ : Nat -> Nat
prev : Nat -> Nat
prev zero = zero
prev (succ n) = n
data _<=_ : Nat -> Nat -> Set where
z<=n : forall {n} -> zero <= n
s<=s : forall {n m} -> (n<=m : n <= m) -> (succ n) <= (succ m)
Run Code Online (Sandbox Code Playgroud)
很容易证明下一个引理:
lem-prev : {x y : Nat} -> x <= y -> (prev x) <= (prev y)
lem-prev …Run Code Online (Sandbox Code Playgroud) 我有以下定义:
{-# LANGUAGE ExistentialQuantification #-}
module Test
where
class Named a where
name :: a -> String
data Wrap = forall a . (Named a, Read a) => Wrap (a -> IO ())
Run Code Online (Sandbox Code Playgroud)
我想写一个Named实例Wrap.下一个不起作用:
instance Named Wrap where
name (Wrap named) =
let a = undefined
_ = named a
in name a
Run Code Online (Sandbox Code Playgroud)
错误:
Could not deduce (Named a0) arising from a use of ‘name’
from the context (Named a, Read a)
Run Code Online (Sandbox Code Playgroud)
但以下工作:
instance Named Wrap …Run Code Online (Sandbox Code Playgroud) haskell ×11
ghc ×2
vector ×2
acid-state ×1
agda ×1
attoparsec ×1
frp ×1
ghci ×1
gtk2hs ×1
immutability ×1
iterate ×1
lazy-io ×1
optimization ×1
typeclass ×1
unicode ×1