foreach增强 [英] foreach enhancement

查看:76
本文介绍了foreach增强的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何增强foreach循环,允许这样的语法:


foeach(int in in 1..10){} // forward

foeach(int in in 99..2){} // backwards

foeach(char'in''a'''''z''){} // chars

foeach(红色中的颜色c。蓝色){} //使用枚举


它应该适用于所有整数数据类型。也许我们可以更进一步:


foeach(int in in 1..10,30..100){} //从1到10和30到100

也许我们也可以尝试进一步增强循环并使其可用

浮点类型:


foeach(浮点数f在1.0..9.5:0.5){} //从1.0到9.5,步长为0.5

这比for循环更具可读性并且打算更多更清楚。

我总是要思考我是否写了类似于for(int i = list.Count-1; i< = 0;

i--)的循环。

这是非生产性和错误的,很难维护。因此,我的建议。


-

cody


[免费软件,游戏和幽默] www.deutronium.de.vu || www.deutronium.tk

解决方案

我会投票给它。也许可以在正则表达式匹配集中使用

或任何类型的set工作。


-

William Stacey,MVP


" cody" < PL ********** @ gmx.de>在消息中写道

新闻:我们************** @ TK2MSFTNGP10.phx.gbl ...

如何增强foreach循环允许像
这样的语法:
foeach(int in in 1..10){} // forward
foeach(int i in 99..2){} //向后
foeach(红色中的颜色c。蓝色){} //使用枚举

它应该适用于所有整数数据类型。也许我们可以进一步增加

foeach(int in in 1..10,30..100){} //从1到10和30到100
也许我们也可以尝试进一步增强循环并使其可用于浮点类型:

foeach(float f in 1.0..9.5:0.5){} //从1.0到9.5步长为0.5

这比for循环更具可读性,并且使得打算更加清晰。
我总是要考虑是否写出像for这样的循环( int i = list.Count-1;
i< = 0; i - )" ;.
这是非生产性的,错误的并且难以维护。因此,我的
提案。
-

[免费软件,游戏和幽默]
www.deutronium.de.vu || www.deutronium.tk





" William Stacey [MVP]" < ST *********** @ mvps.org>在留言中写道

news:uk ************** @ TK2MSFTNGP12.phx.gbl ...

我会投票为了它。可能在正则表达式匹配集中可以用作

或任何类型的set工作。

或者您可以使用迭代器生成功能,无需添加语言即可实现您想要的功能。


foreach(int in in(1,10))




foreach(char c in From(''a'',''z' '))




foreach(字符串s在RegexMatch中(字符串,regexPattern))


等。


为语言添加几个语法添加(...,:和,在这种情况下),

对于这个边缘情况来说是远远不够的,我想。


现在,如果您可以将概念扩展到内联数组表达式或其他

表达式,那么您可以将自己置于更强大的位置。


考虑类似的东西(这些都是伪造的语法,只是建议)


int [] x = [1 ... 10: 2]; //等于新的int [] {1,3,5,7,9}


或者

int [] x = [1:2 - > 10]; // equilvilent to new int []

{1,3,5,7,9,11,13,15,17,19}


或set \array语法的其他部分。


尽管如此,将它装入语言中需要做些什么,我甚至还没有开始考虑b $ b开始考虑这种语法可能导致语言矛盾。

-
William Stacey,MVP

cody < PL ********** @ gmx.de>在消息中写道
新闻:我们************** @ TK2MSFTNGP10.phx.gbl ...

如何增强foreach循环,允许语法如



foeach(int in in 1..10){} // forward
foeach(int i in 99 .2){} // backwards
foeach(char'in''a'''''z''){} // chars
foeach(Color c in Red..Blue) {} //使用枚举

它应该适用于所有整数数据类型。也许我们可以进一步



foeach(int in in 1..10,30..100){} //从1到10和30到100

也许我们也可以尝试进一步增强循环并使它对于浮点类型也可用


