Skip to content

Instantly share code, notes, and snippets.

View klmr's full-sized avatar
📦
Making your R code easier to reuse

Konrad Rudolph klmr

📦
Making your R code easier to reuse
View GitHub Profile
@klmr
klmr / curry.md
Last active February 14, 2017 14:57
Currying explained

In the following, let’s define sum as

sum = function (a, b, c) a + b +c

Because base::sum’s definition involves ..., which would make the following explanation unnecessarily complex.


@klmr
klmr / generator.md
Last active August 28, 2022 02:26
Python-like generators in R

A little experiment using restarts.

(And while we’re at it, let’s torture R’s syntax a little.)

![screenshot][]

In the following we will be using R’s “restarts” feature to implement the state machine that drives generators in languages such as Python. Generators allow lazily generating values on demand: a consumer invokes a generator, and consumes values as they are produced. A new value is only produced once the previous one has been consumed.

@klmr
klmr / table.md
Last active August 4, 2016 10:49
Count dates, disregarding NULLs

Your test data:

text = 'created_date  start_date
2014-12-11    2014-12-10
2014-12-11 2014-12-11
@klmr
klmr / s4-vs-s3.md
Last active June 16, 2019 17:59
A simple side-by-side comparison of S4 with S3 in R

S4

setClass('Seq', representation(data = 'character', id = 'character'),
         validity = function (object) is_dna(object@data))

print_fasta = function (object) {
@klmr
klmr / pipe-function.r
Created June 30, 2016 08:31
Using pipes with higher-order functions
# Higher order function that returns a function:
make_sum = function () {
# Some code here …
message('Requesting to calculate a sum')
sum
}
seq(1, 10) %>% sum
# Now, logically, what does this do?
@klmr
klmr / header.hpp
Created June 22, 2016 09:34
Difference between static and inline functions in C++
inline int f() {
static int x = 0;
return ++x;
}
static int g() {
static int x = 0;
return ++x;
}
@klmr
klmr / deploy.sh
Last active June 9, 2016 19:54
Deploy a generated GitHub page (`_site` subfolder)
#!/usr/bin/env bash
# Configuration
# The remote target branch name needs to be
# 1. DIFFERENT from the local development branch
# 2. Set as the default branch name on GitHub
remote_target_branch=master
generated_contents_dir=_site
get-working-branch() {
@klmr
klmr / Makefile
Last active February 12, 2025 13:08
Self-documenting makefiles
# Example makefile with some dummy rules
.PHONY: all
## Make ALL the things; this includes: building the target, testing it, and
## deploying to server.
all: test deploy
.PHONY: build
# No documentation; target will be omitted from help display
build:
@klmr
klmr / printmakevars
Last active October 20, 2016 12:43
A helper script to print make variables
#!/usr/bin/env bash
# Inspired by <http://blog.melski.net/2010/11/30/makefile-hacks-print-the-value-of-any-variable/>
usage() {
echo >&2 "Usage: $0 [-f makefile] variables..."
echo >&2
echo >&2 "Optional arguments:"
echo >&2 " -f makefile: path to the makefile"
echo >&2
@klmr
klmr / list_comprehension.r
Created February 12, 2016 15:17
Haskell-like list comprehension for R
# Dummy object, only required for name resolution.
set = structure(list(), class = 'set')
print.set = function (x, ...) invisible(x)
`[.set` = function (set, expr, filter) {
expr = substitute(expr)
filter = substitute(filter)
stopifnot(identical(expr[[1]], quote(`<-`)))
stopifnot(identical(expr[[2]][[1]], quote(`|`)))