更新主要问题仍然是示例下的问题,但我想它归结为:
**如果你有一种类型,其中99%的值可以用一种快速,强大的类型表示,而只有1%用非常重的类型表示,(比如int与BigInteger)如何表示?**
一所学校我们学到了很多关于内部表征的知识,但从未在运行时如何改变它.我的意思是:假设你有一个表示小数的类,但是你使用一个整数来表示它是内部的,直到你实际上需要一个比整数更大的值,而只是改变表示...
我之前从未想过这个,当我想到它时,我认为这样做永远不会奏效,因为所有的检查都会杀掉它.但是我只是做了一个测试,因为我对自己的好处太好奇了,并且确实存在更改表示更多perormant的情况:给定这个接口:
interface INumber
{
void add1000();
void SetValue(decimal d);
decimal GetValue();
}
Run Code Online (Sandbox Code Playgroud)
我找到了两种实现的,后者是更强大了很多情况,包括这一次,我组成,以吸引对此事的很多想法我可以(不是代表,它的社区)
1. Representation by only a decimal
public class Number1:INumber
{
private decimal d { get; set; }
public void add1000()
{
d += 1000;
}
public decimal GetValue()
{
return d;
}
public void SetValue(decimal d)
{
this.d = d;
}
}
2. Representation by a decimal and an int
public class Number2:INumber
{
private bool usedecimal;
private int i;
private decimal …Run Code Online (Sandbox Code Playgroud) 给出数组时:
int[] a={1,3,4,5,67,8,899,56,12,33}
Run Code Online (Sandbox Code Playgroud)
如果我希望使用LINQ返回偶数
var q=a.where(p=>p%2==0)
Run Code Online (Sandbox Code Playgroud)
如果我使用C#2.0并严格执行func <>委托解决它的方法是什么?
我试过了 :
Func<int, bool> func = delegate(int val) { return val % 2 == 0; };
Run Code Online (Sandbox Code Playgroud)
但我很困惑如何在这里链接数组"a".
我对使用gcc或Visual Studio打包的STL实现感到好奇,因此快速的Google搜索结果显示了一些结果,例如:
在什么情况下应该使用替代标准模板库?
例如,Apache的页面有一个列表,其中包括"完全符合C++标准"和"针对快速编译和极小的可执行文件大小进行了优化"等项目.如果它太好了,为什么不能取代libstdc ++?
我正在创建一个xml文件,并有一个XSD文件可以解决.
我确定我记得在某处可以看到C#在给定XSD时可以自动创建类对象.因此,如果我在XML中有一个地址元素,我可以使用生成的C#类,它使用xsd来创建类的必需属性.
例如<adress><postcode></postcode><phone></phone>
将映射到名为Address的类,其中包含属性邮政编码和电话.
这种事情是可能的还是我梦想的?
我不得不从rails app上的基于mySql的ruby迁移到使用postgresql.到目前为止没有问题,但我不知道如何解决它.
数据的迁移带来了id,而postgresql现在遇到了现有ID的问题:我不清楚它在哪里获得用于确定nextval基数的值:它肯定不是最高值专栏,虽然您可能认为这是一个好主意.无论如何,它现在与现有的id值发生冲突.从标准RoR迁移创建的id列定义为
not null default nextval('geopoints_id_seq'::regclass)
Run Code Online (Sandbox Code Playgroud)
是否有一些地方可以将其作为基础使用的价值入侵?这个问题现在可能出现在20个左右的表格中:我可以使用
'select max(id) from <table_name>'
Run Code Online (Sandbox Code Playgroud)
但这似乎使自动增量柱的想法毫无意义.
如何最好地处理?
我听说过人们应该总是使用Iterator模式来控制循环,而不是抛出异常(这是在Python迭代器中完成的方式)或使用Sentinel模式,从而返回一个特殊的,哨兵值(通常null)表示迭代的结束.
最佳做法是否建议不要使用哨兵模式?如果是这样,为什么?(除了它不使用foreachJava 1.5中的语法).
编辑:代码示例1 - Sentinel模式
Reader r = ...;
for( int val = r.read(); val != -1; val = r.read()) {
doSomethingWith(val);
}
Run Code Online (Sandbox Code Playgroud)
代码示例2 - 迭代器模式
for(Iterator<Thing> it = getAnIterator() ; it.hasNext(); ) {
Thing t = it.next();
doSomethingWith(t);
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用PowerShell创建一些备份,然后将这些备份复制到Web文件夹(或者换句话说,将它们上传到WebDAV共享).
起初我以为我会从PowerShell中做WebDAV的东西,但似乎这仍然需要相当数量的"手工劳动",即:构建HTTP请求.然后我决定从脚本创建一个Web文件夹,让Windows处理WebDAV的东西.似乎一切都需要创建一个Web文件夹创建一个标准快捷,描述在这里.
我无法弄清楚的是如何将文件实际复制到快捷方式的目标..?也许我会以错误的方式解决这个问题.
如果我能以某种方式加密脚本中的WebDAV凭据,然后让它创建Web文件夹,分流文件,再次删除Web文件夹,那将是理想的选择.或者甚至更好,根本不使用网络文件夹.第三种选择是手动创建Web文件夹并将其留在那里,但我宁愿不这样做.
任何想法/指针/提示?:)
我正在使用编程集体智慧的以下代码,这是一本计算两位电影评论家之间的eclidian距离的书中的函数.
此函数对字典中排名的差异求和,但n维中的欧几里德距离还包括该和的平方根.
AFAIK,因为我们使用相同的功能对每个人进行排名无关紧要我们是否平分根,但我想知道是否有特定原因?
from math import sqrt
# Returns a distance-based similarity score for person1 and person2
def sim_distance(prefs,person1,person2):
# Get the list of shared_items
si={}
for item in prefs[person1]:
if item in prefs[person2]:
si[item]=1
# if they have no ratings in common, return 0
if len(si)==0: return 0
# Add up the squares of all the differences
sum_of_squares=sum([pow(prefs[person1][item]-prefs[person2][item],2)
for item in prefs[person1] if item in prefs[person2]])
return 1/(1+sum_of_squares)
Run Code Online (Sandbox Code Playgroud) 在如何在python中"cd"的问题中,接受的答案建议在类中包装os.chdir调用以使返回原始dir异常安全.这是推荐的代码:
class Chdir:
def __init__( self, newPath ):
self.savedPath = os.getcwd()
os.chdir(newPath)
def __del__( self ):
os.chdir( self.savedPath )
Run Code Online (Sandbox Code Playgroud)
有人可以详细说明这是如何使不安全的呼叫异常安全的吗?
设置使用webHttpBinding的WCF服务...我可以从XML方法返回复杂类型.如何将复杂类型作为参数?
[ServiceContract(Name = "TestService", Namespace = "http://www.test.com/2009/11")]
public interface ITestService
{
[OperationContract]
[WebInvoke(Method = "POST",
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "/Person/{customerAccountNumber}, {userName}, {password}, {PersonCriteria}")]
Person SubmitPersonCriteria(string customerAccountNumber,
string userName,
string password,
PersonCriteria details);
}
Run Code Online (Sandbox Code Playgroud)
由于UriTemplate只允许字符串,最佳做法是什么?这个想法是客户端应用程序将向服务发布请求,例如一个人的搜索条件.该服务将使用包含XML数据的相应对象进行响应.