Skip to content

Instantly share code, notes, and snippets.

import java.awt.*
import javax.swing.*
if (args.length < 1) {
println "Usage: Breathe minutes"
System.exit(0)
}
minutes = args[0] as int
dim = Toolkit.getDefaultToolkit().getScreenSize();
@thorntonrose
thorntonrose / FibTune.groovy
Last active March 27, 2016 00:10
FibTune - Generate tunes using Fibonacci (Groovy)
if (args.length < 1) {
println("Usage: groovy FibTune.groovy words")
return
}
fibMap = [
'I':'1', 'J':'1', 'U':'1', 'V':'1', 'Y':'1',
'H':'3', 'L':'3', 'N':'3', 'R':'3', 'X':'3',
'A':'4', 'E':'4', 'O':'4', 'Q':'4',
'B':'5', 'C':'5', 'F':'5', 'G':'5', 'S':'5', 'Z':'5',
@thorntonrose
thorntonrose / FibTune.html
Created March 27, 2016 00:11
FibTune - Generate tunes using Fibonacci (HTML+JavaScript)
<html>
<head>
<title>FibTune</title>
</head>
<body>
<h1>FibTune</h1>
<p>This program creates a tune using the
<a href="http://www.flutetree.com/playing/fibonacci.html" target="_blank">Fibonacci Technique</a>
and renders it in NAFTracks Six Hole Font (if installed), in Flutetree.com Songbook
@thorntonrose
thorntonrose / Pairs.groovy
Last active April 20, 2018 13:06
Find pairs of numbers in an array with a given sum
def findPairs1(nums, limit) {
print "$nums, $limit => "
def pairs = []
nums.eachWithIndex { n1, i1 ->
nums.eachWithIndex { n2, i2 ->
if (i1 != i2 && (n1 + n2) == limit) {
pairs << [ n1, n2 ].sort()
}
}
// See: https://en.wikipedia.org/wiki/Happy_number
(1 .. 100).each { n ->
println "$n:"
def sums = []
while (true) {
def squares = (n as String).collect { (it as int) ** 2 }
n = squares.sum()
println " $squares => $n" + (n == 1 ? " (happy)" : "")
@thorntonrose
thorntonrose / MailPing.java
Last active March 27, 2016 01:03
Send simple message via SMTP as "ping"
import java.io.*;
import java.net.*;
import java.util.*;
/**
* MailPing is used to test communication with a mail server.
*
* @author Thornton Rose
* @version 1.1
*/
@thorntonrose
thorntonrose / SmtpMailer.java
Last active March 27, 2016 01:03
Send mail via SMTP
import java.io.*;
import java.net.*;
import java.util.*;
import java.text.*;
/**
* SmtpMailer is used to send mail using SMTP.
*
* @author Thornton Rose
* @version 1.0
@thorntonrose
thorntonrose / CsvDoc.groovy
Created March 28, 2016 19:55
Class that represents a CSV (comma separated values) document
class CsvDoc {
def doc = []
def setHeader(titles) {
doc = [ toCsv(titles) ] + doc
}
def addRow(values) {
doc << toCsv(values)
}
@thorntonrose
thorntonrose / Dates.groovy
Created March 28, 2016 19:57
Date utility methods
import java.text.*
class Dates {
static final DATE_TIME = "yyyy-MM-dd HH:mm:ss"
static final EUROPEAN = "dd/MM/yyyy"
static final HSQLDB = "yyyy-MM-dd HH:mm:ss.SSS"
static final ISO = "yyyy-MM-dd'T'HH:mm:ss.SSS"
static final YEAR_MONTH_DAY = "yyyy-MM-dd"
static final YEAR_MONTH_DAY_HOUR_MINUTE = "yyyy-MM-dd (HH:mm)"
static final MONTH_DAY_YEAR = "MM/dd/yyyy"
@thorntonrose
thorntonrose / RestClient.groovy
Last active August 12, 2022 08:45
Simple Groovy REST client
class RestClient {
def proxy = Proxy.NO_PROXY
def basicAuth
// ???: Bake this into request()?
static def encodePath(path) {
path.replaceAll(" ", "%20")
}
def getContent(String url) {