Skip to content

Instantly share code, notes, and snippets.

View mpjdem's full-sized avatar

Maarten Demeyer mpjdem

View GitHub Profile
@mpjdem
mpjdem / renv.lock
Created August 6, 2021 12:31
example renv file for dockerfiler
{
"R": {
"Version": "4.1.0",
"Repositories": [
{
"Name": "CRAN",
"URL": "https://cran.rstudio.com"
}
]
},
@mpjdem
mpjdem / game_of_life.R
Last active October 7, 2021 16:08
Conway's game of life in R, visualised in the terminal
library(data.table)
library(keypress)
## Create the universe. Well, sort of.
dims <- c(49, 49)
universe <- CJ(x = seq(dims[1]), y = seq(dims[2]), k = 1, cell = FALSE)
universe[, cell := sample(c(FALSE, TRUE), prod(dims), TRUE)]
## Neighbourhood to consider for each cell
neighbours <- CJ(xd = -1:1, yd = -1:1, k = 1)[xd != 0 | yd != 0]
@mpjdem
mpjdem / random_seeds_indeed.R
Created December 27, 2019 11:33
Example of global RNG state
pick_name <- function() {
names <- c("Dax", "Wug", "Lep", "Sik", "Bop")
sample(names)[1]
}
set.seed(100)
print(pick_name()) # Wug
print(runif(1)) # 0.4837707
pick_name <- function() {
@mpjdem
mpjdem / dtplyr_test.R
Created November 25, 2019 12:34
test of dtplyr speed
library(dplyr)
library(data.table)
library(dtplyr)
df <- data.frame(customer = sample(100000, size = 5000000, replace = TRUE),
value = runif(5000000))
# dplyr
t0 <- Sys.time()
res_dp <- df %>%
@mpjdem
mpjdem / getemo.R
Last active November 12, 2019 22:58
R script to print out an emoji
#!/usr/bin/Rscript --vanilla
# To install {emo}, do: remotes::install_github("hadley/emo")
# commandArgs() parses arguments provided on the command line into a <list>
args <- commandArgs(trailingOnly = TRUE)
# Use cat() for output
cat(emo::ji(args[[1]]), "\n")
@mpjdem
mpjdem / crossdistance.R
Created September 30, 2019 07:39
Matrix crossdistances
library(Rcpp)
cppFunction('NumericMatrix crossdist(NumericMatrix m1, NumericMatrix m2) {
int nrow1 = m1.nrow();
int nrow2 = m2.nrow();
int ncol = m1.ncol();
if (ncol != m2.ncol()) {
throw std::runtime_error("Incompatible number of dimensions");

Keybase proof

I hereby claim:

  • I am mpjdem on github.
  • I am mpjdem (https://keybase.io/mpjdem) on keybase.
  • I have a public key whose fingerprint is DAE3 6756 D415 3A94 9C20 4250 F3D4 70E9 DB34 7CCF

To claim this, I am signing this object:

@mpjdem
mpjdem / crossdist_example.R
Last active September 23, 2019 11:53
All pairwise Euclidean distances between two matrices in R, using Rcpp
library(Rcpp)
cppFunction('NumericMatrix crossdist(NumericMatrix m1, NumericMatrix m2) {
int nrow1 = m1.nrow();
int nrow2 = m2.nrow();
int ncol = m1.ncol();
if (ncol != m2.ncol()) {
throw std::runtime_error("Incompatible number of dimensions");
@mpjdem
mpjdem / dp_state.R
Created September 18, 2019 21:22
'State' design pattern in R
# General functions
atom <- function(obj) {
atom <- new.env()
atom$obj <- obj
atom
}
deref <- function(atom) {
atom$obj
}
@mpjdem
mpjdem / dp_state.clj
Created September 18, 2019 21:19
'State' design pattern in Clojure
(def user (atom {:name "John Doe"
:user-state :not-subscribed}))
(println user) ;; print the memory address
(println (deref user)) ;; dereference the memory address, and print the value
(println @user) ;; short-hand form of dereferencing
(defn subscribe [user]
(when (= :not-subscribed (:user-state @user))
(swap! user assoc :user-state :subscribed)))