当我遇到一个我不理解的类型推断错误时,我正在玩一个爱好项目.我把它简化为以下简单的例子.
我有以下类和函数:
class Foo { }
class Bar { }
class Baz { }
static T2 F<T1, T2>(Func<T1, T2> f) { return default(T2); }
static T3 G<T1, T2, T3>(Func<T1, Func<T2, T3>> f) { return default(T3); }
Run Code Online (Sandbox Code Playgroud)
现在考虑以下示例:
// 1. F with explicit type arguments - Fine
F<Foo, Bar>(x => new Bar());
// 2. F with implicit type arguments - Also fine, compiler infers <Foo, Bar>
F((Foo x) => new Bar());
// 3. G with explicit type arguments - Still fine... …
Run Code Online (Sandbox Code Playgroud) 在cherryPy
例如,有文件如:
__init__.py
_cptools.py
他们有什么不同?这是什么意思?
我有一组元素,要求它们的最小宽度等于它们的高度,但未明确设置高度.目前我可以min-width
通过jQuery 设置css 属性来实现这一点:
$(document).ready
(
function()
{
$('.myClass').each
(
function() {$(this).css('min-width', $(this).css('height'));}
);
}
);
Run Code Online (Sandbox Code Playgroud)
是否可以直接在css中指定此行为?
这是一个展示我想要实现的目标:http://jsfiddle.net/p8PeW/
在C#中,您可以重载运算符,例如+
和*
.在他们的数学解释中,这些算子具有明确的优先顺序.
重载时是否保留此顺序,是否以某种确定性方式更改,或者您是否也可以重载优先顺序?
当客户端请求页面时,我们的安全管理器会在每个html页面的顶部动态插入一些javascript.它被插入DOCTYPE语句之上.我认为这可能是我遇到布局问题的原因.
想法有人吗?
任何人都可以解释为什么以下片段返回true?
根据"d"自定义格式说明符的文档,"一位数字日的格式没有前导零." 那么为什么当我给一个前导零的一位数日时,TryParseExact不会失败?
DateTime x;
return DateTime.TryParseExact
(
"01/01/2001",
@"d\/MM\/yyyy",
null,
System.Globalization.DateTimeStyles.None,
out x
);
Run Code Online (Sandbox Code Playgroud)
UPDATE
我想也许我原本不清楚.我真正想要的是:为什么TryParseExact接受一些不完全匹配的值?从我看到的所有文档中,'d'匹配'01'和'1'就像'MM'匹配'March'和'03'一样.这里的问题不是值是等价的,它们与格式不匹配.
相关的文档片段是:
来自TryParseExact:字符串表示的格式必须与指定的格式完全匹配.
来自'd'说明符:一位数字日的格式没有前导零.
我似乎非常清楚'01'有一个前导0,因此不完全匹配'd'.
假设我List<Foo> foos
的元素的当前顺序很重要.如果我然后应用LINQ Enumerable方法GroupBy
,Where
或者Select
,我可以依赖结果IEnumerable<Foo>
以与原始列表相同的相对顺序进行迭代吗?
我有一个简单的Eratosthanes Sieve实现如下:
# Generate all primes less than k
def sieve(k):
s = [True] * k
s[0] = s[1] = False
for i in range(4, k, 2):
s[i] = False
for i in range(3, int(sqrt(k)) + 2, 2):
if s[i]:
for j in range(i ** 2, k, i * 2):
s[j] = False
return [2] + [ i for i in range(3, k, 2) if s[i] ]
Run Code Online (Sandbox Code Playgroud)
我通过在10M以下重复生成质数来对此代码进行基准测试:
st = time()
for x in range(1000):
rt = time() …
Run Code Online (Sandbox Code Playgroud) 我昨天偶然发现了这个奇怪的情况,其中t as D
返回一个非空值,但(D)t
会导致编译器错误.
由于我匆忙,我只是使用t as D
和继续,但我很好奇为什么演员阵容无效,因为t
真的是一个D
.任何人都可以解释为什么编译器不喜欢演员?
class Program
{
public class B<T> where T : B<T> { }
public class D : B<D> { public void M() { Console.Out.WriteLine("D.M called."); } }
static void Main() { M(new D()); }
public static void M<T>(T t) where T : B<T>
{
// Works as expected: prints "D.M called."
var d = t as D;
if (d != null)
d.M();
// Compiler error: …
Run Code Online (Sandbox Code Playgroud) Eratosthenes的Sieve是一种相当快速的生成素数的方法,k
如下所示:
p = (2, 3, 4, ..., k)
和i = 2
.i^2
,删除的倍数i
从p
.i
的p
,直到i >= sqrt(k)
.我当前的实现看起来像这样(明显优化预过滤所有偶数):
# Compute all prime numbers less than k using the Sieve of Eratosthenes
def sieve(k):
s = set(range(3, k, 2))
s.add(2)
for i in range(3, int(sqrt(k)), 2):
if i in s:
for j in range(i ** 2, k, i * 2):
s.discard(j)
return sorted(s) …
Run Code Online (Sandbox Code Playgroud) c# ×5
python ×3
algorithm ×1
benchmarking ×1
casting ×1
css ×1
datetime ×1
doctype ×1
enumerable ×1
html ×1
linq ×1
performance ×1
prefix ×1
tryparse ×1