This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 ] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 ] } ), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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(-) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def list = 1..10 | |
def gridList = list.collate( 3 ).collate( 2 ) | |
assert gridList == [ [ [1, 2, 3], [4, 5, 6] ], | |
[ [7, 8, 9], [10] ] ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String input = 'Groovy Base64 Encode' | |
String encoded = input.bytes.encodeBase64() | |
assert encoded == 'R3Jvb3Z5IEJhc2U2NCBFbmNvZGU=' | |
byte[] decoded = encoded.decodeBase64() | |
assert input == new String( decoded ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def val = 1 | |
def a = [ hasNext:{ true }, | |
next:{ val++ } ] as Iterator | |
assert a.take( 2 ).collect() == [ 1, 2 ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |