Skip to content

Instantly share code, notes, and snippets.

View nacnudus's full-sized avatar

Duncan Garmonsway nacnudus

View GitHub Profile
graph LR
    A[N: config<br/>N: state<br/>N: reality] -->|Nothing to do| B(N: config<br/>N: state<br/>N: reality)
    C[Y: config<br/>N: state<br/>N: reality] -->|Create| D(Y: config<br/>Y: state<br/>Y: reality)
    E[N: config<br/>Y: state<br/>N: reality] -->|Someone manually deleted it,<br/>and also removed it from the config.| F(?)
    G[N: config<br/>N: state<br/>Y: reality] -->|Ignore| H(N: config<br/>N: state<br/>Y: reality)
    I[Y: config<br/>Y: state<br/>N: reality] -->|Recreate?<br/>Someone manually deleted it.| D
    K[Y: config<br/>N: state<br/>Y: reality] -->|Error. You must import the existing resource into the state.| D
    M[N: config<br/>Y: state<br/>Y: reality] -->|Delete| N(N: config<br/>N: state<br/>N: reality)
 O[Y: configY: stateY: reality] --&gt;|Nothing to do| P(Y: configY: stateY: reality)
@nacnudus
nacnudus / as.list.hashtab.R
Created May 31, 2022 14:45
Inspect a nested hashtab as a list
# Inspect a nested hash table as a list
as.list.hashtab <- function(h) {
val <- vector("list", 0)
maphash(h, function(k, v) {
r <- if (is.hashtab(v)) {
as.list.hashtab(v)
}else {
v
}
val[[k]] <<- r
@nacnudus
nacnudus / dfs-igraph.R
Last active May 3, 2022 19:21
Depth-first search in igraph
library(igraph)
library(tidygraph)
# Create an igraph
# c - d
# - e
# - f - b
ig <- graph_from_literal(f-+b, c-+e:f, c-+d)
ig
@nacnudus
nacnudus / log.sh
Last active March 17, 2022 13:05
Simple reminder to log my current activity.
#!/bin/bash
ENTRY=$(/usr/bin/yad --title "Activity log" --text="What are you doing?" --entry --auto-kill)
DATETIME=$(date --iso-8601=seconds)
[[ ! -z "$ENTRY" ]] && echo -e "${DATETIME}\t${ENTRY}">> /home/nacnudus/gds/log.log
# Schedule in crontab Every six minutes between 9am and 6pm on weekdays
# */6 9-18 * * 1-5 env DISPLAY=:0 && /usr/bin/bash /path/to/this_file.sh
@nacnudus
nacnudus / emst.jl
Created March 14, 2022 21:37
Euclidean Minimum Spanning Tree in Julia using mlpack
# The fastest implementation available is in mlpack (C++)
using Pkg
Pkg.add("mlpack")
using mlpack
x = rand(5,2)
x_emst = mlpack.emst(x)
# A far slower implementation is in EMST.jl. As of 2022-03-14 my fork has been updated to run with Julia v0.7+using Pkg
using Pkg
Pkg.add(url="https://github.com/nacnudus/EMST.jl", rev="julia-v0.7")
@nacnudus
nacnudus / gmail_message_size_analysis.R
Created May 15, 2021 18:59
Script to analyse Gmail message sizes by from/to/subject, useful when you're running out of space in the free tier
# Script to analyse Gmail message sizes by from/to/subject
# 1. Download Gmail from Google Takeout, specifically the folders "Inbox",
# "Archived", "Sent", and "Bin".
# 2. Extract it into the working directory. It should create the folders
# Takeout/Mail
# 3. Run this script in the working directory
library(tidyverse)
library(tm.plugin.mail)
@nacnudus
nacnudus / parse-cancer-waiting-times-excel.R
Created November 25, 2020 18:40
Parse a UK government spreadsheet of cancer waiting times with R, tidyxl and unpivotr
library(tidyverse)
library(tidyxl)
library(unpivotr)
#' Convert relevant character values to dates
#'
#' @param cells Data frame derived from `tidyxl::xlsx_cells()` or
#' `readr::melt_csv()` or similar.
#' @param condition An expression that returns a logical value,
#' is defined in terms of the columns in `cells`. Similar to `dplyr::filter()`.
@nacnudus
nacnudus / as.list.environment.R
Last active May 3, 2020 19:22
Fastest way to count objects in an R environment
# Compare performance of ls() with as.list.environment() for counting objects
# length(as.list.environment()) is fastest
env <- environment(plot) # A handy, large environment
bench::mark(length(ls(env)), length(as.list(env)))
# # A tibble: 2 x 13
# expression min median `itr/sec` mem_alloc `gc/sec` n_itr n_gc total_time result memory time gc
# <bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl> <int> <dbl> <bch:tm> <list> <list> <list> <list>
# 1 length(ls(env)) 4.69ms 4.96ms 198. NA 0 99 0 501ms <int [1]> <NULL> <bch:tm> <tibble [99 × 3]>
@nacnudus
nacnudus / cursed_starwars_data.R
Created December 10, 2019 19:51 — forked from brooke-watson/cursed_starwars_data.R
cursed_data_challenge
# ---------------------------------------
# untidy data
# ---------------------------------------
# this dataset is a sample of the kind of data that might appear in the wild,
# particularly when dealing with government data,
# particularly when trying to convert an output table or individual report
# back into a raw data format that can be analyzed.
# in these test datasets, discrete observations are spread out across multiple rows.
library(tidyverse)
library(ompr)
library(ompr.roi)
library(ROI.plugin.glpk)
M <- 3 # Volunteers (rows)
N <- 4 # Jobs (combination of role at given time and location) (columnss)
# Jobs are:
# 1. Greet (8am-9am)