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 / _new.md
Last active August 29, 2015 14:00
New things in Java 7 and 8
@timyates
timyates / LocalDateRange.groovy
Created May 1, 2014 11:32
Quick metaClass addition to get Java 8 LocalDate to play with Groovy ranges
// Requires Groovy 2.3 and Java 8
import java.time.*
// Add a next() method
LocalDate.metaClass.next = { delegate.plusDays( 1 ) }
// Then print the next two weeks worth of dates
(LocalDate.now()..LocalDate.now().plusWeeks( 2 )).each {
println it
@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 / 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 / Controller.java
Last active March 27, 2017 17:57
Observable Animation Timer with JavaFX 8 and RxJava
package sample;
import javafx.animation.AnimationTimer;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.paint.Color;
import rx.Observable;
import rx.subscriptions.Subscriptions;
@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 / pisystem.groovy
Last active July 23, 2018 13:27
Pi Approximation With Akka and Groovy
@Grab( 'com.typesafe.akka:akka-actor_2.10:2.3.2' )
@Grab( 'com.typesafe:config:1.2.0' )
import groovy.transform.Immutable
import akka.actor.ActorRef
import akka.actor.ActorSystem
import akka.actor.Props
import akka.actor.UntypedActor
import akka.actor.UntypedActorFactory
import akka.routing.RoundRobinRouter
import scala.concurrent.duration.Duration
@timyates
timyates / forceOverrideTraits.groovy
Last active October 21, 2015 04:25
Messing with groovy traits
// Bad example of @ForceOverride... enjoy! (?)
import groovy.transform.*
class BasicIntQueue extends LinkedList<Integer> {
Integer get() {
this.poll()
}
void put( Integer x ) {
@timyates
timyates / _README.md
Created March 24, 2014 14:16
Simple recommendation system written in Groovy based on Jaccard index.
@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 )