Skip to content

Instantly share code, notes, and snippets.

View timyates's full-sized avatar

Tim Yates timyates

View GitHub Profile
@timyates
timyates / tree.md
Created April 24, 2012 09:33
A two-line tree in Groovy

Two line Tree in Groovy


Update!

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.

@timyates
timyates / from.groovy
Created April 30, 2012 09:45
groovy-stream post
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 ]
@timyates
timyates / AppName.sublime-project
Created May 2, 2012 08:39
Barebones Grails Sublime Text 2 Project file
{
"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" },
@timyates
timyates / example.groovy
Created June 7, 2012 13:51
Using groovy-stream via @grab
@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()
@timyates
timyates / rdp.coffee
Created June 8, 2012 11:03 — forked from rhyolight/rdp.js
Ramer-Douglas-Peucker line filtering algorithm in JavaScript
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
@timyates
timyates / renderer.groovy
Created June 23, 2012 19:03
SwingBuilder rendering to background of the JScrollPane that the code is written in
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()
@timyates
timyates / tr.groovy
Created June 25, 2012 09:35
Adding a tr function to Groovy Lists
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 ]
}
}
}
@timyates
timyates / match.groovy
Created June 27, 2012 09:17
match/when implemented with Groovy's GEP-3
// 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
@timyates
timyates / charSequence.groovy
Created June 28, 2012 10:58
takeWhile/dropWhile in groovy 2.0
def str = 'Groovy is cool'
assert str.takeWhile { it != ' ' } == 'Groovy'
assert str.dropWhile { it != ' ' } == ' is cool'
@timyates
timyates / new.groovy
Created June 28, 2012 11:36
inject with default initialValue
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