我正在尝试检测Type对象的特定实例是否是通用的"IEnumerable"...
我能想到的最好的是:
// theType might be typeof(IEnumerable<string>) for example... or it might not
bool isGenericEnumerable = theType.GetGenericTypeDefinition() == typeof(IEnumerable<object>).GetGenericTypeDefinition()
if(isGenericEnumerable)
{
Type enumType = theType.GetGenericArguments()[0];
etc. ...// enumType is now typeof(string)
Run Code Online (Sandbox Code Playgroud)
但这似乎有点间接 - 是否有更直接/更优雅的方式来做到这一点?
问题
什么是最有效的MGU算法?它的时间复杂度是多少?它是否足以简单地描述为堆栈溢出答案?
我一直试图在谷歌找到答案,但继续寻找我只能通过ACM订阅访问的私人.PDF.
我在SICP找到了一个讨论:这里
解释什么是"最通用的统一算法":取两个包含"自由变量"和"常量"的表达式树...例如
e1 = (+ x? (* y? 3) 5) e2 = (+ z? q? r?)
然后,Most General Unifier算法返回最通用的绑定集,使两个表达式等效.
即
mgu(e1,e2) = (x = z), q = (* y 3), y = unbound, r = 5
通过"最一般",您可以改为绑定(x = 1)和(z = 1),这也会使e1和e2等效,但它会更具体.
SICP文章似乎暗示它相当昂贵.
有关信息,我问的原因是因为我知道类型推断也涉及这种"统一"算法,我想了解它.
看来我可以使用单引号或双引号来转义命令行参数:
PS C:\> echo Hello World
Hello
World
PS C:\> echo 'Hello World'
Hello World
PS C:\> echo "Hello World"
Hello World
Run Code Online (Sandbox Code Playgroud)
但是仍然有一些我无法弄清楚的东西,当你希望从包含空格的目录中运行可执行文件时:
PS C:\> c:\program files\test.exe
The term 'c:\program' is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try again.
At line:1 char:11
+ c:\program <<<< files\test.exe
PS C:\> 'c:\program files\test.exe'
c:\program files\test.exe
PS C:\> "c:\program files\test.exe"
c:\program files\test.exe
PS C:\>
Run Code Online (Sandbox Code Playgroud)
如何让PowerShell运行上面的可执行文件?
在C#中实现一个基本的Scheme解释器时,我惊恐地发现了以下问题:
IEnumerator没有克隆方法!(或者更确切地说,IEnumerable不能为我提供"可克隆"枚举器).
我想要的是什么:
interface IEnumerator<T>
{
bool MoveNext();
T Current { get; }
void Reset();
// NEW!
IEnumerator<T> Clone();
}
Run Code Online (Sandbox Code Playgroud)
我无法想出IEnumerable的实现,它无法提供有效的可克隆IEnumerator(向量,链表等等)所有能够提供IEnumerator的克隆()的简单实现,如上所述...它会比提供Reset()方法更容易!).
缺少Clone方法意味着枚举序列的任何功能/递归习惯用法都不起作用.
这也意味着我无法"无缝地"使IEnumerable的行为像Lisp"列表"(为此你使用car/cdr递归枚举).即唯一的实现"(cdr some IEnumerable)"将是非常低效的.
任何人都可以建议一个现实的,有用的IEnumerable对象的例子,它无法提供有效的"Clone()"方法吗?是否存在"收益"构造的问题?
任何人都可以建议解决方法?
我试图在Scheme中找到多参数"compose"的"最佳"实现(我知道它在某些实现中是内置的,但我假设目前我正在使用没有这个的那个).
对于2参数的组合函数,我有这个:
(define compose
(lambda (f g)
(lambda x
(f (apply g x)))))
Run Code Online (Sandbox Code Playgroud)
这样做的好处是,如果最右边的函数需要额外的参数,那么它们仍然可以通过组合函数传递.这具有令人满意的特性,即在某事物之上组成身份功能不会改变功能.
例如:
(define identity
(lambda (x) x))
(define list1
(compose identity list))
(define list2
(compose identity list1))
(list2 1 2 3)
> (1 2 3)
Run Code Online (Sandbox Code Playgroud)
现在做一个"n-argument"compose我可以这样做:
(define compose-n
(lambda args
(foldr compose identity args)))
((compose-n car cdr cdr) '(1 2 3))
> 3
Run Code Online (Sandbox Code Playgroud)
但这不再保留那个漂亮的"身份"属性:
((compose-n identity list) 1 2 3)
> procedure identity: expects 1 argument, given 3: 1 2 3
Run Code Online (Sandbox Code Playgroud)
问题是用于foldr命令的"初始"函数.它已建成:
(compose identity (compose list identity)) …Run Code Online (Sandbox Code Playgroud) 请考虑以下代码:
NumberFormat format = NumberFormat.getInstance();
format.setMinimumFractionDigits(spotDecimalPlaces);
format.setMaximumFractionDigits(spotDecimalPlaces);
Run Code Online (Sandbox Code Playgroud)
它"安全"吗?是NumberFormat.getInstance()保证返回一个新的NumberFormat每次对象?
或者是否有可能getInstance()返回相同的实例?(在这种情况下,此代码会影响恰好使用的JVM中的其他位置getInstance...)
查看源代码,似乎每次都返回一个新实例.JavaDoc对此事感到沮丧.
如果上面的代码确实是"安全的",那么在我看来getInstance(),这个方法的名称很差 - 它应该被调用createInstance().
是否NumberFormat.getInstance()保证始终返回新实例?
我们有一些客户端代码,它使用.NET中的SqlConnection类与SQLServer数据库通信.它会间歇性地失败并出现此错误:
"ExecuteReader需要一个开放且可用的连接.连接的当前状态为Closed"
"临时"解决方案是重新启动过程,之后一切正常 - 但是,这显然不能令人满意.
代码保留了SqlConnection实例的缓存,每个数据库一个.
我们想重新编写代码,但在此之前,我需要了解一些事情:
我的第一个问题是:重复连接和断开SqlConnection对象是否效率低下,或者底层库是否代表我们执行连接池?
// Is this bad/inefficient?
for(many-times)
{
using(SQLConnection conn = new SQLConnection(connectionString))
{
// do stuff with conn
}
}
Run Code Online (Sandbox Code Playgroud)
因为我们的代码不能执行上述操作,所以问题的可能原因似乎是在连接的"生命周期"期间底层SQLServer数据库发生了某些事情,导致连接被关闭...
如果事实证明"缓存"SqlConnection对象是值得的,那么建议的方法是处理所有可以通过"重新连接"到数据库来解决的错误.我在谈论的场景如下:
我注意到SqlConnection上有一个"State"属性...是否有适当的方法来查询?
最后,我有一个测试SQLServer实例设置了完全访问权限:我怎样才能再现确切的错误"ExecuteReader需要一个开放的可用连接.连接的当前状态是关闭的"
我希望声明并初始化一维托管数组.
如果它是C#代码,我会这样写:
VdbMethodInfo[] methods = new VdbMethodInfo[] {
new VdbMethodInfo("Method1"),
new VdbMethodInfo("Method2")
};
Run Code Online (Sandbox Code Playgroud)
我想在托管C++中写(实际上,我正在编写一个程序生成)同样的东西...
到目前为止,我有:
typedef array<VdbMethodInfo^, 1> MethodArray;
// How do I avoid pre-declaring the size of the array up front?
MethodArray^ methods = gcnew MethodArray(2);
methods[0] = gcnew VdbMethodInfo("Method1");
methods[1] = gcnew VdbMethodInfo("Method2");
Run Code Online (Sandbox Code Playgroud)
这有两个问题:
Managed C++中的GC数组是否有"数组初始化"语法?什么是正确的语法?是否有关于此问题和其他类似问题的良好网络链接?
我在C#中编写了一个小的Scheme解释器,并意识到我实现它的方式,很容易添加对正确延续的支持.
所以我添加了它们......但是想要"证明"我们添加它们的方式是正确的.
然而,我的Scheme解释器不支持"变异"状态 - 一切都是不可改变的.
因此编写单元测试来暴露"向上"延续非常容易:
AssertEqual(Eval("(call/cc (lambda (k) (+ 56 (k 3))))"), 3);
Run Code Online (Sandbox Code Playgroud)
但是,我还想写一个单元测试,证明如果继续"逃避",那么它仍然有效:
AssertEqual(Eval("(call/cc (lambda (k) k))", <some continuation>);
Run Code Online (Sandbox Code Playgroud)
但是,当然,上面只会测试"我得到了延续"......而不是它实际上是一个有效的延续.
然而,我能找到的所有例子总是最终使用"set!".证明逃脱的延续.
什么是最简单的Scheme示例,它展示了在不依赖突变的情况下对向后延续的适当支持?
向后延续任何使用没有变异吗?我开始怀疑自己是不是,因为你只能用它来再次执行完全相同的计算......如果没有副作用,这是没有意义的.这就是Haskell没有延续的原因吗?
有没有办法确保在Hotspot崩溃时整个Java进程将退出?
出于进程隔离的目的,我们在远程Windows Java进程(java.exe)中托管本机库.但是,我们发现即使存在热点崩溃,虽然主线程因热点崩溃而"死亡",但进程本身并不会死亡.我们必须在任务管理器中杀死它.
我们认为这可能是因为本机库本身创建了自己的线程,这使得进程保持活跃状态.
如果存在热点崩溃,我们希望整个Java进程死掉.
我们目前的工作是使用另一个线程来读取生成进程的输出(我们必须先读取控制台输出以阻止进程阻塞).我们修改它以明确查看VM崩溃:
private static String HOTSPOT_CRASH = "# A fatal error has been detected by the Java Runtime Environment";
public void run() {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
do {
line = reader.readLine();
if (line != null) {
logger.info(prefix + ": " + line);
}
if(line.contains(HOTSPOT_CRASH)) {
logger.error(String.format("FATAL Hotspot crash detected. Killing child process '%s'...", hostProcess));
hostProcess.destroy();
}
} while (line != null);
reader.close();
} catch (IOException e) {
e.printStackTrace();
} …Run Code Online (Sandbox Code Playgroud) .net ×4
scheme ×3
c# ×2
java ×2
c++-cli ×1
generics ×1
haskell ×1
ienumerable ×1
logic ×1
managed-c++ ×1
powershell ×1
reflection ×1
sql-server ×1
unification ×1
windows ×1