Skip to content

Instantly share code, notes, and snippets.

View EwanDawson's full-sized avatar

Ewan Dawson EwanDawson

  • Ping Identity
  • Edinburgh, UK
View GitHub Profile
payload.collate(2).collectEntries { [it[0], it[1]] }
invoke('https://gist.github.com/EwanDawson/67902abfb90239b502a255fcff0f69ca/raw/21037fea2ac819d92663dd2b8f6b80ac21512f32/multiply.groovy', [payload[0], 2])
payload[0] * payload[1]
{
"nonce": "0x0000000000000042",
"difficulty": "0x40000",
"alloc": {
"bbbbbaaaaa82db86a35502193b4c6ee9a76ebe8f": {
"balance": "10015200000000000000000"
}
},
@EwanDawson
EwanDawson / days_in_year.R
Created May 14, 2013 08:55
#R script to get the number of days in a year. Given a vector containing years, returns a vector of the same length with the number of days in each year. Requires #zoo Example: > diy(2000:2004) [1] 366 365 365 365 366
diy <- function(year) {
require(zoo)
as.numeric(as.Date(as.yearmon(year) + 1) - as.Date(as.yearmon(year)))
}
@GrabResolver(name='grails-core', root='http://repo.grails.org/grails/core')
@Grab(group='org.grails', module='grails-datastore-gorm-mongo', version='1.0.0.BUILD-SNAPSHOT')
@Grab(group='org.slf4j', module='slf4j-simple', version='1.6.1')
import grails.persistence.*
import org.grails.datastore.gorm.mongo.config.*
MongoDatastoreConfigurer.configure("myDatabase", Book)
Book.withSession {
@EwanDawson
EwanDawson / asType.groovy
Created October 10, 2012 11:03
Extend Groovy asType(Class) method to handle additional classes
import org.joda.time.DateTime
def oldAsType = String.metaClass.getMetaMethod("asType", [Class] as Class[])
String.metaClass.asType = { Class type ->
type.isAssignableFrom(DateTime) ?
new DateTime(delegate) :
oldAsType.invoke(delegate, [type] as Class[])
}
assert "2012-01-01T10:10:10" as DateTime == new DateTime("2012-01-01T10:10:10")
assert "1234" as Integer == 1234
@GrabResolver(name='javahg', root='https://oss.sonatype.org/content/repositories/snapshots/')
@Grab(group='com.aragost.javahg', module='javahg', version='0.3-SNAPSHOT')
import com.aragost.javahg.*
import com.aragost.javahg.commands.*
config = new RepositoryConfiguration()
config.hgrcPath = null // forces Mercurial to look in the usual places for .hgrc
rep = Repository.open(config, new File('.')) // assume the current directory is a repo
// Print the status of the repo
@EwanDawson
EwanDawson / regex-capture.groovy
Created April 17, 2012 16:09
Idiomatic regex group capturing in Groovy
// Using Matcher object returned by =~ operator
matcher = "Hello world v1.01" =~ /.* v(\S*)/
if (matcher.matches()) version = matcher[0][1]
assert version == "1.01"
// We can make this a little tidier using the 'with' method
version = ("Hello world v1.01" =~ /.* v(\S*)/).with { matches() ? it[0][1] : null }
assert version == "1.01"
@EwanDawson
EwanDawson / gist:1629928
Created January 18, 2012 00:09
Debian: find all the files of a particular package that have been modified since the package was installed
# Actually, lists the installed files in the order they were updated, most recent first.
# All the unmodified files will have the same last modified date, so it should be pretty
# easy to spot where the updated files begin.
PACKAGE=jetty # Replace with your package
ls -lt $(ls -Fd1 $(dpkg -L $PACKAGE) | grep -v "[\/\@]$")