Chris Uppal said:
How do you handle two simulataneous iterations ?
Do you mean nested iterations?
(Another interpretation could be iterations in two threads.)
A type implementing both »Iterator« and »Iterable« as if by
public java.util.Iterator iterator(){ return this; }
is called an »iteration« by me.
An example for nested iterations:
public class Main
{ public static void main( final java.lang.String[] args )
{
for( java.lang.Integer i : new
de.dclj.ram.system.iteration.IntegralRange( 10, 12 ))
java.lang.System.out.println( i );
for( de.dclj.ram.type.tuple.Tuple i :
new de.dclj.ram.system.iteration.TupleNesting
<java.lang.Integer,java.lang.Integer>
( new de.dclj.ram.system.iteration.IntegralRange( 1, 3 ),
new de.dclj.ram.system.iteration.IntegralRange( 2, 5 )))
java.lang.System.out.println( i ); }}
This prints:
10
11
12
( 1; 2 )
( 1; 3 )
( 1; 4 )
( 1; 5 )
( 2; 2 )
( 2; 3 )
( 2; 4 )
( 2; 5 )
( 3; 2 )
( 3; 3 )
( 3; 4 )
( 3; 5 )
The first loop is an example of a simple iteration.
The second shows an iteration combinator combining two
iterations into a new nested iteration. (I believe, you asked
whether this is possible.)
I also could have used two nested for-loops. The above code
only was written with the intention to also demonstrate
»TupleNesting«
~~~
The source code for the types used should be
browsable as part of my ram.jar publication:
http://www.purl.org/stefan_ram/pub/ram-jar
(The »distribution« on this page is an older version, possibly
not containing the types used here. But the link »Online
API-specification with links to online-source code« should
lead to the source code. This is a preliminary and incomplete
pre-alpha publication of my library only. I am in the process
of preparing an alpha release.)
~~~
Another example: How to get the comma separated segments
from the file »tmp.txt«?
1,2,3
alpha, beta, 3
Answer: Combine a line iteration with a segment iteration:
public class Main
{ public static void main( final java.lang.String[] args )
{ de.dclj.ram.java.io.LineReadableAndClosable reader = new
de.dclj.ram.java.io.DefaultLineReadableAndClosable
( new java.io.BufferedReader
( new de.dclj.ram.java.io.FileReader
( new de.dclj.ram.java.io.DefaultPath( "tmp.txt" )).getFileReader() ));
for( java.lang.String i :
new de.dclj.ram.system.iteration.DependentNesting
<java.lang.String,java.lang.String>
( new de.dclj.ram.java.io.Lines( reader ),
de.dclj.ram.java.lang.StringSegments.class ))
java.lang.System.out.println( i ); }}
This prints:
1
2
3
alpha
beta
3
(StringSegments objects always use the comma "," as a
separator, customizing them to use another seperator might be
possible in a later version.)