Re: case and foreach
- Posted by codepilot Gmail Account <codepilot at gmail.com> Mar 31, 2005
- 558 views
I think the modified "for x in s do" is just about perfect. But what about selectively not doing the iteration on some elements, like every even one. for x in s do-all end for for i = 1 to length(seq) by 2 do--odd x=seq[i] end for for i = 2 to length(seq) by 2 do--even x=seq[i] end for for x in s by 2 do--odd end for for x=2 in s by 2 do--even constant bitmap={1,0,1,0,1,0,1,0} for x in s by bitmap do--any arbitrary bitmap same length as s end for --same as for i = 1 to length(s) do if not bitmap[i] then next end if x=s[i] end for --but since there is not next for i = 1 to length(s) do if bitmap[i] then x=s[i] .............. ............. .......... end if end for Lets try this example and see the optimization sequence a,b a=rand(repeat(128,256))-1--256 seq all bytes b=rand(repeat(128,256))-1--256 seq all bytes-- for i = 1 to length(a) do a[i]+=b[i] end for--normal method for x in a and y in b do x+=y end for--new method --new method can allow for sse2 optimization of 16bytes fit in each, and if you could hint the code to tell the translator or interpreter that 'a' and 'b' will all be bytes then it could do the following mov eax, address of a mov ebx, address of b MOVDQA xmm1,[eax] PADDUSB xmm1, [ebx] MOVNTDQ [eax], xmm1 in a loop, and if it knew the length of the sequence it could even unroll it, and make huge speed. On Thu, 31 Mar 2005 15:03:21 -0800, Alexander Toresson <guest at rapideuphoria.com> wrote: > > posted by: Alexander Toresson <alexander.toresson at gmail.com> > > Jason Gade wrote: > > > > I've thought about foreach and I don't really think that it needs to be > > added to the > > language because it can be done with a function or procedure. > > > > foreach(list_of_stuff, routine_id(some_action)) > > > > or > > > > list_of_stuff = foreach(list_of_stuff, routine_id(some_action)) --Prefer > > pass by reference > > or optimization of this case > > Thing is, this puts the loop and the code inside the loop in different places > making the code harder to read. Imagine how eu code would be if the for loop > was implemented the same way, as a function or procedure! Spaghetti-code. > > > But it is something that could be helped by having pass by reference and an > > eval() > > function. > > > > One problem with a built-in foreach -- does it only iterate the top level of > > a sequence > > or does it dig down through sub sequences? I think that is one reason to > > leave it > > as a procedure or function. > > With the same logic, why should the find() function exist? It only iterates > the top level of a sequence, yet it's very useful and your code can get much > faster if you structure your sequences so you can use it. > > Regards, Alexander Toresson > > > >