Skip to content

Instantly share code, notes, and snippets.

View timyates's full-sized avatar

Tim Yates timyates

  • Manchester, UK
View GitHub Profile
@Grab( 'com.netflix.rxjava:rxjava-groovy:0.17.1' )
import rx.Observable
import rx.Observer
import rx.subscriptions.Subscriptions
def fibObs = Observable.create { Observer<BigInteger> observer ->
Thread t
t = Thread.start {
def prev = 0G
def next = 0G
@timyates
timyates / groovy.groovy
Created March 11, 2014 19:46
Blog post for Java groovy-stream integration
Stream.from( x:[1,2,3], y:[4,5,6] )
.map { x + y }
.filter { it % 2 }
.each { println it }

In the current groovy-stream master branch (0.8), I'm adding Java support.

Say you have two lists, [1, 2, 3] and [4, 5, 6] and you want to print all the sums of these numbers that are odd.

You can now do this in Java 1.6+

Map<String,List<Integer>> map = new HashMap<String,List<Integer>>() {{
    put( "x", Arrays.asList( 1, 2, 3 ) ) ;
    put( "y", Arrays.asList( 4, 5, 6 ) ) ;
@timyates
timyates / case.groovy
Created February 19, 2014 11:54
CamelCase, Snake_case and caterpillar-case with Guava
@Grab( 'com.google.guava:guava:16.0.1' )
import static com.google.common.base.CaseFormat.*
String.metaClass.caseFormat = { from, to ->
from.to( to, delegate )
}
// From camelCase (LOWER_CAMEL) to SNAKE_CASE (UPPER_UNDERSCORE)
assert 'varName'.caseFormat( LOWER_CAMEL, UPPER_UNDERSCORE ) == 'VAR_NAME'
@timyates
timyates / Collate.java
Last active May 30, 2016 08:32
Implementing Groovy's collate method in Java 8 Streams
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/* Efficiency must be questioned
*
* Also, no error checking, so could go infinite if called with dodgy params
*
* (left as an exercise for the reader) ;-)
@timyates
timyates / wheee.groovy
Created January 27, 2014 12:51
Spinner whilst parsing files in Groovy on the command line
@Grab( 'com.bloidonia:groovy-common-extensions:0.5.5' )
@Grab('com.xlson.groovycsv:groovycsv:1.0')
import static com.xlson.groovycsv.CsvParser.parseCsv
import groovy.transform.*
// A bufferedReader with some output
@InheritConstructors
class SpinnerReader extends BufferedReader {
private String output = '/-\\|'
private int offset = 0
@timyates
timyates / withNull.groovy
Created January 22, 2014 16:01
Idiosyncrasies of Groovy's with method and null values
import org.codehaus.groovy.runtime.NullObject
def map = [ a: 1, b: 2 ]
// map.c is equal to null
assert map.c == null
// But then if we use with
map.c.with { c ->
// c is not equal to null!
@timyates
timyates / lenstest.groovy
Last active January 3, 2016 05:29
Functional Lenses and Groovy
import groovy.transform.*
// Immutable test object
def tim = new Person( 'tim', new Address( 'Woo', 123 ) )
// Lens for setting address for a person
Lens personAddress = new Lens( { p -> p.address }, { p, a -> new Person( p.name, a ) } )
// And one for setting zipcode for an address
Lens addressZipcode = new Lens( { a -> a.zip }, { a, z -> new Address( a.street, z ) } )
//Project Euler # 10
//http://projecteuler.net/problem=10
@GrabResolver( name='s', root='https://oss.sonatype.org/repositories/snapshots' )
@Grab(group='org.gperfutils', module='gbench', version='0.4.2-groovy-2.1')
@Grab( 'com.bloidonia:groovy-stream:0.7.0-SNAPSHOT' )
import groovy.transform.*
import groovy.stream.Stream
import groovyx.gbench.Benchmark