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 / concat.groovy
Created February 14, 2012 12:28
Concatenation of Closures in Groovy
// take a boolean evaluating closure, a result for truth and a result for false
def fn_if = { cond, tr, fa, Object... overflow ->
if( cond() ) [ tr, *overflow ] else [ fa, *overflow ]
}
// take two parameters, and return the result of multiplying these together
def fn_mult = { a, b, Object... overflow ->
[ a * b, *overflow ]
}
@timyates
timyates / composition_1.groovy
Created February 14, 2012 16:08
Closure Composition to run a stream of tokens
// This will tag our closures so that we know to just push them on the Stack
def fnMethod = { c ->
c.getMetaClass().isFn = { -> true }
c
}
// Define our operations
def ops = [
fn_add: fnMethod( { a, b, Object... remaining -> [ a + b, *remaining ] } ),
fn_sub: fnMethod( { a, b, Object... remaining -> [ a - b, *remaining ] } ),
@timyates
timyates / step_1_clone.txt
Created February 16, 2012 14:39
Working with groovy snippets
$ git clone [email protected]:timyates/groovy-core.git
Cloning into groovy-core...
Warning: No xauth data; using fake authentication data for X11 forwarding.
remote: Counting objects: 144322, done.
remote: Compressing objects: 100% (32204/32204), done.
remote: Total 144322 (delta 92128), reused 144110 (delta 91947)
Receiving objects: 100% (144322/144322), 106.70 MiB | 5.17 MiB/s, done.
Resolving deltas: 100% (92128/92128), done.
@timyates
timyates / step_1_commit.txt
Created February 16, 2012 15:43
Working with Groovy snippets 2
(onearginject*) $ git add src/test/groovy/ClosureMethodTest.groovy
(onearginject*) $ git commit -m "Added tests for new one arg inject"
[onearginject fe2e59d] Added tests for new one arg inject
1 files changed, 17 insertions(+), 0 deletions(-)
@timyates
timyates / grid.groovy
Created February 22, 2012 15:52
What's new in Groovy 1.8.6 -- the collate method
def list = 1..10
def gridList = list.collate( 3 ).collate( 2 )
assert gridList == [ [ [1, 2, 3], [4, 5, 6] ],
[ [7, 8, 9], [10] ] ]
@timyates
timyates / base64Example.groovy
Created February 23, 2012 11:23
What's new in Groovy 1.8.6 -- byte[].encodeHex
String input = 'Groovy Base64 Encode'
String encoded = input.bytes.encodeBase64()
assert encoded == 'R3Jvb3Z5IEJhc2U2NCBFbmNvZGU='
byte[] decoded = encoded.decodeBase64()
assert input == new String( decoded )
@timyates
timyates / beware.groovy
Created February 24, 2012 13:22
What's new in Groovy 1.8.6: Array.contains()
// Given a List of BigDecimals
List bewareList = [ 1.1, 2.0, 3.1 ]
// And a matching array of double
double[] bewareArray = bewareList as double[]
def ival = 2 // This will be a java.lang.Integer
def dval = 2.0 // And this, a java.math.BigDecimal
// Both of these pass
assert bewareArray.contains( dval )
@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 / 1.groovy
Created April 10, 2012 13:36
A self contained Iterator from a Map in Groovy
def val = 1
def a = [ hasNext:{ true },
next:{ val++ } ] as Iterator
assert a.take( 2 ).collect() == [ 1, 2 ]
@timyates
timyates / LazyGenerator.groovy
Created April 20, 2012 14:04
LazyGenerator
LazyGenerator.from 1..10 where { it < 3 } transform { it * idx++ } using idx:0 each {
println "Transformed Range $it"
}
LazyGenerator.from x:1..5, y:2..5 where { ( x + y ) % ( x + 2 ) == 0 } eachWithIndex { match, idx ->
println "$idx) $match"
}
LazyGenerator.from 1..10 each {
println "Range: $it"