Skip to content

Instantly share code, notes, and snippets.

View timyates's full-sized avatar

Tim Yates timyates

View GitHub Profile
@timyates
timyates / C compile Linux
Created June 29, 2012 10:07
JNA and Groovy
gcc -o libgreet.so -shared greet.c
@timyates
timyates / Test.java
Created July 20, 2012 09:31
Java 7 and Groovy to find files in a folder less than N lines sorted so newest creation date is first
import java.io.IOException ;
import java.nio.file.* ;
import java.nio.file.attribute.BasicFileAttributes ;
import java.util.ArrayList ;
import java.util.Collections ;
import java.util.Comparator ;
import java.util.List ;
public class Test {
@timyates
timyates / new.groovy
Created July 23, 2012 14:55
Nomenclature
Stream s = Stream.from [1,2,3] filter { it >= 2 } map { it * mult } using mult:2
println s.collect() // prints [ 4, 6 ]
@timyates
timyates / bench.groovy
Created July 25, 2012 13:00
Benchmarking getting the last 4 elements of a list in reverse order in Groovy
@Grab('com.googlecode.gbench:gbench:0.3.0-groovy-2.0')
import gbench.*
[10,100,1000,10000,100000].each { max ->
println "For (1..$max)"
new BenchmarkBuilder().run( measureCpuTime:false, quiet:true ) {
'Reverse and take' {
assert (1..max).reverse().take( 4 ) == [ max, max - 1, max - 2, max - 3 ]
}
'Take with backwards listIterator' {
@timyates
timyates / allthewaydown.groovy
Created July 27, 2012 11:02
Streams of Streams
@GrabResolver( name='bloidonia', root='https://raw.github.com/timyates/bloidonia-repo/master')
@Grab('com.bloidonia:groovy-stream:0.5.1')
import groovy.stream.Stream
@Grab('commons-io:commons-io:2.4')
import org.apache.commons.io.LineIterator
// A copy of "The Raven" by the Brothers Grimm
new URL( 'http://www.cs.cmu.edu/~spok/grimmtmp/070.txt' ).with {
@timyates
timyates / sort.groovy
Created August 10, 2012 14:02
Sort with multiple comparators
List list = [
[id:0, firstName: 'Sachin', lastName: 'Tendulkar', age: 40 ],
[id:1, firstName: 'Sachin', lastName: 'Tendulkar', age: 103 ],
[id:2, firstName: 'Ajay', lastName: 'Tendulkar', age: 48 ],
[id:3, firstName: 'Virendra', lastName: 'Sehwag', age: 5 ],
[id:4, firstName: 'Virendra', lastName: 'Sehwag', age: 50 ],
[id:5, firstName: 'Sachin', lastName: 'Nayyar', age: 15 ]
]
Collection.metaClass.sort = { boolean mutate, Closure... closures ->
@timyates
timyates / README.md
Created August 16, 2012 14:00
Run core.logic Clojure script from Groovy
@timyates
timyates / ensureClosed.groovy
Created September 11, 2012 12:43
Add an ensureClosed method to Object in Groovy
Object.metaClass.ensureClosed = { Closure c ->
try {
c( delegate )
}
finally {
[ delegate ].flatten().each {
if( it.respondsTo( 'close' ) ) {
it.close()
}
}
@timyates
timyates / fridayfun.groovy
Last active October 10, 2015 16:58
A Ping-Pong HotSwapped Untyped Akka Actor in Groovy
@Grab( 'com.typesafe.akka:akka-actor_2.10:2.3.2' )
@Grab( 'com.typesafe:config:1.2.0' )
import akka.actor.UntypedActor
import akka.japi.Procedure
import akka.actor.ActorSystem
import akka.actor.Props
import com.typesafe.config.ConfigFactory
class PingPongActor extends UntypedActor {
static final String PING = 'PING'
@timyates
timyates / ensembl.groovy
Created September 28, 2012 09:35
Quick and dirty querying of the ensembl REST api with Groovy
@Grab( 'org.codehaus.groovy.modules.http-builder:http-builder:0.5.2' )
import groovyx.net.http.RESTClient
// A connection to ensembl
def ensembl = new RESTClient( 'http://beta.rest.ensembl.org/' )
// Get a list of species
def spec = ensembl.get( path:'info/species',
query:[ 'content-type':'application/json' ] )