사용자 도구

사이트 도구


groovy:제어문

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

다음 판
이전 판
groovy:제어문 [2012/11/30 12:17]
kwon37xi 새로 만듦
groovy:제어문 [2013/05/17 14:50] (현재)
kwon37xi [for]
줄 2: 줄 2:
   * [[http://groovy.codehaus.org/Control+Structures|Groovy Control Structure]]   * [[http://groovy.codehaus.org/Control+Structures|Groovy Control Structure]]
   * http://groovy.codehaus.org/JN2535-Control   * http://groovy.codehaus.org/JN2535-Control
 +  * [[http://groovy.codehaus.org/Looping|Groovy Looping]]
  
 +===== for =====
 +  * for 문은 거의 사용할 일이 없다.
 +  * Index가 필요한 for 문은 ''list.eachWithIndex { obj, idx -> 처리문... }'' 형태로 만들면 된다.<code groovy>
 +def stringList = [ "java", "perl", "python", "ruby", "c#", "cobol",
 +                   "groovy", "jython", "smalltalk", "prolog", "m", "yacc" ]
 +
 +stringList.eachWithIndex() { obj, i -> println " ${i}: ${obj}" }
 +</code>
 ===== switch/case ===== ===== switch/case =====
   * case 에는 ''isCase()'' 구현체 아무것이나 와도 된다. ''1..10'' 형태의 Range도 isCase()를 구현하고 있다.   * case 에는 ''isCase()'' 구현체 아무것이나 와도 된다. ''1..10'' 형태의 Range도 isCase()를 구현하고 있다.
 +  * [[http://ndpar.blogspot.kr/2011/06/functional-groovy-switch-statement.html|Functional Groovy switch statement]]<code groovy>
 +class CaseCategory {
 +    static boolean isCase(Closure casePredicate, Object switchParameter) {
 +        casePredicate.call switchParameter
 +    }
 +}
  
-==== Functional case ==== +use (CaseCategory) { 
-  * http://ndpar.blogspot.kr/2011/06/functional-groovy-switch-statement.html+    switch (param) { 
 +        case { it % 2 == 0 } : return 'even' 
 +        case { it % 3 == 0 } : return 'threeven' 
 +        case { 0 < it }      return 'positive' 
 +        default              : return 'negative' 
 +    } 
 +
 +</code>
groovy/제어문.1354245435.txt.gz · 마지막으로 수정됨: 2012/11/30 12:17 저자 kwon37xi