我正在使用Mioco。mio::net::tcp::TcpStream没有实现Clone,所以似乎不可能TcpStream跨多个线程/协程共享 a (或者如果可能,我不确定如何;我对 Rust 很陌生)。因此,我假设要同时读取/写入单个TcpStream,有必要使用单个协程来进行读取和写入。
为了避免在传入数据不频繁时无限期地阻塞读取,似乎有必要在从TcpStream. std::net::TcpStream有set_read_timeout实现这一目标,但我找不到等效的mio::net::tcp::TcpStream. 我该怎么办?或者有没有办法TcpStream在多个协程之间共享 mio ,避免超时的需要?
我阅读了如何在 Common Lisp 中创建二进制流(不是文件)?,它描述了如何创建二进制流而不是双向流。我尝试使用引用的库自己完成,但失败了。我的尝试看起来像:
(defun make-test-binary-stream ()
(make-two-way-stream (flexi-streams:make-in-memory-input-stream
(vector))
(flexi-streams:make-in-memory-output-stream)))
Run Code Online (Sandbox Code Playgroud)
我像这样使用它:
(let ((str (make-test-binary-stream)))
(lisp-binary:write-float :double 123.45d0 :stream str)
(lisp-binary:read-binary-type 'double-float str))
Run Code Online (Sandbox Code Playgroud)
我期望的结果是123.45d0,但它返回0.0d0。
我如何创建一个行为符合我预期的二进制流,允许我写入一个值然后读回相同的值?我想要这样一个流来测试将流作为输入和输出到流的编码和解码函数的正确性。
在Haskell中,种类(类型)允许一些有用的东西,比如类型构造函数.我的问题是,对于各种类型(类型的类型)是否会有任何好处,或者他们可以做什么,只有种类和类型不能轻易完成?
在Go中,使用迭代遍历字符串
for i := 0; i < len(myString); i++{
doSomething(myString[i])
}
Run Code Online (Sandbox Code Playgroud)
只访问字符串中的单个字节,而迭代遍历字符串
for i, c := range myString{
doSomething(c)
}
Run Code Online (Sandbox Code Playgroud)
迭代单个Unicode代码点(rune在Go中称为s),它可能跨越多个字节.
我的问题是:在迭代字符串时如何跳转range Mystring?continue可以通过一个unicode代码点向前跳,但是i += 3如果你想跳过三个代码点,就不可能做到这一点.那么,通过n个代码点推进前进的最惯用方法是什么?
我在golang nuts邮件列表上问了这个问题,并且得到了回答,礼貌列表中的一些有用的人.有人告诉我,但是建议我在Stack Overflow上为此创建一个自我回答的问题,为同一个问题保存下一个人有些麻烦.这就是这个.
我目前正在建设伊德里斯cabal install idris.响应输出:
Redirecting build log to {handle:/home/me/.cabal/logs/idris-0.9.14.3.log}
Run Code Online (Sandbox Code Playgroud)
我决定跟踪构建tail -f /home/me/.cabal/logs/idris-0.9.14.3.log.
虽然大部分输出都是有意义的,但是:
Simplifier:
Result size of Simplifier iteration=1
= {terms: 77,520, types: 104,583, coercions: 37,209}
Result size of Simplifier iteration=2
= {terms: 59,582, types: 78,325, coercions: 18,371}
Run Code Online (Sandbox Code Playgroud)
有很多行只包含
*** CPSZ:
所以我想知道,纯粹是出于好奇," *** CPSZ:"代表什么.
在Idris中,Vect n a是一种数据类型,表示包含类型a的项的n长度的向量.想象一下,我有一个功能:
foo : Int -> Vect 4 Int
foo n = [n-1, n, n+1, n*4]
Run Code Online (Sandbox Code Playgroud)
函数的主体并不重要,它可以是返回4 Ints向量的任何东西.现在,我想将此函数与concatMap一起使用,如下所示:
bar : Vect n Int -> Vect (4*n) Int
bar vals = concatMap foo vals
Run Code Online (Sandbox Code Playgroud)
Bar是一个函数,它采用长度为n的Int向量并返回长度为4*n的向量.
concatMap的类型签名是:
Prelude.Foldable.concatMap : Foldable t => Monoid m => (a -> m) -> t a -> m
Run Code Online (Sandbox Code Playgroud)
因此,如果我尝试编译bar,我会收到错误:
When elaborating right hand side of bar:
Can't resolve type class Monoid (Vect (plus n (plus n (plus n (plus n 0)))) Int)
Run Code Online (Sandbox Code Playgroud)
这意味着Vect n …
在 Coq 标准库中,“小于”关系对于自然数 ( lt_dec) 和整数 ( Z_lt_dec) 是可判定的。然而,当我搜索 ( Search ({ _ _ _ } + {~ _ _ _ })) 时,我找不到 aQ_le_dec或Qle_dec的有理数,只能Qeq_dec找到可判定的相等性。
这是因为“小于”关系对于 Coq 中的有理数来说是不可判定的吗?或者它是可以决定的,但决策过程没有在标准库中实现?
如果我在编译时知道两个常量,Java编译器将折叠它们.
final static int foo = 2;
final static int bar = 17;
int myVariable;
int myFunction(){
return foo*bar + myVariable;
}
Run Code Online (Sandbox Code Playgroud)
在运行时,myFunction将返回34 + myVariable,并且不需要计算2*17,因为它是在编译时完成的.
我的问题是:如果直到运行时才知道常量,它会不会这样做?我相信这称为运行时代码专业化.
final int foo;
final int bar;
int myVariable;
int myFunction(){
return foo*bar + myVariable;
}
Run Code Online (Sandbox Code Playgroud)
如果foo和bar在对象的构造函数中初始化为2和17,myFunction是否会专门返回34 + myVariable,或者每次调用函数时它仍会计算foo*bar,即使foo*bar永远不会改变?
*编辑:我指的是最新版本的JVM,1.7.0_45.
我在Clojure中使用deftype时遇到了问题.如果我运行以下代码:
(defprotocol TestProt
(geta [this])
(getb [this]))
(deftype TestType [a b]
TestProt
(geta [this] a)
(getb [this] b))
(defn test-function [^TestType a-testtype]
(print (.geta a-testtype)))
(def test-tt (TestType. 1 1))
(test-function test-tt)
Run Code Online (Sandbox Code Playgroud)
然后编译器抛出:ClassCastException MyProject.core.TestType无法强制转换为MyProject.core.TestType.我做错了什么,或者这是一个错误?请注意,如果我从test-function中删除类型注释,那么它只是:
(defn test-function [a-testtype]
(print (.geta a-testtype)))
Run Code Online (Sandbox Code Playgroud)
然后代码工作正常,但我得到一个关于反射的警告(启用了反射警告),并且它运行得更慢,这违背了在我当前用例中使用deftype的目的.
编辑:好的,代码在repl中工作,但是当我使用ctrl-alt-s加载它时(我在Eclipse中通过逆时针运行它).所以问题似乎是Eclipse或逆时针.
首先,我并不完全熟悉有问题的概念,如果我滥用任何术语,请原谅我.我想知道的是,如果我有类似的东西:
int someGlobal = 7;
int sumThree(int a, int b){
return (a + b + someGlobal);
}
Run Code Online (Sandbox Code Playgroud)
如果可以通过调用部分应用/ curry该函数sumThree(3),那么这将被转换为等效于:
int sumThree(int b){
return (3 + b + someGlobal);
}
Run Code Online (Sandbox Code Playgroud)
或者:
int sumThree(int b){
return (3 + b + 7); //or, return (10+b)
}
Run Code Online (Sandbox Code Playgroud)
其中一个会"更正确"吗?或者,如果它们只是同一种形式的不同形式,那么用什么术语来区分它们中的两种?上面两段代码的不同之处在于,如果someGlobal的值在sumThree(3)被调用之后但在应用函数的其余部分之前发生了变化,则前者会对后者产生不同的结果,因为后者捕获了值(7)当时有些全球人sumThree(3)称之为.
为了好玩,我试图编写一个天真最长路径算法的实现(用于在循环图中查找最长非循环路径的长度).我开始使用命令式算法的直接端口,该算法运行良好并且表现相当好.
data Route = Route {dest:: !Int32, cost:: !Int32}
type Node = [Route]
lPathImperative :: V.Vector Node -> Int32 -> UMV.IOVector Bool -> IO (Int32)
lPathImperative !nodes !nodeID !visited = do
UMV.write visited (fromIntegral nodeID) True
max <- newIORef 0
Prelude.mapM_ (\ Route{dest, cost} -> do
isVisited <- UMV.read visited (fromIntegral dest)
case isVisited of
True -> return ()
False -> do
dist <- fmap (+ cost) $ lPathImperative nodes dest visited
maxVal <- readIORef max
if dist > maxVal …Run Code Online (Sandbox Code Playgroud) 我正在运行MinGW G ++ 4.8.1,当我更改以下代码时:
for (Room &r : TempRooms2) {
r.vNeighbours.clear();
}
Run Code Online (Sandbox Code Playgroud)
至
for (Room &r : TempRooms2) {
r.vNeighbours.clear();
r.Layer=-1;
}
Run Code Online (Sandbox Code Playgroud)
该程序之前完美运行,在libwinpthread-1.dll中发生了段错误.现在,我之前没有使用过这个G ++ 4.8.1版本的线程功能,经过测试后发现它们在我的操作系统上不稳定,所以我将来需要更改为不同的版本.我想利用线程.在那之前,有没有办法告诉G ++停止尝试并行循环?
*编辑:对不起,事实证明崩溃是由于代码进入其他地方的无限循环.我只是假设它是一个线程化的东西,因为GDP说'pthread_tls_init'的调用是segfaulting,我认为只有一个线程程序会使用它.如果有人能够解释为什么Windows因为无限循环而导致程序出现在调试器中作为pthread中的段错误,我将不胜感激.*
我开始学习Coq,并试图证明一些看似相当简单的东西:如果列表包含x,那么该列表中x的实例数将> 0.
我已经定义了contains和count函数,如下所示:
Fixpoint contains (n: nat) (l: list nat) : Prop :=
match l with
| nil => False
| h :: t => if beq_nat h n then True else contains n t
end.
Fixpoint count (n acc: nat) (l: list nat) : nat :=
match l with
| nil => acc
| h :: t => if beq_nat h n then count n (acc + 1) t else count n acc t
end.
Run Code Online (Sandbox Code Playgroud)
我试图证明:
Lemma contains_count_ge1 : …Run Code Online (Sandbox Code Playgroud)