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 / gist:5098252
Created March 6, 2013 10:04
Terser code to convert a byte code to a string.
Dim bytes As New Byte() { CByte(131) }
Dim str As String = Encoding.GetEncoding(1252).GetString(bytes)
' Or, much simpler:
Dim str As String = ChrW(&H0192).ToString()
@klmr
klmr / gist:5003412
Last active December 14, 2015 01:08
A note on line-breaks and indentation in VB to maximise readability:
' Single-line lambdas:
_myService.MyMethod(userId, profileId,
Sub(message As EventArgs) _eventAggregator.SendMessage(message))
' Multi-line lambdas: align `Sub` and `End Sub`:
_myService.MyMethod(userId, profileId, Sub(message As EventArgs)
_eventAggregator.SendMessage(message)
_localVariable = e.Result
#!/usr/bin/env bash
source ~/.config/extpaths # For samtools
set -e
set -u
n=0
for bam in $*; do
@klmr
klmr / foo.cpp
Created February 19, 2013 11:55
Ternary
result = x == "foo" ? foo() :
x == "bar" ? bar() :
x == "baz" ? baz() :
x == "qux" ? qux();
@klmr
klmr / gist:4960666
Created February 15, 2013 14:23
Read-group extraction without temporary buffer
while (getline(in, line)) {
if (line.compare(0, 4, "@RG\t") != 0) continue;
size_t start = line.find("\tID:");
if (start == string::npos) continue;
start += 4;
size_t end = line.find("\t", start);
if (end == string::npos or end == start) continue;
gids.emplace(string.begin() + start, string.begin() + end);
// Or, in C++03:
//gids.insert(line.substr(start, end - start));
@klmr
klmr / count-nuc.sh
Last active December 10, 2015 18:38
Count nucleotides in a sequence (from file or standard input).
#!/usr/bin/env bash
set -e
set -u
input="${1-/dev/stdin}"
declare -A counts=(
[A]=0
[C]=0
@klmr
klmr / gist:4250731
Created December 10, 2012 14:04
C++11 enum classes boilerplate
enum class dna_strand : char {
positive = '+',
negative = '-'
};
std::ostream& operator <<(std::ostream& out, dna_strand strand) {
return out << static_cast<char>(strand);
}
std::istream& operator >>(std::istream& in, dna_strand& strand) {
@klmr
klmr / IsAssignableToGenericTypeTests.cs
Created November 30, 2012 09:23
Comprehensive test cases for the IsAssignableToGenericType test
using System;
using System.Collections.Generic;
namespace klmr {
// Version from http://stackoverflow.com/a/75502/1968
public class Test {
public static bool IsAssignableToGenericType(Type givenType, Type genericType) {
var interfaceTypes = givenType.GetInterfaces();
foreach (var it in interfaceTypes)
@klmr
klmr / trie.cpp
Created November 27, 2012 14:19
Anagram clustering using a trie
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <boost/container/vector.hpp>
struct TrieNode {
boost::container::vector<TrieNode> children;
std::vector<std::vector<std::string>::const_iterator> head;
@klmr
klmr / string.hpp
Created October 12, 2012 17:21
Ultra-simple string class
#ifndef TEXT_STRING_HPP
#define TEXT_STRING_HPP
#include <algorithm>
#include <cstring>
#include <iterator>
#include <memory>
#include <iosfwd>
namespace text {