我有一个无窗口的计时器(没有WM_TIMER),它只在一个给定的时间段过去后才会触发一次回调函数.它被实现为SetTimer()/KillTimer().时间段足够小:100-300毫秒.
是否足够便宜(我的意思是性能)在SetTimer()/KillTimer()每个如此短的时间间隔内调用对?
如果我有100个定期打电话的定时器SetTimer()/KillTimer()怎么办?系统中可能同时存在多少Window定时器对象?
这是一个问题:使用一堆这样的计时器对象并依赖于计时器的良好Windows实现,或者创建一个Windows计时器对象,每个计时器对应一个,例如30毫秒,并订阅所有自定义100-300毫秒的一次性计时器.
谢谢
阅读关于函数和委托文字的TDPL(5.6.1)
auto f = (int i) {};
assert(is(f == function));
Run Code Online (Sandbox Code Playgroud)
我有一个断言失败.这个断言是否正确?
我尝试了以下方法:
int z = 5;
auto f = (int i) { return i < 5; };
auto d = (int i) { return i < z; };
assert(is(typeof(f) == typeof(d)));
Run Code Online (Sandbox Code Playgroud)
断言在那里是有效的.实际上f是委托,即使它不需要帧指针来访问局部变量也不是函数.这是一个错误吗?
另外,我不明白assert(is(f == function));应该怎么做.
我试过assert(is(f == delegate));但也失败了.怎么了?
我使用DMD32 D Compiler v2.053
UPDATE
auto f = (int i) {};
assert(is(typeof(f) == delegate))
Run Code Online (Sandbox Code Playgroud)
工作正确,虽然没有理由成为代表
但
auto f = function (int i) {};
assert(is(typeof(f) == void function(int))); // …Run Code Online (Sandbox Code Playgroud) Jane Street的Core lib具有以下功能:Fn.const.
https://github.com/janestreet/core_kernel/blob/master/lib/fn.ml
let const c = (); fun _ -> c
Run Code Online (Sandbox Code Playgroud)
val const:'a - >'b - >'a
产生一个只返回其第一个参数的函数
我真的不明白.
();第一?let const c = fun () -> c?这将给出一个函数unit作为参数并始终返回初始值c.let f = const 5,f将成为一个以'_a参数为参数的函数.返回具有弱多态参数的函数的目的是什么?ps我看到Fn模块里面的几个函数都有();返回函数之前的用法是();什么?
(交叉发布到lwt github问题)
我已经将我的用法归结为此代码示例,这将泄漏文件描述符.
说你有:
#require "lwt.unix"
open Lwt.Infix
let echo ic oc = Lwt_io.(write_chars oc (read_chars ic))
let program =
let server_address = Unix.(ADDR_INET (inet_addr_loopback, 2000)) in
let other_addr = Unix.(ADDR_INET (inet_addr_loopback, 2001)) in
let server = Lwt_io.establish_server server_address begin fun (tcp_ic, tcp_oc) ->
Lwt_io.with_connection other_addr begin fun (nc_ic, nc_oc) ->
Lwt_io.printl "Created connection" >>= fun () ->
echo tcp_ic nc_oc <&> echo nc_ic tcp_oc >>= fun () ->
Lwt_io.printl "finished"
end
|> Lwt.ignore_result
end
in
fst (Lwt.wait ())
let …Run Code Online (Sandbox Code Playgroud) TDPL描述了一种assert(false);陈述行为.这样的断言不会从发布版本中删除(与所有其他断言一样),并且实际上会立即停止程序.问题是为什么?为什么这么混乱的行为呢?他们可能会添加halt();或类似的东西,以便能够停止该程序.
有时,我在C++代码中使用以下构造:
if (something_goes_wrong)
{
assert(false);
return false;
}
Run Code Online (Sandbox Code Playgroud)
显然,这种结构在D中是不可能的.
UPDATE
要清楚.问题是为什么int x=0; assert(x);不会崩溃程序的发布版本,但assert(0);会?为何如此奇怪的语言设计决定?
这是我fold为树实现(左)的尝试(它是非常简化的版本,但仔细地再现了真正的树结构):
type 'a tree = Leaf of 'a | Node of 'a * 'a tree list
let rec fold t acc f =
match t with
| Leaf x -> f acc x None
| Node (x, lst) ->
let deferred acc =
List.fold_left (fun acc t' -> fold t' acc f) acc lst in
f acc x (Some deferred)
Run Code Online (Sandbox Code Playgroud)
我们的想法是使用延迟调用子树.它让我们:
玩具示例:
open Printf
let () =
let tree = Node (3, [Leaf 5; Leaf 3; …Run Code Online (Sandbox Code Playgroud) 有时,我们对捕获对象状态的lambda的生命周期一无所知(例如,将其从对象返回,将其注册为回调而无需取消订阅等).如何确保lambda在调用时不会访问已经销毁的对象?
#include <iostream>
#include <memory>
#include <string>
class Foo {
public:
Foo(const std::string& i_name) : name(i_name) {}
std::function<void()> GetPrinter() {
return [this]() {
std::cout << name << std::endl;
};
}
std::string name;
};
int main() {
std::function<void()> f;
{
auto foo = std::make_shared<Foo>("OK");
f = foo->GetPrinter();
}
auto foo = std::make_shared<Foo>("WRONG");
f();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这个程序只是巧合地打印出"错误"而不是"OK"(http://ideone.com/Srp7RC)(似乎它只是为第二个Foo对象重用了相同的内存).无论如何,这是一个错误的计划.Foo我们执行时,第一个对象已经死了f.
有我的玩具GADT表达:
type _ expr =
| Num : int -> int expr
| Add : int expr * int expr -> int expr
| Sub : int expr * int expr -> int expr
| Mul : int expr * int expr -> int expr
| Div : int expr * int expr -> int expr
| Lt : int expr * int expr -> bool expr
| Gt : int expr * int expr -> bool expr
| And …Run Code Online (Sandbox Code Playgroud) 我正在尝试从CSV文件中读取时间序列并将它们保存为xts,以便能够使用quantmod处理它们.问题是不解析数值.
CSV文件:
name;amount;datetime
test1;3;2010-09-23 19:00:00.057
test2;9;2010-09-23 19:00:00.073
Run Code Online (Sandbox Code Playgroud)
R代码:
library(xts)
ColClasses = c("character", "numeric", "character")
Data <- read.zoo("c:\\dat\\test2.csv", index.column = 3, sep = ";", header = TRUE, FUN = as.POSIXct, colClasses = ColClasses)
as.xts(Data)
Run Code Online (Sandbox Code Playgroud)
结果:
name amount
2010-09-23 19:00:00 "test1" "3"
2010-09-23 19:00:00 "test2" "9"
Run Code Online (Sandbox Code Playgroud)
请参阅amount列包含字符数据,但预计为数字.我的代码出了什么问题?
码
struct test
{
private real value;
this(real value)
{
this.value = value;
}
bool opUnary(string op)() if (op == "!")
{
return !value;
}
}
void main()
{
test a = 123.12345;
bool b = !a;
}
Run Code Online (Sandbox Code Playgroud)
编译错误
prog.d(19): Error: expression a of type test does not have a boolean value
Run Code Online (Sandbox Code Playgroud)
也在dmd 2.053,2.054上测试
我的代码出了什么问题?