Skip to content

Instantly share code, notes, and snippets.

@pallove
Last active September 20, 2016 06:00
Show Gist options
  • Save pallove/706780c3586bf28669d435ea54160f69 to your computer and use it in GitHub Desktop.
Save pallove/706780c3586bf28669d435ea54160f69 to your computer and use it in GitHub Desktop.
groovy language exercise
import groovy.transform.Field;
@Field x = 1..5
def hello(obj) {
groovy_lang()
println "what the fuck $obj.a, $obj.b, $obj.c, $x.from, $x.to"
}
def groovy_lang() {
def noparam = {println "no param $it"}
def params = {n, v -> println "with params: $v.from, $v.to, n = $n"}
noparam("you")
params(20, x)
println "lang test"
x.each{ println "range: $it"}
def amap = [name : "leon", age: 30, website : "pallove.com", nickname : "pallove", walk : 30]
amap.findAll {it.value == 30}.each {
println "the key:$it.key"
}
def bmap = amap.findAll {it.value == 30}
bmap.each{println "value:$it.key"}
amap.findAll {k, v -> println "$k, $v"}
def result = [a:1, b:2, c:4, d:5].findAll { it.value % 2 == 0 }
// assert result.every { it instanceof Map.Entry }
assert result*.key == ["b", "c"]
assert result*.value == [2, 4]
def map=[:]
map.get("a", -1) << 20
println "map[a] = $map.a"
// assert map == [a:[5]]
def addmap = []
x.each{addmap << it}
addmap.each { println "append value:$it" }
one_arg(a:0, b:-1)
two_arg([b:30, a:40], { v ->
println "two_arg - closure called: $v"
})()
three_arg([a:'you', b:'are'], [c:'the', d:'one']) {
println "three_arg - closure called"
}
multi_arg(2, 3, 4)
(append_value(one_arg(a:0, b:-1)
, two_arg(b:100, a:99) {v -> println "two_arg in append arg $v"}
, three_arg([a:'what', b:"the"], [c:"fuck", d:"language"]) {println "dummy groovy language!"})
<< {
println "append_value function"
}).each { println "result:" + it; it() }
println "append_value end -----------------"
(append_value(one_arg(b:100, a:99)) << {
println "accept one_arg function"
}).each { println "result:" + it; it() }
(append_value(2, 3, 4) << 5).each{println "value:$it"}
([4, 5, 6] << [7, 8]).each{println "append value:$it"}
}
def one_arg(obj) {
println "one arg a = $obj.a, b = $obj.b"
{ -> println "closure call: one arg a = $obj.a, b = $obj.b"}
}
def two_arg(obj, closure) {
println "two arg obj = $obj"
closure(200)
return {println "closure call: two arg obj = $obj"}
}
def three_arg(obj1, obj2, closure) {
println "three_arg obj1 = $obj1, obj2 = $obj2"
return closure
}
def append_value(...args) {
args.toList()
}
def multi_arg(...args) {
def property = args.getProperties()
property.each{
println "$it.key=$it.value"
}
args.each {
println "...args:$it"
}
println "$args.length, $args"
def args_list = args.toList()
assert args instanceof Object[]
assert args_list instanceof List
}
task(compile) << {
hello b:30, a:"10", c:999
println 'compiling source'
println "compile $compile"
println "path: $compile.path, $compile.group"
assert dist instanceof Task
// def property = compile.getProperties()
// property.each{
// println "$it.key=$it.value"
// }
}
task(compileTest(dependsOn: compile)) << {
println 'compiling unit tests'
}
task test(dependsOn: [compile, compileTest]) << {
println 'running unit tests'
}
task dist(dependsOn: [compile, test]) << {
println 'building the distribution'
}
task langtest << {
println "############ test groov lang"
}
langtest.dependsOn compile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment