Skip to content

Instantly share code, notes, and snippets.

View jlmelville's full-sized avatar

James Melville jlmelville

View GitHub Profile
@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 / 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 / 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 = "")
@jlmelville
jlmelville / get_seed.R
Created January 19, 2019 02:16
generating a seed for use in Rcpp
# http://rorynolan.rbind.io/2018/05/08/rcsetseed/
get_seed <- function() {
sample.int(.Machine$integer.max, 1)
}
{"lastUpload":"2019-08-18T16:13:32.193Z","extensionVersion":"v3.4.1"}
@jlmelville
jlmelville / vmwareplayer.sh
Created February 10, 2020 01:08
VMware player setup for Ubuntu-like
# Get copy and paste working
sudo apt-get install open-vm-tools-desktop
# Activate shared directory: name and define it (e.g. vmshare) in the VM config
# On the guest
# mkdir -p /mnt/hgfs if necessary
sudo vmhgfs-fuse .host:/vmshare /mnt/hgfs/ -o allow_other -o uid=1000 -o nonempty
# in /etc/fstab for auto mount
.host:/vmshare /mnt/hgfs/vmshare fuse.vmhgfs-fuse defaults,allow_other,uid=1000 0 0