我最愚蠢的延伸是在2008年...... [英] My silliest extention made yet 2008...

查看:68
本文介绍了我最愚蠢的延伸是在2008年......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

......而且只有一月。这可能会变得更糟。 =)


我真的很喜欢像C#这样的语言,它在IEnumerable< T>上有一个foreach。


但有时候你需要去索引,你必须for循环。

在C-dervived语言中它通常是这样的:


for(int i = 0; i< theList.Count -1; i ++){[etc .. etc ..]}


我一直都喜欢基于0的偏移,即使我们用索引来谈论。 br />
但是作为一个索引,我一直不喜欢代码的-1部分

计数/长度。


我不要以为我一个人在承认有时你会错过那个小b / b $ b减去一个,并且索引超出范围的例外。一个错误的原因和

你的程序有缺陷。


所以我尝试在IList上创建扩展< T>

简单返回Count-1的简单函数

首先是UpperBound()

for(int i = 0; i< theList.UpperBound() ; i ++){[etc .. etc ..]}


这让我看起来太像Visual Basic了,所以不用了。


在Delphi / Pascal中你有序数类型的pred()和succ(),所以你可以

(并且应该):i / $
for i:= 0到pred(theList.Count)开始[etc .. etc ..]结束;


我试过:

for(int i = 0 ; i< theList.PredCount(); i ++){[etc .. etc ..]}


这完全没有意义,所以我试着结束:

for(int i = 0; i< theList.End(); i ++){[etc .. etc ..]}


我试过几个停止和结束单词的组合以及我能带来的最好的结果是.ToEnd()


for(int i = 0; i< theList.ToEnd(); i ++){[etc .. etc ..]}


任何想法,除了我应该甚至想到这个? =)


- Michael Starberg

解决方案

Michael Starberg写道:


..而且它只是一月。这可能会变得更糟。 =)



关于一天没有愚蠢的问题就像

a一天没有Usenet这里有一些鼓舞人心的报价。


实际上,我刚刚做到了,但应该有。


我真的很喜欢像C#这样的语言,它在IEnumerable< ; T> ;.


但有时你需要按索引进行操作,你必须循环使用。

在C-dervived语言中,它通常看起来像这样:


for(int i = 0; i< theList.Count -1; i ++){[etc .. etc ..]}


我总是喜欢基于0的偏移量,即使我们用索引来说话。

但是作为一个索引,我一直不喜欢
计数/长度。



可能是因为它错了。你的循环将从0到theList.Count

- 2 ...你错过了最后一个元素,即List.Count - 1.即,

除非列表为空。然后你的循环会立即对无效的

索引进行barf,因为0肯定不小于-1。


for(int i = 0; i! = theList.Count; ++ i){...}


这是一种非常自然的写作方式。事实上,有一个原因

人们更喜欢从零开始的索引:它为你提供了更少的机会来实现

一个一个错误。
< blockquote class =post_quotes>
我不认为我独自承认有时你会错过那个小的

减一,索引超出范围的例外。一个错误的原因和

你的程序有缺陷。



见上文......不要写那个,然后。


任何想法,除了我应该ToEnd甚至想到这个? =)



零是你应该开始的数字,起始

的数字应为零。你不能从一开始,除非你算上
苹果。两个就出来了。


-

J.


2008年1月17日星期四09:59:46 -0800,Michael Starberg

< mi *************************** @ gmail .comwrote:


但是有时候你需要按索引去,你必须循环。

在C-dervived语言中它通常看起来像这样:


for(int i = 0; i< theList.Count -1; i ++){[etc .. etc ..]}


我总是喜欢基于0的偏移,即使我们用

索引来说话。

但是作为一个索引,我总是不喜欢代码的-1部分

计数/长度。



我不知道你在说什么。如果你想按列表枚举列表中的每个

元素,条件是<而不是< =

(这是典型的),你需要theList.Count,而不是theList.Count - 1。


您发布的代码是将错过最后一个元素。


Pete


Michael Starberg写道:


for(int i = 0; i< theList.Count -1; i ++){[etc .. etc ..]}


我一直很喜欢基于0的偏移量,即使我们用

指数来说话。但是作为一个索引,我一直不喜欢

的-1部分计数/长度的代码。



嗯?在C#中,你根本不使用-1(你必须将它与

Delphi for loop混淆):


for(int i = 0; i< theList.Count; ++ i)


注意第二个是while条件,IOW:


for (A; B; C)

{

D;

}


相当于:


A;

而(B)

{

D;

C;

}


-

Rudy Velthuis http://rvelthuis.de


