Wow... Kiyotaka Oku's fork of this shows how to do it in one line :-)
The other day, I saw Harold Cooper's One-line tree in Python via autovivication, and wondered if the same thing was possible in Groovy.
Wow... Kiyotaka Oku's fork of this shows how to do it in one line :-)
The other day, I saw Harold Cooper's One-line tree in Python via autovivication, and wondered if the same thing was possible in Groovy.
| import groovy.stream.Stream | |
| // Repeat an object indefinitely | |
| Stream s = Stream.from { 1 } | |
| assert s.take( 5 ).collect() == [ 1, 1, 1, 1, 1 ] | |
| // Use an Iterable | |
| Stream s = Stream.from 1..3 | |
| assert s.collect() == [ 1, 2, 3 ] |
| { | |
| "folders": | |
| [ | |
| { "path":"grails-app/conf", "name":"Configuration" }, | |
| { "path":"grails-app/controllers", "name":"Controllers" }, | |
| { "path":"grails-app/domain", "name":"Domain Objects" }, | |
| { "path":"grails-app/i18n", "name":"Internationalization" }, | |
| { "path":"grails-app/services", "name":"Services" }, | |
| { "path":"grails-app/taglib", "name":"Tag Libraries" }, | |
| { "path":"grails-app/utils", "name":"Utils" }, |
| @GrabResolver( name='bloidonia', root='https://raw.github.com/timyates/bloidonia-repo/master') | |
| @Grab('com.bloidonia:groovy-stream:0.2') | |
| import groovy.stream.Stream | |
| def s = Stream.from 1..4 | |
| assert [1,2,3,4] == s.collect() |
| findPerpendicularDistance = ( point, line ) -> | |
| [pointX,pointY] = point[0..1] | |
| lineStart = x: line[0][0], y: line[0][1] | |
| lineEnd = x: line[1][0], y: line[1][1] | |
| slope = ( lineEnd.y - lineStart.y ) / ( lineEnd.x - lineStart.x ) | |
| intercept = lineStart.y - ( slope * lineStart.x ) | |
| Math.abs( slope * pointX - pointY + intercept ) / Math.sqrt( Math.pow( slope, 2) + 1) | |
| douglasPeucker = ( points, epsilon ) -> | |
| maxIndex = maxDistance = 0 |
| import groovy.swing.* | |
| import java.awt.* | |
| import javax.swing.* | |
| import javax.swing.event.* | |
| import org.codehaus.groovy.control.customizers.ImportCustomizer | |
| import org.codehaus.groovy.control.CompilerConfiguration | |
| String script = '''color = Color.RED | |
| |fillRect 10, 10, 100, 100'''.stripMargin() |
| def tr = { inputs, outputs -> | |
| (inputs,outputs) = [inputs,outputs].collect { it instanceof List ? it : [ it ] } | |
| assert inputs.size() == outputs.size(), 'tr: Inputs and outputs must be the same size' | |
| delegate.collect { item -> | |
| inputs.indexOf( item ).with { idx -> | |
| idx == -1 ? item : outputs[ idx ] | |
| } | |
| } | |
| } |
| // No commas | |
| def a = 'tim' | |
| def nocom = match( a ) { | |
| when 'dave' 'Hi Dave' | |
| when 'tim' 'Hi Tim' | |
| otherwise 'none of the above' | |
| } | |
| assert nocom == 'Hi Tim' | |
| // Commas |
| def str = 'Groovy is cool' | |
| assert str.takeWhile { it != ' ' } == 'Groovy' | |
| assert str.dropWhile { it != ' ' } == ' is cool' |
| def nums = [ 10, 20, 30 ] | |
| // First element of nums (10) is used as default value | |
| def product = nums.inject { acc, num -> acc * num } | |
| assert product == 6000 |