File tree Expand file tree Collapse file tree 5 files changed +64
-15
lines changed
Expand file tree Collapse file tree 5 files changed +64
-15
lines changed Original file line number Diff line number Diff line change 11class Collection (list ):
2- pass
2+ def add (self ,o ):
3+ self .append (o )
4+
5+ def iterator (self ):
6+ return Iterator (self )
37
48
59class List (Collection ):
@@ -24,12 +28,12 @@ class ArrayList(Collection):
2428 def __init__ (self ,typ ):
2529 self ._typ = typ
2630
27- def add ( self , o ):
28- self . append ( o )
29-
30- def iterator (self ):
31- return Iterator ( self )
32-
33-
34-
31+
32+ class Arrays ( object ):
33+ @ classmethod
34+ def asList (self , * a ):
35+ res = List ( )
36+ res . extend ( a )
37+ return res
38+
3539
Original file line number Diff line number Diff line change 1+ // from http://www.javapractices.com/topic/TopicAction.do?Id=88
2+
3+ import java .util .*;
4+
5+ /** Different iteration styles. */
6+ public class IterateNoIndex {
7+
8+ /**
9+ * Iterating without an index is more compact and less
10+ * error prone.
11+ */
12+ public static void withoutIndex (){
13+ //for-each loop
14+ List <String > trees = Arrays .asList ("Maple" , "Birch" , "Poplar" );
15+ for (String tree : trees ){
16+ log (tree );
17+ }
18+
19+ //with an Iterator
20+ Iterator <String > iter = trees .iterator ();
21+ while (iter .hasNext ()) {
22+ log (iter .next ());
23+ }
24+ }
25+
26+ /** Iterating with an index is more error prone. */
27+ public static void withIndex (){
28+ //traditional for-loop
29+ for (int idx =0 ; idx < 10 ; ++idx ){
30+ log ("Iteration..." );
31+ }
32+ }
33+
34+ // PRIVATE //
35+ private static void log (String aMessage ){
36+ System .out .println (aMessage );
37+ }
38+
39+ public static void main ( String ... aArguments ) {
40+ withoutIndex ();
41+ withIndex ();
42+ }
43+
44+ }
45+
Original file line number Diff line number Diff line change 44
55public final class LoopStyles {
66
7- public static void main (String ... aArguments ) {
7+ public static void main ( String ... aArguments ) {
88 List <String > flavours = new ArrayList <String >();
99 flavours .add ("chocolate" );
1010 flavours .add ("strawberry" );
Original file line number Diff line number Diff line change @@ -25,10 +25,10 @@ strategies
2525
2626 expr-to-box: Plus(e) -> H hs=0 [ "+" ~e]
2727 expr-to-box: Minus(e) -> H hs=0 [ "-" ~e]
28- expr-to-box: PreIncr(e) -> H hs=0 [ "++" ~e "###NOTIMPL### "]
29- expr-to-box: PreDecr(e) -> H hs=0 [ "--" ~e "###NOTIMPL### "]
30- expr-to-box: PostIncr(e) -> H hs=0 [~e "++" "###NOTIMPL### "]
31- expr-to-box: PostDecr(e) -> H hs=0 [~e "--" "###NOTIMPL### "]
28+ expr-to-box: PreIncr(e) -> H hs=0 [~e " += 1 ##PreIncr "]
29+ expr-to-box: PreDecr(e) -> H hs=0 [~e " -= 1 ##PreDecr "]
30+ expr-to-box: PostIncr(e) -> H hs=0 [~e " += 1 ##PostIntcr "]
31+ expr-to-box: PostDecr(e) -> H hs=0 [~e " -= 1 ##PostDecr "]
3232
3333 expr-to-box: Mul(e1, e2) -> H hs=1 [~e1 "*" ~e2]
3434 expr-to-box: Div(e1, e2) -> H hs=1 [~e1 "/" ~e2]
Original file line number Diff line number Diff line change @@ -168,7 +168,7 @@ rules
168168 H hs=0 [
169169 "while " ~*bcond ":"
170170 ]
171- H hs=1 [" " ~bupdate]
171+ H hs=0 [" " ~bupdate]
172172 ]
173173
174174 where
You can’t perform that action at this time.
0 commit comments