int item = Math.random() * 100000
def doCode = { return 'hi' }
Map $_ = [a: 1, b: ['c d': [z: item, doit: doCode]]]
use (UtilCategory) {
assert $_.dotPath() == [a: 1, b: ['c d': [z: item, doit: doCode]]]
assert $_.dotPath('') == [a: 1, b: ['c d': [z: item, doit: doCode]]]
assert $_.dotPath(null) == [a: 1, b: ['c d': [z: item, doit: doCode]]]
assert $_.dotPath('a') == 1
assert $_.dotPath('b.c d.z') == item
assert $_.dotPath('b/c d/z', '/') == item
assert $_.dotPath('b.c d.doit')() == 'hi'
assert $_.dotPath('b.c d.y') == null
assert $_.dotPath('b.c d.z.z') == null
assert $_.dotPath('b.c d.z.z.') == null
assert 1.dotPath('toString') == null
}
//========================================
@Category(Object)
class UtilCategory {
def dotPath(String path = '', String delimiter = /\./) {
Object self = this
if (!path) return self
try {
return path.split(/$delimiter/).inject(self) { x, i->
x = x[i]
}
}
catch (Exception ex) { return null }
}
}
Showing posts with label category. Show all posts
Showing posts with label category. Show all posts
Thursday, January 16, 2014
dotPath
Wednesday, March 30, 2011
Groovy++ Mixin and Category
@Typed
package categoriestest
@Category(List)
@Typed
class Shuffler {
def shuffleit() {
def result = new ArrayList(this)
Collections.shuffle(result)
result
}
}
// @Mixin(Shuffler)
@Typed
class Sentence extends ArrayList {
Sentence(Collection initial) { super(initial) }
}
@Typed
class Main {
static main(args) {
def words = ["The", "quick", "brown", "fox"]
Sentence.mixin Shuffler.class
def sentence = new Sentence(words)
println sentence.shuffleit()
}
}
Main.main(null)
http://gppconsole.appspot.com/script/20001
Tuesday, March 29, 2011
Category - Advanced
class StringCategory {
static String camelize(String self) {
def newName = self.split("_").collect() {
it.substring(0, 1).toUpperCase() + it.substring(1, it.length())
}.join()
newName[0..<1].toLowerCase() + newName[1..-1]
}
static Integer getPm(Integer self) {
(self == 12 ? 12 : self + 12)
}
static Integer getAm(Integer self) {
self == 12 ? 0 : self
}
}
class SpeakCategory {
static String shout(String self) { // Method argument is String, so we can add shout() to String object.
self.toUpperCase() + '!!'
}
static String whisper(String self, boolean veryQuiet = false, Integer a = 0) {
"${veryQuiet ? 'sssssssh' : 'sssh'}.. $self $a"
}
static String army(String self) {
"$self. Sir, yes sir!"
}
}
use (StringCategory) {
println 'Hi_there_fella'.camelize()
println 12.am + ' o\'clock'
println 2.pm + ' o\'clock'
}
use (SpeakCategory) {
println "Pay attention".shout()
println "Be vewy, vewy, quiet.".whisper()
println "Be vewy, vewy, quiet.".whisper(true, 1)
println "Groovy rocks".army()
}
// Or we can use the @Category annotation.
// implicit 'this', no 'self' arg passed in
@Category(String)
class StreetTalk {
String hiphop(Integer times = 1) {
"Yo, yo, here we go! " * times + "${this}"
}
}
use(StreetTalk) {
println 'Groovy is dope!'.hiphop(2)
}
http://groovyconsole.appspot.com/script/448002
Category - Simple
class MathCategory {
static Number sin(Number self, Number add = 0){Math.sin(self) + add}
static Number cos(Number self, Number add = 0){Math.cos(self) + add}
}
use(MathCategory) {
println 42.sin()
println 42.sin(1)
println 42f.sin()
println 42f.class.name
println 42G.cos()
println 42G.cos(2)
println 42G.class.name
}
http://groovyconsole.appspot.com/script/451001
Subscribe to:
Comments (Atom)