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 / excel.groovy
Last active May 15, 2024 14:01
Create a styled Excel spreadsheet with Groovy and Apache POI
@Grab( 'org.apache.poi:poi:3.9' )
import static org.apache.poi.ss.usermodel.CellStyle.*
import static org.apache.poi.ss.usermodel.IndexedColors.*
import org.apache.poi.hssf.usermodel.HSSFWorkbook
new HSSFWorkbook().with { workbook ->
def styles = [ LIGHT_BLUE, LIGHT_GREEN, LIGHT_ORANGE ].collect { color ->
createCellStyle().with { style ->
fillForegroundColor = color.index
fillPattern = SOLID_FOREGROUND
@timyates
timyates / Stairs.groovy
Last active June 26, 2023 22:50
Groovy version of Frege-stairs doodle
import java.awt.Color
import java.awt.Graphics2D
import static java.awt.RenderingHints.*
import java.awt.image.BufferedImage
import groovy.transform.Immutable
@Immutable class Point { double x, y }
@Immutable class Trail { Point first, second, third, last }
Closure<Point> bearing = { Point start, Point over ->
@timyates
timyates / fact.groovy
Created March 6, 2013 14:16
Y combinator factorial in Groovy
fact_improver = { partial ->
{ BigInteger n ->
n == 0 ? 1 : n * partial( n - 1 )
}
}
y = { improver ->
{ gen -> { BigInteger n -> improver( gen( gen ) )( n ) } } { gen ->
{ BigInteger n -> improver( gen( gen ) )( n ) } }
}
@timyates
timyates / FizzBuzz.java
Created November 15, 2014 12:27
FizzBuzz with no Conditionals in Java 8 with RxJava
// Basically a Java copy of https://twitter.com/adereth/status/530740818420957184
import rx.Observable;
import java.util.Arrays;
import java.util.function.Function;
public class FizzBuzz {
public static void main(String[] args) {
Function<Integer,String> f = a -> "Fizz";
@timyates
timyates / arrow.groovy
Last active February 9, 2022 00:07
Using AsciImage from Groovy
// Requires Groovy 2.4+ and Java 8
@Grab('com.bloidonia:ascii-image:1.1')
import com.bloidonia.asciiimage.AsciImageProcessor
import java.awt.image.BufferedImage
import java.awt.RenderingHints
import java.awt.Color
AsciImageProcessor.fromLines([
'. . . . . . .',
'. . 3 . . . .',
@timyates
timyates / arrowdoc.groovy
Created April 11, 2015 09:10
ascii-image and document-builder sitting in a tree
@Grab(group='com.craigburke.document', module='pdf', version='0.3.1')
@Grab(group='com.craigburke.document', module='word', version='0.3.1')
@Grab('com.bloidonia:ascii-image:1.1')
import com.bloidonia.asciiimage.AsciImageProcessor
import java.awt.image.BufferedImage
import java.awt.RenderingHints
import java.awt.Color
import javax.imageio.ImageIO
import com.craigburke.document.builder.PdfDocumentBuilder
@timyates
timyates / compose.kt
Created April 4, 2012 12:57
Composing functions in Kotlin
fun compose<T>( fa: ( T ) -> T, fb : ( T ) -> T ) : ( T ) -> T {
return { ( a : T ) : T -> fb( fa( a ) ) }
}
fun main( args : Array<String> ) {
val composed = compose<Int>( { ( a : Int ) : Int -> a + 10 }, // add 10
{ ( a : Int ) : Int -> a * 2 } ) // multiply by 2
println( composed( 2 ) ) // prints 24
}
@timyates
timyates / Main.java
Last active August 17, 2021 20:36
Overlay a struct over a block of memory in C
// And similar in Panama (Azul java 16.0.2 with --add-modules jdk.incubator.foreign)
package com.bloidonia;
import jdk.incubator.foreign.GroupLayout;
import jdk.incubator.foreign.MemoryLayout;
import jdk.incubator.foreign.MemoryLayouts;
import jdk.incubator.foreign.MemorySegment;
import jdk.incubator.foreign.SequenceLayout;
import java.lang.invoke.VarHandle;
@timyates
timyates / Intro.md
Created August 6, 2013 15:49
File structure to Json object

Given the directory structure:

folder/
 |--- test/
 |     |--- for/
 |           |--- groovy/
 |           |     \--- afile.groovy
 |           |--- insidefor.txt
 |           |--- insidefor2.txt

|--- woo.html

@timyates
timyates / abacus.groovy
Last active July 18, 2020 14:17
A GroovyFX version of the Canoo JavaFX Abacus tutorial part IV
@Grab('org.codehaus.groovyfx:groovyfx:0.3.1')
import static groovyx.javafx.GroovyFX.start
final int ROW_COUNT = 10
final int COL_COUNT = 10
final int RADIUS = 20
final int DIAMETER = 2 * RADIUS
final int MOVE_WAY = 8 * DIAMETER
final int WIDTH = COL_COUNT * DIAMETER + MOVE_WAY
final int HEIGHT = ROW_COUNT * DIAMETER