对我来说,与其他任何东西(甚至是另一个null类型)进行比较的null类型是未定义的操作.如果我错了,请纠正我.
根据这个假设,以下内容对我有意义:
nil.is_a? Comparable
=> false
nil.respond_to? :<=
=> false
nil.respond_to? :<
=> false
nil.respond_to? :>=
=> false
nil.respond_to? :>
=> false
Run Code Online (Sandbox Code Playgroud)
但是,nil 确实响应"宇宙飞船"比较运算符:
nil.respond_to? :<=>
=> true
Run Code Online (Sandbox Code Playgroud)
我无法想象比较nil甚至有意义的情况,更不用说实际了.为什么nil会有这种行为?
我的目录中有一个文件列表:
opencv_calib3d.so2410.so
opencv_contrib.so2410.so
opencv_core.so2410.so
opencv_features2d.so2410.so
opencv_flann.so2410.so
opencv_highgui.so2410.so
opencv_imgproc.so2410.so
opencv_legacy.so2410.so
opencv_ml.so2410.so
opencv_objdetect.so2410.so
opencv_ocl.so2410.so
opencv_photo.so2410.so
Run Code Online (Sandbox Code Playgroud)
它们是批量重命名的一系列错误的产物,现在我不知道如何从它们中删除中间的“.so”。例如:
opencv_ocl.so2410.so 应该 opencv_ocl2410.so
这是我尝试过的:
# attempt 1, replace the (first) occurrence of `.so` from the filename
for f in opencv_*; do mv "$f" "${f#.so}"; done
# attempt 2, escape the dot
for f in opencv_*; do mv "$f" "${f#\.so}"; done
# attempt 3, try to make the substring a string
for f in opencv_*; do mv "$f" "${f#'.so'}"; done
# attempt 4, combine 2 and 3 …Run Code Online (Sandbox Code Playgroud) 我正在尝试double从json字符串反序列化二维数组的值。以下代码复制了我的问题:
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
// Here is the json string I'm deserializing:
string json = @"{
""array2D"": [
[
1.2120107490162675,
-0.05202334010360783,
-0.9376574575207149
],
[
0.03548978958456018,
1.322076093231865,
-4.430964590987738
],
[
6.428633738739363e-05,
-1.6407574756162617e-05,
1.0
]
],
""y"": 180,
""x"": 94
}";
// Here is how I deserialize the string:
JObject obj = JObject.Parse(json);
int x = obj.Value<int>("x");
int y = obj.Value<int>("y");
// PROBLEM: InvalidCastException occurs at this line:
double[,] array = obj.Value<double[,]>("array2D");
Run Code Online (Sandbox Code Playgroud)
这两个整数x和y具有期望的值94和 …
我刚刚阅读了Eric Lippert的" Arrays被认为是有害的 "文章.他告诉他的读者,他们"可能不应该将数组作为公共方法或财产的价值返回",其理由如下(稍微解释):
现在调用者可以使用该数组并用他们喜欢的任何内容替换它的内容.回归的明智之举是
IList<>.你可以自己构建一个很好的只读集合对象,然后根据需要传递对它的引用.
在本质上,
数组是变量的集合.调用者不需要变量,但是如果这是获取值的唯一方法,它将采用它们.但是被调用者和调用者都不希望这些变量发生变化.
为了理解变量与值的含义,我构建了具有Array和IList<>字段的demo类,以及返回对两者的引用的方法. 这是在C#Pad上.
class A {
private readonly int[] arr = new []
{
10, 20, 30, 40, 50
};
private readonly List<int> list = new List<int>(new []
{
10, 20, 30, 40, 50
});
public int[] GetArr()
{
return arr;
}
public IList<int> GetList()
{
return list;
}
}
Run Code Online (Sandbox Code Playgroud)
如果我理解正确,这种GetArr()方法是Lippert的不良做法,GetList()是他明智的做法.这是一个不良调用者可变性行为的演示GetArr():
var a = new A();
var arr1 = a.GetArr(); …Run Code Online (Sandbox Code Playgroud) 我有一个这样的模块结构:
module Parent
PARENT_CONST = 'parent const'
def Parent.meth
puts 'parent meth'
end
end
module Child
include Parent
end
Run Code Online (Sandbox Code Playgroud)
这按预期工作:
puts Parent::PARENT_CONST
=> parent const
Parent.meth
=> parent meth
Run Code Online (Sandbox Code Playgroud)
我可以从孩子访问父母的常量,但不能访问方法。像这样:
Child::PARENT_CONST
=> parent const
Child.meth
=> in `<main>': undefined method `meth' for Child:Module (NoMethodError)
Run Code Online (Sandbox Code Playgroud)
有没有办法让我做到这一点?
这是来自流行的EmguCV包的类签名.这是Image课堂 - 并非所有课程都很重要:
/// <summary>
/// An Image is a wrapper to IplImage of OpenCV.
/// </summary>
/// <typeparam name="TColor">Color type of this image (either Gray, Bgr, Bgra, Hsv, Hls, Lab, Luv, Xyz, Ycc, Rgb or Rbga)</typeparam>
/// <typeparam name="TDepth">Depth of this image (either Byte, SByte, Single, double, UInt16, Int16 or Int32)</typeparam>
public partial class Image<TColor, TDepth>
: CvArray<TDepth>, IImage, IEquatable<Image<TColor, TDepth>>
where TColor : struct, IColor
where TDepth : new()
Run Code Online (Sandbox Code Playgroud)
具体来说,注意
...
where TDepth : new() // …Run Code Online (Sandbox Code Playgroud) SortedListin 的默认容量是System.Collections.Generic多少?有人告诉我它是4,但下面的例子显示0.为什么?
SortedList<int,string> sortedlist = new SortedList<int,string>();
Console.WriteLine(sortedlist.Capacity);
Run Code Online (Sandbox Code Playgroud) 我正在使用C#和Visual Studio 2015通过ClickOnce开发和部署应用程序,并且试图缩小ClickOnce更新下载大小。 ClickOnce的是能够唯一增量更新:
更新应用程序时,ClickOnce会将当前应用程序的应用程序清单中指定的文件的哈希签名与新版本进行比较。如果不同,则ClickOnce下载新版本。如果签名匹配,则ClickOnce复制现有文件并在新版本的应用程序中使用它。即使只更改了一个或两个文件,该方法也可以避免ClickOnce再次下载整个应用程序。
我已验证,在的更新之间MyApp,大多数文件的哈希签名不会更改(在命令行上使用shasum或在实际的清单文件中)。但是ClickOnce仍会在每次更新时下载每个文件。
例如,MyApp取决于Emgu.CV.dll,而我只是MyApp 1.0.9.4从Visual Studio 2015中发布的。这是Emgu.CV.dll位于输出输出位置清单文件的引用MyApp\Application Files\MyApp_1_0_9_4\MyApp.exe.manifest:
<dependentAssembly dependencyType="install" allowDelayedBinding="true" codebase="Emgu.CV.dll" size="363520">
<assemblyIdentity name="Emgu.CV" version="3.0.0.2158" publicKeyToken="7281126722AB4438" language="neutral" processorArchitecture="msil" />
<hash>
<dsig:Transforms>
<dsig:Transform Algorithm="urn:schemas-microsoft-com:HashTransforms.Identity" />
</dsig:Transforms>
<dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha256" />
<dsig:DigestValue>lUb/oa0aQL6HWEhY8Juj6Mc1wChKo0owhJJ+sSfqZUs=</dsig:DigestValue>
</hash>
</dependentAssembly>
Run Code Online (Sandbox Code Playgroud)
这是先前部署中相同的.dll的依赖项标签MyApp_1_0_9_2\MyApp.exe.manifest:
<dependentAssembly dependencyType="install" allowDelayedBinding="true" codebase="Emgu.CV.dll" size="363520">
<assemblyIdentity name="Emgu.CV" version="3.0.0.2158" publicKeyToken="7281126722AB4438" language="neutral" processorArchitecture="msil" />
<hash>
<dsig:Transforms>
<dsig:Transform Algorithm="urn:schemas-microsoft-com:HashTransforms.Identity" />
</dsig:Transforms>
<dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha256" /> …Run Code Online (Sandbox Code Playgroud) 某些浮点数具有二进制浮点表示的固有不准确性:
> puts "%.50f" % (0.5) # cleanly representable
0.50000000000000000000000000000000000000000000000000
> puts "%.50f" % (0.1) # not cleanly representable
0.10000000000000000555111512312578270211815834045410
Run Code Online (Sandbox Code Playgroud)
这不是什么 新鲜事.但为什么红宝石BigDecimal也表现出这种行为呢?
> puts "%.50f" % ("0.1".to_d)
0.10000000000000000555111512312578270211815834045410
Run Code Online (Sandbox Code Playgroud)
(我正在使用rails速记.to_d而BigDecimal.new不仅仅是为了简洁,这不是特定于rails的问题.)
问题:为什么"0.1".to_d仍然会出现10 -17的错误?我认为目的BigDecimal明确是为了避免这样的不准确?
起初我认为这是因为我将一个已经不准确的浮点转换0.1为BigDecimal,并且BigDecimal只是无损地表示不准确.但我确保我使用的是字符串构造函数(如上面的代码片段所示),这应该可以避免这个问题.
编辑:
更多的调查表明,BigDecimal内部仍然干净利落.(很明显,因为否则这将是一个非常广泛使用的系统中的一个巨大的错误.)这是一个仍然显示错误的操作示例:
> puts "%.50f" % ("0.1".to_d * "10".to_d)
1.00000000000000000000000000000000000000000000000000
Run Code Online (Sandbox Code Playgroud)
如果表示是有损的,那将显示与上面相同的误差,只是移动了一个数量级.这里发生了什么?
我很惊讶我无法在stackoverflow或MSDN上找到这个问题的答案.我强烈怀疑我的搜索技巧是这里的差距,但无论如何我都会冒险.我在stackoverflow 上看了这 三个 帖子.这些都不是我所要求的直接问题或答案,但它们相关性很大,我希望无论如何都要从中收集答案.但没有运气!无论如何,这是问题!
当我定义一个声明Action<int, int>属性的接口时
public interface ICoordinateProcessor {
System.Action<int, int> onTwoIntegers { get; }
}
Run Code Online (Sandbox Code Playgroud)
它可以使用返回null的lambda轻松实现,并将两个整数作为参数
public class RealCoordinateProcessor : ICoordinateProcessor {
public override Action<int, int> onTwoIntegers {
get {
return (x, y) => this.someInternalState = x + y;
}
}
}
Run Code Online (Sandbox Code Playgroud)
十分简单!但是当我使用roslyn自动完成界面时,它会填写以下内容:
public class RealCoordinateProcessor : ICoordinateProcessor {
public override Action<int, int> onTwoIntegers => throw new NotImplementedException();
}
Run Code Online (Sandbox Code Playgroud)
编译没有错误或警告,也是我从未见过并且更喜欢使用的非常简洁的语法. 如何使用更严格的语法来获得与上面第二个片段相同的效果?
或者等效地,如何在第三个片段中访问lambda的参数? 当我尝试这个:
public override Action<int, int> onTwoIntegers (x, y) => throw new …Run Code Online (Sandbox Code Playgroud) 我觉得我疯了.为什么模式不\s匹配单个空格字符?见下文或此垫.
Console.WriteLine(Regex.IsMatch(@" ", " "));
// True
Console.WriteLine(Regex.IsMatch(@"\s", " "));
// False!?
Run Code Online (Sandbox Code Playgroud) 我想O(1)在O(N)时间复杂度上不使用任何额外的内存(空间复杂度)来反转字符串,字符串N的长度在哪里。
我不能在内置函数中使用字符串 reverse。
c# ×7
ruby ×3
arrays ×2
asp.net ×1
auto-update ×1
bash ×1
batch-rename ×1
bigdecimal ×1
casting ×1
clickonce ×1
collections ×1
comparable ×1
emgucv ×1
filenames ×1
generics ×1
hash ×1
json ×1
json.net ×1
lambda ×1
linux ×1
methods ×1
mixins ×1
module ×1
null ×1
precision ×1
properties ×1
python ×1
regex ×1
reverse ×1
string ×1