Skip to content

Instantly share code, notes, and snippets.

View jlmelville's full-sized avatar

James Melville jlmelville

View GitHub Profile
@jlmelville
jlmelville / build.gradle
Created October 27, 2015 17:17
Workaround for gradle application plugin 'the input line is too long' error on Windows
tasks.withType(CreateStartScripts).each { task ->
task.doLast {
String text = task.windowsScript.text
text = text.replaceFirst(/(set CLASSPATH=%APP_HOME%\\lib\\).*/, { "${it[1]}*" })
task.windowsScript.write text
}
}
@jlmelville
jlmelville / Encoding.java
Created October 31, 2015 21:23
Encoding-aware handling of text files in Java
try (InputStreamReader inputStreamReader = new InputStreamReader(
new FileInputStream(path.toString()), StandardCharsets.UTF_8)) {
// do stuff with the inputStreamReader rather than a File
}
}
byte[] bytes = string.getBytes(StandardCharsets.UTF_8);
String string = new String(bytes, StandardCharsets.UTF_8);
@jlmelville
jlmelville / killPollingThreads.groovy
Created October 31, 2015 21:27
Killing SCM polling threads in Jenkins
Thread.allStackTraces.keySet().each() {
if (it.name.contains("SCM polling")) {
println "Interrupting thread ${it.id} ${it.name}"
it.interrupt()
}
}
@jlmelville
jlmelville / DatabaseSmokeTest.groovy
Last active March 7, 2020 13:01
JDBC drivers and Groovy
// Oracle example
@GrabResolver(name='nexus', root='http://path/to/some/nexus/server')
@GrabConfig(systemClassLoader=true)
@Grab(group='oracle', module='ojdbc6', version='11.2.0.4')
import groovy.sql.Sql
def hostname = "hostname of Oracle DB here"
def port = "port of the server here"
def db = "service name here"
def url = "jdbc:oracle:thin:@//${hostname}:${port}/${db}"
@jlmelville
jlmelville / gradle-jobs-dsl.groovy
Created November 1, 2015 22:13
Manually creating a Gradle task with the Jenkins Jobs DSL
configure { project ->
project / builders / 'hudson.plugins.gradle.Gradle' {
description 'Builds and runs unit tests.'
tasks "clean test javadoc"
rootBuildScriptDir ''
fromRootBuildScriptDir true
useWrapper true
makeExecutable true
useWorkspaceAsHome false
}
@jlmelville
jlmelville / list_iterate.R
Last active December 30, 2015 04:50
Iterating over a list in R
# a simple function to create a list of lists:
list_of_lists <- function(...) {
list(...)
}
my_lol <- list_of_lists(list(cleesh = "krill"), list(nitfol = "rezrov"))
# want to print out "krill" and "rezrov"
# various suggestions for iterating over a list:
for (name in names(my_lol)) {
message(my_lol[[name]][[1]])
@jlmelville
jlmelville / kabsch.R
Last active January 6, 2022 20:32
The Kabsch algorithm in R for aligning one point set over another
#' Kabsch Algorithm
#'
#' Aligns two sets of points via rotations and translations.
#'
#' Given two sets of points, with one specified as the reference set,
#' the other set will be rotated so that the RMSD between the two is minimized.
#' The format of the matrix is that there should be one row for each of
#' n observations, and the number of columns, d, specifies the dimensionality
#' of the points. The point sets must be of equal size and with the same
#' ordering, i.e. point one of the second matrix is mapped to point one of
@jlmelville
jlmelville / dev-vignette.Rmd
Created April 16, 2017 05:26
Loading a development package for use in a vignette (e.g. in RStudio)
```{r setup, include=FALSE}
devtools::load_all("..")
knitr::opts_chunk$set(echo = TRUE, collapse = TRUE, comment = "#>")
```
@jlmelville
jlmelville / benchmark.R
Last active August 20, 2018 06:14
Benchmarking mize with funconstrain
# Rough code to run mize with various settings over all the test functions in
# the funconstrain package https://github.com/jlmelville/mize and
# https://github.com/jlmelville/funconstrain
# Example
# devtools::install_github("jlmelville/mize")
# devtools::install_github("jlmelville/funconstrain")
# library("mize")
# library("funconstrain")
@jlmelville
jlmelville / words.R
Created December 29, 2018 19:47
Find the longest word using the given vector of letters.
words_canon <- function(words) {
words$canon <- apply(words, 1,
function(x) {
paste(
stringr::str_sort(
unlist(
strsplit(
tolower(as.character(x)), split = "",
fixed = TRUE))),
collapse = "")