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: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
@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 / SessionLocked.html
Created April 1, 2013 21:05
Source code of Ryanair error page
<div id="sessionLocked">
<div id="p-en">
<h1>Error - Your session has been locked</h1>
<p><b>We are experiencing some difficulties with the website. Please wait 10 seconds and if you are not redirected back <a href="#" id="A1">click here</a>.</b></p>
<p><b>If you have already submitted payment your new reservation or flight change MAY have been confirmed.</b></p>
<p><b>Before starting again please firstly check your email account to see if you have received an email travel itinerary or alternatively you can check to see if your transaction processed by going to <a href="https://www.bookryanair.com/SkySales/FRManageBooking.aspx">Manage Booking</a> section of our website and completing the information required in Option 2.</b></p>
<br/>
</div>
<div id="l-de">
<h1>Fehler – Ihre Sitzung wurde gesperrt</h1>
@klmr
klmr / import.R
Created April 3, 2013 13:42
(Incomplete) simple module mechanism for R.
import <- function (module) {
module <- as.character(substitute(module))
# Search path handling omitted for simplicity.
filename <- paste(module, 'R', sep = '.')
includeGuard <- paste('.INCLUDE', toupper(module), 'R', sep = '_')
if (exists(includeGuard))
return()
assign(includeGuard, TRUE, envir = topenv())
@klmr
klmr / auto_cast.hpp
Last active December 16, 2015 09:28
template <typename Source>
struct auto_cast_helper {
Source const& value;
explicit auto_cast_helper(Source const& value) : value(value) { }
template <typename Target>
operator Target() const {
return static_cast<Target>(value);
}
@klmr
klmr / minimal.cpp
Created April 29, 2013 21:51
Minimised code
struct CProfiler {
struct CScopeProfiler { };
struct CScopeRun { };
typedef std::unordered_set< const CScopeProfiler*,
std::hash< const CScopeProfiler* >,
std::equal_to< const CScopeProfiler* >,
CAllocator< const CScopeProfiler* > > CScopeProfilersSet;
@klmr
klmr / examples.R
Last active December 16, 2015 22:19
Pointfree functional style in R (http://www.haskell.org/haskellwiki/Pointfree)
source('functional.R')
# Composing functions for fun and profit:
hsv2col <- function (col)
apply(col, 2, lpartial(do.call, hsv) %.% as.list)
# In non-functional style:
hsv2col <- function (col)
apply(col, 2, function (c) do.call(hsv, as.list(x)))
@klmr
klmr / gist:5555335
Created May 10, 2013 15:52
Wrapper around `gregexpr` to find and extract substrings
rxmatches <- function (pattern, text) {
result <-
mapply(function (positions, t)
mapply(function (p, l)
if (p == -1) NULL else substr(t, p, p + l - 1),
positions,
attr(positions, 'match.length')),
gregexpr(pattern, text),
text)
# Sanitise to get rid of redundant singleton lists of NULL
@klmr
klmr / gist:5884628
Last active December 19, 2015 02:39
Comparison of compilation with and without visibility=hidden
$ time g++-4.8 -std=c++0x -pedantic -Wall -Wextra -Werror -fvisibility=hidden -fvisibility-inlines-hidden -O0 -o test test.cpp
real 2m2.243s
user 1m59.593s
sys 0m2.444s
$ ll test
-rwxr-xr-x 1 rudolph staff 8678736 28 Jun 14:19 test
$ time g++-4.8 -std=c++0x -isystem/usr/local/include -pedantic -Wall -Wextra -Werror -O0 -o test test.cpp
real 2m1.174s
user 1m58.565s
sys 0m2.448s
@klmr
klmr / csv_parse.cpp
Created July 18, 2013 15:51
A very simple and incomplete outline of a file format parser in C++ which determines the exact format dynamically at runtime. Most of the classes involved represent values. A single class hierarchy represents a control flow logic.
#include <fstream>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
// This class is a container, hence a value type (akin to vector)
class row { /* ... */ };
// This class is a container (collection of rows), hence a value type.