在事实真相之前,谎言会在全世界中途传播

有机会穿上裤子。

- 温斯顿丘吉尔爵士(1874-1965)


... and it is only January. This might get worse. =)

I really like languages like C#, that has a foreach on IEnumerable<T>.

But sometimes you need to go by index and you have to for-loop.
In C-dervived languages it typically looks like this:

for (int i = 0; i < theList.Count -1 ; i++) { [etc.. etc..] }

I have have always liked 0-based offsets, even if we talk in terms of index.
But for being a index, I have always disliked the -1 part of the code on
Count/Length.

I don''t think I am alone when confessing that sometimes you miss that little
minus one, with a index out of bounds exception. A lame reason for a bug and
your program to be flawed.

So I tried creating extentions on IList<T>
Simple function that simply returns Count-1

First up was UpperBound()
for (int i = 0; i < theList.UpperBound() ; i++) { [etc.. etc..] }

That made it look way tooo much like Visual Basic for me, so no thanks.

In Delphi/Pascal you have pred() and succ() on ordinal types, so you could
(and should) do:
for i := 0 to pred(theList.Count) do begin [etc.. etc..] end;

I tried that:
for (int i = 0; i < theList.PredCount() ; i++) { [etc.. etc..] }

That made no sense at all, so I tried end:
for (int i = 0; i < theList.End() ; i++) { [etc.. etc..] }

I''ve tried several combinations of stop and end words and the best I can
come with is .ToEnd()

for (int i = 0; i < theList.ToEnd() ; i++) { [etc.. etc..] }

Any thoughts, except that I should ToEnd even thinking of this? =)

- Michael Starberg

解决方案

Michael Starberg wrote:

.. and it is only January. This might get worse. =)

There''s some inspiring quote about a day without stupid questions being like
a day without Usenet.

Actually, I just made that up, but there ought to be.

I really like languages like C#, that has a foreach on IEnumerable<T>.

But sometimes you need to go by index and you have to for-loop.
In C-dervived languages it typically looks like this:

for (int i = 0; i < theList.Count -1 ; i++) { [etc.. etc..] }

I have have always liked 0-based offsets, even if we talk in terms of index.
But for being a index, I have always disliked the -1 part of the code on
Count/Length.

Probably because it''s wrong. Your loop will go from 0 through theList.Count
- 2... You''re missing the last element, which is theList.Count - 1. That is,
unless the list is empty. Then your loop will immediately barf on an invalid
index, since 0 is certainly not smaller than -1.

for (int i = 0; i != theList.Count; ++i) { ... }

This is a perfectly natural way of writing things. In fact, there''s a reason
people prefer zero-based indexing: it gives you much less opportunity for
off-by-one errors.

I don''t think I am alone when confessing that sometimes you miss that little
minus one, with a index out of bounds exception. A lame reason for a bug and
your program to be flawed.

See above... don''t write that, then.

Any thoughts, except that I should ToEnd even thinking of this? =)

Zero is the number thou shalt start from, and the number of the starting
shall be zero. One thou shalt not start from, unless thou by counting
apples. Two is right out.

--
J.


On Thu, 17 Jan 2008 09:59:46 -0800, Michael Starberg
<mi***************************@gmail.comwrote:

But sometimes you need to go by index and you have to for-loop.
In C-dervived languages it typically looks like this:

for (int i = 0; i < theList.Count -1 ; i++) { [etc.. etc..] }

I have have always liked 0-based offsets, even if we talk in terms of
index.
But for being a index, I have always disliked the -1 part of the code on
Count/Length.

I have no idea what you''re talking about. If you want to enumerate every
element in the list, by index, and the condition is "<" instead of "<="
(which is typical), you need "theList.Count", not "theList.Count - 1".

The code you posted is going to miss the last element.

Pete


Michael Starberg wrote:

for (int i = 0; i < theList.Count -1 ; i++) { [etc.. etc..] }

I have have always liked 0-based offsets, even if we talk in terms of
index. But for being a index, I have always disliked the -1 part of
the code on Count/Length.

Huh? In C#, you don''t use -1 at all (you must be confusing it with a
Delphi for loop):

for (int i = 0; i < theList.Count; ++i)

Note that the second is a while condition, IOW:

for (A; B; C)
{
D;
}

is equivalent to:

A;
while (B)
{
D;
C;
}

--
Rudy Velthuis http://rvelthuis.de

"A lie gets halfway around the world before the truth has a
chance to get its pants on."
-- Sir Winston Churchill (1874-1965)


这篇关于我最愚蠢的延伸是在2008年......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