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