Skip to content

Instantly share code, notes, and snippets.

Avatar
🎯
Focusing

Maarten Demeyer mpjdem

🎯
Focusing
View GitHub Profile
@mpjdem
mpjdem / game_of_life.R
Last active Jan 7, 2020
Conway's game of life in R, visualised in the terminal
View game_of_life.R
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 Dec 27, 2019
Example of global RNG state
View random_seeds_indeed.R
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 Nov 25, 2019
test of dtplyr speed
View dtplyr_test.R
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 Nov 12, 2019
R script to print out an emoji
View getemo.R
#!/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 Sep 30, 2019
Matrix crossdistances
View crossdistance.R
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");
View keybase.md

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 Sep 23, 2019
All pairwise Euclidean distances between two matrices in R, using Rcpp
View crossdist_example.R
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 Sep 18, 2019
'State' design pattern in R
View dp_state.R
# General functions
atom <- function(obj) {
atom <- new.env()
atom$obj <- obj
atom
}
deref <- function(atom) {
atom$obj
}
@mpjdem
mpjdem / dp_state.clj
Created Sep 18, 2019
'State' design pattern in Clojure
View dp_state.clj
(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)))
@mpjdem
mpjdem / dp_strategy.R
Last active Sep 3, 2019
'Strategy' design pattern in R
View dp_strategy.R
qsort <- function(inp, cfun) {
if (length(inp) > 1) {
cres <- cfun(inp, inp[[length(inp)]])
c(qsort(inp[cres < 0], cfun), inp[cres == 0], qsort(inp[cres > 0], cfun))
} else {
inp
}
}
users <- list(list(name = "John Doe", subscription = 0),