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 / maybe.java
Created June 23, 2014 16:32
Java 7-ification of renderRay
private Polygon renderRay( Point mp, Set<Point> points, Set<Segment> segmentSet ) {
Polygon lightPoly = new Polygon();
List<Point> beams = new ArrayList<>();
List<Double> raypoints = new ArrayList<>();
for( Point p : points ) {
double atan = Math.atan2(p.getY() - mp.getY(), p.getX() - mp.getX());
raypoints.add(atan - 0.001);
raypoints.add(atan);
raypoints.add(atan + 0.001);
}
@timyates
timyates / README.md
Created August 14, 2014 07:47
my latest jdk switcher for os x bash

Once this is in your shell, you can just do:

$ jdk 6
Using Java 6 /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

$ java -version
java version "1.6.0_65"
Java(TM) SE Runtime Environment (build 1.6.0_65-b14-466.1-11M4716)
Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-466.1, mixed mode)
@timyates
timyates / test.R
Created August 14, 2014 09:00
lubridate and ISO8601
library(lubridate)
dates = c('2014-08-14T12:00:00+01:00',
'2014-08-14T14:46:34',
'2014-08-14T14:46:34.876',
'2014-08-14T14:46:34Z',
'2014-08-14T14:46:34-05:00',
'2014-08-14T14:46:34Z')
print(paste('Given', paste(dates, collapse=', ')))
@timyates
timyates / test.java
Created September 25, 2014 13:14
Cache control in Ratpack with Java
chain(context -> {
handler(ctx -> {
ctx.getResponse().getHeaders().set("Pragma", "No-cache");
ctx.getResponse().getHeaders().set("Cache-Control", "no-cache,no-store,max-age=0");
ctx.getResponse().getHeaders().setDate("Expires", new Date(LocalDate.ofEpochDay(0).atStartOfDay().toInstant(ZoneOffset.UTC).toEpochMilli()));
ctx.next();
});
assets("public");
});
@timyates
timyates / FunctionalGameOfLife.java
Created November 18, 2014 16:22
First pass at the Game of Life in Java 8 Streams
import java.awt.*;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
@timyates
timyates / fb.groovy
Created December 23, 2014 22:19
FizzBuzz with groovy-stream and no conditionals
@Grab('com.bloidonia:groovy-stream:0.9.0')
import groovy.stream.*
def (n, f, b, fb) = [Closure.IDENTITY, { 'fizz' }, { 'buzz' }, {'fizzbuzz'}]
def fns = Stream.from([n, n, f, n, b, f, n, n, f, b, n, f, n, n, fb]).repeat()
def num = Stream.from(1..100).zip(fns) { counter, fn -> fn(counter) }
num.each { println it }
@timyates
timyates / RxSplitByType.java
Created December 28, 2014 22:36
RXJava Split based on Type
/**
* Not sure if this is the best way to do this... Comments more than welcome
*
* Given a stream of Events, how best to split them to be processed separately
*/
import rx.Observable;
import rx.subjects.BehaviorSubject;
import java.util.HashMap;
@timyates
timyates / Counter.groovy
Last active August 29, 2015 14:15
7guis counter
import javafx.geometry.Insets
import static groovyx.javafx.GroovyFX.start
start {
stage(title: 'Counter', show: true, onHidden:{e -> System.exit(0)}) {
scene {
hbox(spacing:10, padding:new Insets(10)) {
textField(id: 'count', editable: false, prefColumnCount: 10, text:'0')
button('count', onAction:{e -> count.text = (count.text as Integer) + 1})
}
@timyates
timyates / SupplierCircuitBreaker.java
Created February 21, 2015 22:44
Saturday night with Java 8 and Circuit Breakers...
import rx.Observable;
import rx.functions.Func4;
import rx.subjects.BehaviorSubject;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalUnit;
import java.util.function.Supplier;
public class SupplierCircuitBreaker<T> implements Supplier<T> {
@timyates
timyates / dream.groovy
Last active August 29, 2015 14:16
Filed under "wouldn't it be nice" ;-)
def matcher = ~/(?<year>\d\d\d\d)-(?<month>\d\d)-(?<day>\d\d)/
switch('2014-10-10') {
case matcher: { year, month, day ->
println "On day $day of month $month, in the year $year"
}
}