Skip to content

Instantly share code, notes, and snippets.

View timyates's full-sized avatar

Tim Yates timyates

  • Manchester, UK
View GitHub Profile
@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'

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 / 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 }
@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 / combineLatestTest.groovy
Last active August 29, 2015 13:57
Messing with Groovy and RxJava's combineLatest
@Grab( 'com.netflix.rxjava:rxjava-groovy:0.17.1' )
import rx.*
import rx.schedulers.*
import java.util.concurrent.TimeUnit
// Emit the date every second
def date = Observable.create { observer ->
Schedulers.newThread().schedulePeriodically( { inner ->
observer.onNext( new Date() )
}, 0, 1000, TimeUnit.MILLISECONDS )
@timyates
timyates / _README.md
Created March 24, 2014 14:16
Simple recommendation system written in Groovy based on Jaccard index.
@timyates
timyates / piapprox.groovy
Last active August 29, 2015 13:59
Pi Approximation in GPars with Groovy
import groovy.transform.*
import groovyx.gpars.actor.*
import groovyx.gpars.group.*
@Immutable class Calculate {}
@Immutable class Work { int start, nrOfElements }
@Immutable class Result { double value }
@Immutable class PiApproximation { double pi ; long duration }
double calculatePiFor( int start, int nrOfElements ) {
@timyates
timyates / YCombinatorFactorial.java
Last active August 29, 2015 14:00
Y Combinator based factorial in Java 8
package test;
import java.math.BigInteger;
import java.util.function.Function;
public class YCombinatorFactorial<T> {
private interface Improver<T> {
Function<T,T> apply( Improver<T> f ) ;
}
@timyates
timyates / LongAdderTest.java
Last active August 29, 2015 14:00
Gaussian distribution in Java 8 with parallel Streams and LongAdder
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.LongAdder;
import java.util.stream.IntStream;
public class LongAdderTest {
ConcurrentHashMap<Integer,LongAdder> frequencyMap = new ConcurrentHashMap<>() ;
Random rnd = new Random() ;
void run() {
@timyates
timyates / _new.md
Last active August 29, 2015 14:00
New things in Java 7 and 8