Skip to content

Instantly share code, notes, and snippets.

View jonocarroll's full-sized avatar
👨‍💻
Learning all the languages

Jonathan Carroll jonocarroll

👨‍💻
Learning all the languages
View GitHub Profile
@jonocarroll
jonocarroll / count_operations.R
Created April 16, 2025 14:40
count_operations from Stephanov's Efficient Programming with Components, implemented (crudely) in R
## global counters
reset <- function() {
assign("counts_lt", 0, .GlobalEnv)
assign("counts_gt", 0, .GlobalEnv)
assign("counts_eq", 0, .GlobalEnv)
assign("counts_as", 0, .GlobalEnv)
}
reset()
@jonocarroll
jonocarroll / particles.jl
Created January 9, 2025 02:09
Animation of colliding particles based on 'particles' (Chapter 2 of Paul Smaldino’s book, Modeling Social Behavior)
using Distances
using Plots
function ninradius(indiv, radius, distances)
findall(distances[indiv, :] .< radius)
end
function getdists(particlelist)
pairwise(Euclidean(), reduce(hcat, [[p.x, p.y] for p in particlelist]), dims=2)
end
@jonocarroll
jonocarroll / py_mutable_args.py
Created December 16, 2024 22:44
Example of python's mutable arguments being mutated
>>> def make_a_list(val1 = 0, val2 = 0, lst = []):
... lst.append(val1)
... lst.append(val2)
... return lst
>>> # you need to know that you're modifying base_list
>>> base_list = [1, 2, 3, 4]
>>> extend_list = make_a_list(5, 6, base_list)
>>> print(extend_list)
[1, 2, 3, 4, 5, 6]
@jonocarroll
jonocarroll / cumsum_cut.R
Last active October 31, 2024 16:50
cumsum with reset points
partition <- function(x, cut) {
v <- rep(0, length(x))
v[cut] <- 1
cumsum(v)
}
cumsum_cut <- function(x, cut = NULL) {
cut <- sort(cut)
vs <- split(x, partition(x, cut))
vs <- sapply(vs, \(x) {x[1] <- 0; x})
@jonocarroll
jonocarroll / in_place.R
Created September 26, 2024 22:48
A new vector class that enables a function subsetting assignment method x[f] <- value
`[.vec` <- function(x, i, ...) {
UseMethod("[.vec", i)
}
`[<-.vec` <- function(x, i, value, ...) {
UseMethod("[<-.vec", i)
}
`[.vec.default` <- function(x, i, ...) {
class(x) <- setdiff(class(x), "vec")
@jonocarroll
jonocarroll / donut.R
Last active September 17, 2024 09:16
ts=0.07;ps=0.02;A=B=0;
while(1){ j=i=0;z=rep(0,1760);
b=rep(' ',1760); while(j < 6.28){
j=j+ts;i=0;while(i<6.28){i=i+ps;c=
sin(i);l=cos(i); d=cos(j);f=sin(j);
e=sin(A);g=cos(A );h=d+2;D=1/(c*h*e+
f*g+5);m=cos(B) ;n=sin(B);t=c*h*g-
f*e;x=as.integer (40+30*D*(l*h*m-t*n));
y=as.integer(12+ 15*D*(l*h*n+t*m));
o=as.integer(x+( 80*y));N=as.integer(
enum <- function(...) {
vals <- if (is.null(...names())) {
setNames(seq_len(...length()), c(...))
} else {
c(...)
}
out <- new.env(parent = emptyenv())
list2env(as.list(vals), out)
lapply(names(vals), lockBinding, out)
lockEnvironment(out)
@jonocarroll
jonocarroll / fuzzygroup.R
Created March 13, 2024 05:24
Perform fuzzy grouping on medical terms
## Medical Term Fuzzy Grouping
## J. Carroll 2024
##
## Uses the {zoomerjoin} package: https://github.com/beniaminogreen/zoomerjoin
## read in a set of medical terms, lowercased
terms <- tolower(readLines("https://raw.githubusercontent.com/socd06/medical-nlp/master/data/vocab.txt"))
## example data with typos and inserted words
gi <- c("gastrointestinal disorders", "gastrointestinal tract disorders", "gastreinstestinal disorder")
@jonocarroll
jonocarroll / check_errors.sh
Last active November 20, 2023 07:22
Use crontab to monitor the failure of a script
#!/bin/bash
LOG_FILE="/Users/username/output.log"
NOTIFIED_FILE="/Users/username/notified.txt"
EMAIL="username@example.com"
# Read the contents of the notified file, if it exists
notified=$(cat "$NOTIFIED_FILE" 2>/dev/null)
# If the notified file doesn't exist, assume no previous notification
@jonocarroll
jonocarroll / rowmin.rs
Created November 17, 2023 00:34
rextendr Rust/R function to calculate the rowMins of a matrix (no NA processing)
library(rextendr)
rust_function("
fn rowmin(m: RMatrix::<i32>) -> Vec<i32> {
// convert to an ndarray
let matrix = <ArrayView2<i32>>::from_robj(&m).unwrap();
// store results
let mut min_values = Vec::with_capacity(matrix.len());