Groovy mode

x
 
1
//Pattern for groovy script
2
def p = ~/.*\.groovy/
3
new File( 'd:\\scripts' ).eachFileMatch(p) {f ->
4
  // imports list
5
  def imports = []
6
  f.eachLine {
7
    // condition to detect an import instruction
8
    ln -> if ( ln =~ '^import .*' ) {
9
      imports << "${ln - 'import '}"
10
    }
11
  }
12
  // print thmen
13
  if ( ! imports.empty ) {
14
    println f
15
    imports.each{ println "   $it" }
16
  }
17
}
18
19
/* Coin changer demo code from http://groovy.codehaus.org */
20
21
enum UsCoin {
22
  quarter(25), dime(10), nickel(5), penny(1)
23
  UsCoin(v) { value = v }
24
  final value
25
}
26
27
enum OzzieCoin {
28
  fifty(50), twenty(20), ten(10), five(5)
29
  OzzieCoin(v) { value = v }
30
  final value
31
}
32
33
def plural(word, count) {
34
  if (count == 1) return word
35
  word[-1] == 'y' ? word[0..-2] + "ies" : word + "s"
36
}
37
38
def change(currency, amount) {
39
  currency.values().inject([]){ list, coin ->
40
     int count = amount / coin.value
41
     amount = amount % coin.value
42
     list += "$count ${plural(coin.toString(), count)}"
43
  }
44
}
45

MIME types defined: text/x-groovy