Skip to content

Instantly share code, notes, and snippets.

Avatar
🧬
making R code more modular

Konrad Rudolph klmr

🧬
making R code more modular
View GitHub Profile
@klmr
klmr / solution.r
Created Dec 17, 2020
Advent of Code day 15 in R
View solution.r
test_cases_desc = R'(
Given the starting numbers 1,3,2, the 2020th number spoken is 1.
Given the starting numbers 2,1,3, the 2020th number spoken is 10.
Given the starting numbers 1,2,3, the 2020th number spoken is 27.
Given the starting numbers 2,3,1, the 2020th number spoken is 78.
Given the starting numbers 3,2,1, the 2020th number spoken is 438.
Given the starting numbers 3,1,2, the 2020th number spoken is 1836.
)'
`%>%` = magrittr::`%>%`
View pathfail.py
import pathlib
p = pathlib.Path('/foo')
print(f'p: "{p}", parent: "{p.parent}", root: "{p.root}"')
# p: "/foo", parent: "/", root: "/"
print(f'parent == root: {p.parent == p.root}')
# parent == root: False
print(f'parent.samefile(root): {p.parent.samefile(p.root)}')
# parent.samefile(root): True
We couldn’t find that file to show.
@klmr
klmr / simple.r
Last active Apr 22, 2020
Generate N random numbers fulfilling some criteria; see https://stackoverflow.com/q/61352556/1968
View simple.r
alpha = numeric(1e4L)
beta = numeric(1e4L)
i = 0L
while (i < 1e4L) {
a = rnorm(1L, 10, 2)
b = rgamma(1L, 8, 1)
d = a - b
if (d < 1) {
i = i + 1L
alpha[i] = a
@klmr
klmr / rng.cpp
Last active Nov 3, 2020
Correctly seed an arbitrary RNG in C++
View rng.cpp
#include <algorithm>
#include <array>
#include <functional>
#include <random>
template <typename T = std::mt19937>
auto get_random_generator() -> T {
auto constexpr seed_bytes = sizeof(typename T::result_type) * T::state_size;
auto constexpr seed_len = seed_bytes / sizeof(std::seed_seq::result_type);
auto seed = std::array<std::seed_seq::result_type, seed_len>();
@klmr
klmr / read_file.cpp
Last active Apr 7, 2021
“Canonical” code to slurp a file in C++17
View read_file.cpp
auto read_file(std::string_view path) -> std::string {
constexpr auto read_size = std::size_t{4096};
auto stream = std::ifstream{path.data()};
stream.exceptions(std::ios_base::badbit);
auto out = std::string{};
auto buf = std::string(read_size, '\0');
while (stream.read(& buf[0], read_size)) {
out.append(buf, 0, stream.gcount());
}
@klmr
klmr / relatedness.r
Created Nov 12, 2019
Average number of shared alleles between two siblings
View relatedness.r
n_genes = 20000
n = 100
relatedness = replicate(n, {
child1 = sample(c('mother', 'father'), n_genes, replace = TRUE)
child2 = sample(c('mother', 'father'), n_genes, replace = TRUE)
mean(child1 == child2)
})
mean(relatedness)
@klmr
klmr / freq_table.cpp
Created Oct 23, 2019
Frequency table and mode calculation
View freq_table.cpp
#include <algorithm>
#include <initializer_list>
#include <iostream>
#include <iterator>
#include <string_view>
#include <type_traits>
#include <unordered_map>
namespace stats {
@klmr
klmr / errors.r
Last active Aug 30, 2019
Simple error handling with subclasses in R. Code is a direct port of Python code from https://codereview.stackexchange.com/q/225419/308
View errors.r
os_error = function (message, call = NULL) {
class = c('os_error', 'error', 'condition')
structure(list(message = message, call = call), class = class)
}
value_error = function (message, call = NULL) {
class = c('value_error', 'error', 'condition')
structure(list(message = message, call = call), class = class)
}
@klmr
klmr / fib.py
Last active Nov 21, 2020
Efficient, tail recursive fibonacci implementation for R and Python (but since they doesn’t do TCO it’s still using O(n) space)
View fib.py
def fib(n: int) -> int:
def fib(n, a, b):
if n == 0: return a
if n == 1: return b
return fib(n - 1, b, a + b)
return fib(n, 0, 1)