This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@groovy.transform.Field String pattern = 'A__S__E' | |
class XWordCategory { | |
static boolean matchesPattern(String self, String pattern) { | |
pattern == null ? false : self ==~ pattern.replace('_', /\w{1}/) | |
} | |
} | |
use(XWordCategory) { | |
assert 'KEPI'.matchesPattern('____') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovy.time.TimeCategory | |
[Date, Integer].each { it.mixin(TimeCategory) } | |
println Date.parse('yyyy-MM-dd', '2014-04-04') + 1.months |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
assert String.format('%tA', Date.parse('yyyy-MM-dd', '2013-02-04')) == 'Monday' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Ex 1: Convert words in list to upper case | |
List<String> output = wordList.stream() | |
.map(String::toUpperCase) | |
.collect(Collectors.toList()); | |
// Ex 2: Finds words in list with even length | |
List<String> output = wordList.stream() | |
.filter(w -> w.length() & 1 == 0) | |
.collect(Collectors.toList()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
InputStream.metaClass.eachChunk << { int preferredChunkSize, Closure closure -> | |
delegate.eachByte(preferredChunkSize) { buffer, bytesRead -> | |
if (bytesRead == preferredChunkSize) { | |
closure(buffer) | |
} else if (bytesRead > 0) { | |
byte[] data = new byte[bytesRead] | |
System.arraycopy(buffer, 0, data, 0, bytesRead) | |
closure(data) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For moving jars in order to take advantage of Groovy's indy feature | |
def groovyHome = args.length == 0 ? new File('.') : new File(args[0]) | |
def libDir = new File(groovyHome, 'lib') | |
def indyDir = new File(groovyHome, 'indy') | |
if (!groovyHome.exists() || !libDir.exists() || !indyDir.exists() || !new File(groovyHome, 'bin').exists()) { | |
println "$groovyHome does not appear to be a valid Groovy Home" | |
System.exit(0) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int colToIndex(String col) { | |
col.length() == 1 ? col.chars[0] - 65 : (col.chars[0] - 64) * Math.pow(26, col.length() - 1) + colToIndex(col.substring(1)) | |
} | |
[A:0, B:1, Y:24, Z:25, AA:26, AB:27, AY:50, AZ:51, BA:52, BB:53, | |
BY:76, BZ:77, CA:78, CB:79, CY:102, CZ:103, DA:104, DB:105, | |
ZY:700, ZZ:701, AAA:702, AAB: 703, AAZ: 727, ABA: 728, AZZ: 1377, | |
BAA:1378 ].each { k, v -> | |
assert colToIndex(k) == v | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// returns combinations of the input list of the provided size, r | |
def combinationsOf = { List list, int r -> | |
assert (0..<list.size()).contains(r) | |
def combs = [] as Set | |
list.eachPermutation { | |
combs << it.subList(0, r).sort { a, b -> a <=> b } | |
} | |
combs as List | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Grab(group='org.ccil.cowan.tagsoup', module='tagsoup', version='1.2') | |
@Grab(group='org.springframework', module='spring-web', version='3.2.10.RELEASE') | |
import static org.springframework.web.util.UriUtils.encodeQuery | |
import groovy.transform.TupleConstructor | |
class MvnRepository { | |
def parser = new XmlSlurper(new org.ccil.cowan.tagsoup.Parser()) | |
String findGroupId(String artifactId) { | |
String groupId = null |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@tasklist /NH /FI "IMAGENAME eq chrome.exe" | groovy -e "int tm = 0, ci = 0; new BufferedReader(new InputStreamReader(System.in)).text.eachLine { def p = it.split(/\s{2,}/); if (p.length == 4) {tm += (p[-1] - ',' - ' K') as int; ci++} }; println tm + ' (' + ci + ' instances)'" |