foeach(浮点数f在1.0..9.5:0.5){} //从1.0到9.5,步长为0.5
这比for循环更具可读性并且打算更多
更清楚。
我总是要考虑是否写了类似于for(int i = list.Count-1;


i< = 0;

我 - )。
这是非生产性和错误的,很难维持。因此,我的


提案。


-


[免费软件,游戏和幽默] www.deutronium.de.vu || www.deutronium.tk


我觉得foreach所需的主要增强功能是允许在运行时更改集合

值。在某些情况下,需要添加和删除

来自集合的值。 Foreach声明不支持它

(有充分理由可能)


我的想法(或请求)


1.允许更改收集值


示例 -


foreach(pNodeList中的xmlNode pNode)

{

//允许添加新节点

pNodeList.Add(newNode);


//允许pNode到在foreach内被移除。

}


2.现在棘手的部分是刷新收集并启动foreach

循环再次修改后的集合。


foreach(pNodeList中的xmlNode pNode)

{

if(MyConditionMet == true)

{

pNodeList.Add(newNode);

pNodeList.Refresh()//刷新集合。


MyConditionMet = false;


//重启foreach循环的命令打破当前的一个

自收集修改后

foreach.ReStart_And_Break_current ;

}

}


欢迎评论


-

Shak

(休斯顿)

" cody" < PL ********** @ gmx.de>在消息中写道

新闻:我们************** @ TK2MSFTNGP10.phx.gbl ...

如何增强foreach循环允许像
这样的语法:
foeach(int in in 1..10){} // forward
foeach(int i in 99..2){} //向后
foeach(红色中的颜色c。蓝色){} //使用枚举

它应该适用于所有整数数据类型。也许我们可以进一步增加

foeach(int in in 1..10,30..100){} //从1到10和30到100
也许我们也可以尝试进一步增强循环并使其可用于浮点类型:

foeach(float f in 1.0..9.5:0.5){} //从1.0到9.5步长为0.5

这比for循环更具可读性,并且使得打算更加清晰。
我总是要考虑是否写出像for这样的循环( int i = list.Count-1;
i< = 0; i - )" ;.
这是非生产性的,错误的并且难以维护。因此,我的
提案。
-

[免费软件,游戏和幽默]
www.deutronium.de.vu || www.deutronium.tk



What about an enhancement of foreach loops which allows a syntax like that:

foeach(int i in 1..10) { } // forward
foeach(int i in 99..2) { } // backwards
foeach(char c in ''a''..''z'') { } // chars
foeach(Color c in Red..Blue) { } // using enums

It should work with all integral datatypes. Maybe we can step a bit further:

foeach(int i in 1..10, 30..100) { } // from 1 to 10 and 30 to hundred

And maybe we could also try to enhance the loop further and make it usable
for floating point types too:

foeach(float f in 1.0..9.5 : 0.5) { } // from 1.0 to 9.5 in steps of 0.5

This is far more readable as a for loop and makes the intend much clearer.
I always have to think if I write loops like "for (int i=list.Count-1; i<=0;
i--)".
This is unproductive and errorprone and hard to maintain. Thus, my proposal.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk

解决方案

I''ll vote for it. Could probably work in regex match sets into that as well
or any kind of "set" work.

--
William Stacey, MVP

"cody" <pl*************************@gmx.de> wrote in message
news:us**************@TK2MSFTNGP10.phx.gbl...

What about an enhancement of foreach loops which allows a syntax like that:
foeach(int i in 1..10) { } // forward
foeach(int i in 99..2) { } // backwards
foeach(char c in ''a''..''z'') { } // chars
foeach(Color c in Red..Blue) { } // using enums

It should work with all integral datatypes. Maybe we can step a bit further:
foeach(int i in 1..10, 30..100) { } // from 1 to 10 and 30 to hundred

And maybe we could also try to enhance the loop further and make it usable
for floating point types too:

foeach(float f in 1.0..9.5 : 0.5) { } // from 1.0 to 9.5 in steps of 0.5

This is far more readable as a for loop and makes the intend much clearer.
I always have to think if I write loops like "for (int i=list.Count-1; i<=0; i--)".
This is unproductive and errorprone and hard to maintain. Thus, my proposal.
--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk





