Skip to content

Instantly share code, notes, and snippets.

@tomgullo
tomgullo / gist:630476
Created October 17, 2010 02:50
LinkedListsInGroovy.groovy
class Node {
def next
def data
}
def n3 = new Node(data:"three", next:null)
def n2 = new Node(data:"two", next:n3)
def n1 = new Node(data:"one", next:n2)
class MyList { def firstNode }
@tomgullo
tomgullo / send_email.groovy
Created January 19, 2012 14:32
send email in Java/Spring environment
//load jars
String jar_class_path = ?
def jardir = new File(jar_class_path)
def jars = jardir.listFiles().findAll { it.name.endsWith('.jar') }
jars.each { this.class.classLoader.rootLoader.addUrl( it.toURI().toURL() ) }
def props = new Properties()
props.putAll( ['mail.smtp.host':'localhost', 'mail.smtp.timeout':20000', 'mail.smtp.connectiontimeout':'20000'])
//wire up Spring
@tomgullo
tomgullo / SimpleValidation.groovy
Created February 9, 2012 15:47
Simple validation method
//simple validation method in Groovy
def validateIt(Map params, List requiredList, Map requiredSizeMap) {
def message = ''
for (e in params) {
if (e.key in requiredList) {
if (!e.value) { message += "${e.key} is required\n" }
}
if (e.key in requiredSizeMap.keySet()) {
def validSize = requiredSizeMap[e.key]
if (e.value.size() > validSize) { message += "${e.key} cannot be more than ${validSize} characters\n" }
@tomgullo
tomgullo / test_mapreduce_mongodb.js
Created February 15, 2012 14:42
aggregate count using map reduce in MongoDB
db.things.insert({name:'joe', state:'va'})
db.things.insert({name:'jane', state:'va'})
db.things.insert({name:'sally', state:'md'})
var m = function() { emit( this.state, {count:1}); }
var r = function() (key, values) {
var result = { count: 0 }
values.forEach(function(value) { result.count++ } );
return result;
}
@tomgullo
tomgullo / load_mongodb.js
Created February 15, 2012 15:45
Load MongoDB with test data
function randomOne(from, to) {
return Math.floor(Math.random() * (to - from + 1) + from);
}
var name = ['tom', 'jeff', 'carol', 'mike', 'robert', 'suzan', 'wendy']
var st = ['va', 'md', 'ny', 'ca', 'fl', 'oh', 'mi'];
var food = ['pizza', 'hamburger', 'spaghetti', 'burrito', 'seafood', 'indian', 'thai']
var quote = ['abc abc abc abc....', 'cba cba cba cba....', 'xyz xyz xyz...',
'jio jio jio....', 'oww oww oww...', 'fow fow fow...', 'soi soi soi...']
@tomgullo
tomgullo / load.rb
Created February 19, 2012 16:24
load zips into mongodb using ruby
require 'rubygems'
require 'mongo'
@conn = Mongo::Connection.new
@db = @conn['geo-db']
@coll = @db['places']
File.open("US.txt", "r").each do | line |
sp = line.split("\t")
#country zip state region lat long
@tomgullo
tomgullo / convert_roman_to_dec.groovy
Created March 5, 2012 14:08
Groovy script to convert decimals to roman numerals
/*
The key is to map unique roman numerals to binary values, then start from traversing through the list from high values to low values.
When binary is greater, start building the roman string, subtracting from the binary as you go.
Takeaway: If writing a conversion problem, make list mapping one to another. Also, look for "shortcuts" or patterns to avoid full lists mapping each value.
*/
def binaryToRoman(int binary) {
def RCODE = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"]
@tomgullo
tomgullo / passing_args_and_callbacks.js
Created March 16, 2012 14:24
Simple example of callbacks
var App = {
something: function(args1, callback) {
console.log("in something 2");
callback.apply(App, arguments);
return false;
},
dothis: function() {
var l = ['one','two'];
var s = "hi";
App.something(s, function(args1) {
@tomgullo
tomgullo / example_using_qunit.html
Created March 20, 2012 18:49
Example testing js using qunit
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>QUnit Tests</title>
<link rel="stylesheet" href="qunit.css">
</head>
<body>
<h1 id="qunit-header">QUnit Hello World</h1>
<h2 id="qunit-banner"></h2>
@tomgullo
tomgullo / underscore_template.html
Created March 26, 2012 15:12
underscore template example
<html>
<head>
<script type="text/javascript" src="jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="underscore-min.js"></script>
<script>
$(document).ready(function() {
var people_records = {people:[{first_name:'joe', last_name:'shmo'}, {first_name:'jane', last_name:'smith'}]};
var row_template = "<% _.each(people, function(obj) { %> <tr> <td> <%= obj.first_name %> </td> <td> <%= obj.last_name %> </td> </tr> <% }); %>";
$("#table_rows").html( _.template( row_template, people_records ) );