如何强制Java在除以0.0或从负双提取根时抛出算术异常?代码如下:
double a = 1; // or a = 0 to test division by 0
double b = 2;
double c = 100;
double d = b*b - 4*a*c;
double x1 = (-b - Math.sqrt(d)) / 2 / a;
double x2 = (-b + Math.sqrt(d)) / 2 / a;
Run Code Online (Sandbox Code Playgroud) 直观地说,后者应该比前者更快.但是,当我看到基准测试结果时,我感到非常惊讶:
require 'benchmark/ips'
b = (0..20).to_a;
y = 21;
Benchmark.ips do |x|
x.report('<<') { a = b.dup; a << y }
x.report('+=') { a = b.dup; a += [y] }
x.report('push') { a = b.dup; a.push(y) }
x.report('[]=') { a = b.dup; a[a.size]=y }
x.compare!
end
Run Code Online (Sandbox Code Playgroud)
结果是:
Calculating -------------------------------------
<< 24.978k i/100ms
+= 30.389k i/100ms
push 24.858k i/100ms
[]= 22.306k i/100ms
-------------------------------------------------
<< 493.125k (± 3.2%) i/s - 2.473M
+= 599.830k (± 2.3%) i/s - 3.009M
push 476.374k (± 3.3%) …
Run Code Online (Sandbox Code Playgroud) 如何在Clojure中重写这个Ruby代码?
seq = [1, 2, 3, 4, 5].each_cons(2)
#=> lazy Enumerable of pairs
seq.to_a
=> [[1, 2], [2, 3], [3, 4], [4, 5]]
Run Code Online (Sandbox Code Playgroud)
Clojure的:
(??? 2 [1 2 3 4 5])
;=> lazy seq of [1 2] [2 3] [3 4] [4 5]
Run Code Online (Sandbox Code Playgroud) 我有一张地图m
,一把钥匙k
和一个功能f
.是否可以更简单地重写此代码?
(assoc m k (f (get m k))
Run Code Online (Sandbox Code Playgroud) 我有如下扩展方法:
public static T GetValueAs<T, R>(this IDictionary<string, R> dictionary, string fieldName)
where T : R
{
R value;
if (!dictionary.TryGetValue(fieldName, out value))
return default(T);
return (T)value;
}
Run Code Online (Sandbox Code Playgroud)
目前,我可以通过以下方式使用它:
var dictionary = new Dictionary<string, object>();
//...
var list = dictionary.GetValueAs<List<int>, object>("A"); // this may throw ClassCastException - this is expected behavior;
Run Code Online (Sandbox Code Playgroud)
它工作得很好,但是第二种类型的参数确实很烦人。在C#4.0中是否可以重写GetValueAs这样一种方法,使得该方法仍将适用于不同类型的字符串键字典,并且无需在调用代码中指定第二种类型的参数,即use
var list = dictionary.GetValueAs<List<int>>("A");
Run Code Online (Sandbox Code Playgroud) 或至少像 var list = dictionary.GetValueAs<List<int>, ?>("A");
Run Code Online (Sandbox Code Playgroud) 代替 var list = dictionary.GetValueAs<List<int>, object>("A");
Run Code Online (Sandbox Code Playgroud) 我正在处理复杂的注册表。我的用户将拥有他们的个人资料,其中包含联系信息和项目集合。我的架构(用户、个人资料、电话、集合、项目)如下所示:
defmodule MyProject.User
schema "users" do
field :email, :string
field :name, :string
has_many :profiles, MyProject.Profile, on_delete: :delete_all
has_many :collections, through: [:profiles, :collections]
timestamps()
end
end
defmodule MyProject.Profile
schema "profiles" do
field :address, :string
field :skype, :string
belongs_to :user, MyProject.User
has_many :phones, MyProject.Phone, on_delete: :delete_all
has_many :collections, MyProject.Collection, on_delete: :delete_all
timestamps()
end
end
defmodule MyProject.Phone
schema "phones" do
field :phone, :string
belongs_to :profile, MyProject.Profile
end
end
defmodule MyProject.Collection
schema "collections" do
field :name, :string
has_many :items, MyProject.Item, on_delete: :delete_all
timestamps()
end …
Run Code Online (Sandbox Code Playgroud) 正如 Elixir文档所说:
...Access 透明地忽略 nil 值:
iex> keywords = [a: 1, b: 2]
iex> keywords[:c][:unknown]
nil
Run Code Online (Sandbox Code Playgroud)
这对我来说似乎很容易出错。我宁愿看到nil[key]
失败也不愿回来nil
。
我很确定语言开发人员选择这条路线是有充分理由的。那是什么原因呢?
我创建了一个Chrome扩展程序,用于扫描页面并在弹出窗口中创建当前页面的h1-h6标签列表.这是主StackOverflow页面的列表:
h1 | All Questions
h3 | XmlElement has a list as attribute but its items aren't separated by comma
h3 | Eclipse, Easily remove/fix all @Override due to Java version change
...
Run Code Online (Sandbox Code Playgroud)
我想要一个"导出"按钮,这样我就能够以CSV格式保存此报告.可能吗?
clojure ×2
elixir ×2
ruby ×2
.net-4.0 ×1
arrays ×1
benchmarking ×1
c# ×1
covariance ×1
ecto ×1
exception ×1
generics ×1
java ×1
nested-forms ×1
performance ×1