"William Stacey [MVP]" <st***********@mvps.org> wrote in message
news:uk**************@TK2MSFTNGP12.phx.gbl...

I''ll vote for it. Could probably work in regex match sets into that as
well
or any kind of "set" work.
Or you could just use iterators to generate functions that achieve what you
want without adding to the language.

foreach (int i in From(1,10))

or
foreach (char c in From(''a'',''z''))

or
foreach (string s in RegexMatch(string,regexPattern))

etc.

adding several syntax additions(..., :, and , in this case) to the langauge
for this edge case is a touch to far, I think.

Now, if you can extend the concept out to inline array expressions or other
expressions in general you could get yourself in a much stronger position.

Consider something like(these are all psuedo syntax, just suggestions)

int[] x = [1...10 : 2]; // equivilent to new int[] { 1,3,5,7,9 }

or perhaps
int[] x = [1 : 2 -> 10]; //equilvilent to new int[]
{1,3,5,7,9,11,13,15,17,19}

or other bits of set\array syntax.

Still, fitting it into the langauge will take some doing, I havn''t even
started to consider language contradictions this syntax may cause.
--
William Stacey, MVP

"cody" <pl*************************@gmx.de> wrote in message
news:us**************@TK2MSFTNGP10.phx.gbl...

What about an enhancement of foreach loops which allows a syntax like


that:


foeach(int i in 1..10) { } // forward
foeach(int i in 99..2) { } // backwards
foeach(char c in ''a''..''z'') { } // chars
foeach(Color c in Red..Blue) { } // using enums

It should work with all integral datatypes. Maybe we can step a bit


further:


foeach(int i in 1..10, 30..100) { } // from 1 to 10 and 30 to hundred

And maybe we could also try to enhance the loop further and make it
usable
for floating point types too:

foeach(float f in 1.0..9.5 : 0.5) { } // from 1.0 to 9.5 in steps of 0.5

This is far more readable as a for loop and makes the intend much
clearer.
I always have to think if I write loops like "for (int i=list.Count-1;


i<=0;

i--)".
This is unproductive and errorprone and hard to maintain. Thus, my


proposal.


--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk



I feel major enhancement required in foreach is allowing the collection
value to be changed during runtime. In some situations, Adding and deleting
a value from collection is required. Foreach statement does not support it
(for good reason may be)

My thoughts (or requests)

1. Allow the collection value to be changed

example -

foreach(XmlNode pNode in pNodeList)
{
//allow new node to be added
pNodeList.Add(newNode);

//allow pNode to be removed inside foreach.
}

2. Now the tricky part is refreshing collection and starting the foreach
loop again for modified collection.

foreach(XmlNode pNode in pNodeList)
{
if(MyConditionMet == true)
{
pNodeList.Add(newNode);
pNodeList.Refresh () //refreshing collection.

MyConditionMet = false;

//command to restart the foreach loop breaking current one
since collection is modified
foreach.ReStart_And_Break_current;
}
}

Comments are welcomed

--
Shak
(Houston)
"cody" <pl*************************@gmx.de> wrote in message
news:us**************@TK2MSFTNGP10.phx.gbl...

What about an enhancement of foreach loops which allows a syntax like that:
foeach(int i in 1..10) { } // forward
foeach(int i in 99..2) { } // backwards
foeach(char c in ''a''..''z'') { } // chars
foeach(Color c in Red..Blue) { } // using enums

It should work with all integral datatypes. Maybe we can step a bit further:
foeach(int i in 1..10, 30..100) { } // from 1 to 10 and 30 to hundred

And maybe we could also try to enhance the loop further and make it usable
for floating point types too:

foeach(float f in 1.0..9.5 : 0.5) { } // from 1.0 to 9.5 in steps of 0.5

This is far more readable as a for loop and makes the intend much clearer.
I always have to think if I write loops like "for (int i=list.Count-1; i<=0; i--)".
This is unproductive and errorprone and hard to maintain. Thus, my proposal.
--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk



这篇关于foreach增强的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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