Skip to content

Instantly share code, notes, and snippets.

View nathan-russell's full-sized avatar

Nathan Russell nathan-russell

View GitHub Profile
@nathan-russell
nathan-russell / ip-info.sh
Last active November 23, 2015 23:34
IP Address Geographical Information Using ipinfo.io
#!/bin/bash
##
LBREAK="====================================================="
echo -e "\n"
##
if [[ -z "$@" ]]
then
ipCMD="ipinfo.io/"
echo Using own public IP Address:
curl -s "$ipCMD"
#include <Rcpp.h>
// ColumnBindable
//
// Provides a common, matrix-like interface
// for use in Cbind class.
template<int RTYPE, typename T>
class ColumnBindable {
public:
typedef typename Rcpp::traits::storage_type<RTYPE>::type stored_type;
@nathan-russell
nathan-russell / cbind_v2.cpp
Created December 7, 2015 23:04
Revised for dangling temporaries issue
#include <Rcpp.h>
// ColumnBindable
//
// Provides a common, matrix-like interface
// for use in Cbind class.
template<int RTYPE, typename T>
class ColumnBindable {
public:
typedef typename Rcpp::traits::storage_type<RTYPE>::type stored_type;
@nathan-russell
nathan-russell / gen_cbind_macro_header.R
Last active December 8, 2015 01:06
R script for generating cbind macro header file
make_cbind_n <- function(n) {
if (n == 2) {
return("#define CBIND_IMPL_2(T1, T2) (cbind_impl(T1, T2))")
}
macro_args <- paste0("T", 1:n, collapse = ", ")
call_args <- paste0("T", 1:(n-1), collapse = ", ")
sprintf(
"#define CBIND_IMPL_%i(%s) (CBIND_IMPL_2(CBIND_IMPL_%s(%s), T%i))",
n, macro_args, n - 1, call_args, n
@nathan-russell
nathan-russell / test_cbind_macro.cpp
Created December 8, 2015 01:13
Test __VA_ARGS__ cbind macro
#include <Rcpp.h>
template<int RTYPE, typename T>
class ColumnBindable {
public:
typedef typename Rcpp::traits::storage_type<RTYPE>::type stored_type;
typedef T VEC_TYPE;
private:
const VEC_TYPE& vec;
@nathan-russell
nathan-russell / cbind_macro_dispatch.h
Created December 8, 2015 01:15
Sample generated macro (n == 50)
#ifndef CBIND__MACRO__DISPATCH__H
#define CBIND__MACRO__DISPATCH__H
#define CBIND_IMPL_2(T1, T2) (cbind_impl(T1, T2))
#define CBIND_IMPL_3(T1, T2, T3) (CBIND_IMPL_2(CBIND_IMPL_2(T1, T2), T3))
#define CBIND_IMPL_4(T1, T2, T3, T4) (CBIND_IMPL_2(CBIND_IMPL_3(T1, T2, T3), T4))
#define CBIND_IMPL_5(T1, T2, T3, T4, T5) (CBIND_IMPL_2(CBIND_IMPL_4(T1, T2, T3, T4), T5))
#define CBIND_IMPL_6(T1, T2, T3, T4, T5, T6) (CBIND_IMPL_2(CBIND_IMPL_5(T1, T2, T3, T4, T5), T6))
#define CBIND_IMPL_7(T1, T2, T3, T4, T5, T6, T7) (CBIND_IMPL_2(CBIND_IMPL_6(T1, T2, T3, T4, T5, T6), T7))
#include <Rcpp.h>
template <int RTYPE>
struct CompareTwo {
typedef typename Rcpp::traits::storage_type<RTYPE>::type stored_type;
typedef Rcpp::Vector<RTYPE> VECTOR;
inline static Rcpp::Vector<LGLSXP>
apply(const VECTOR& x, stored_type y, stored_type z) {
R_xlen_t i = 0, n = x.size();
@nathan-russell
nathan-russell / NumbersTable.sql
Last active January 12, 2016 14:22
Numbers Table vs. Recursive CTE
USE bizdev
--
GO
--
SET STATISTICS IO ON;
SET STATISTICS TIME ON;
--
DECLARE @max INT = 40000;
--
SELECT GETDATE() - n.Number
#include <Rcpp.h>
namespace sugar {
namespace median_detail {
// need to return double for integer vectors
// (in case of even-length input vector)
// and Rcpp::String for STRSXP
// also need to return NA_REAL for
// integer vector yielding NA result
saveRDS((v1 <- 1:5), file = "/tmp/v1.RDS"); system("gzip -cd -S=gz /tmp/v1.RDS | hexdump")
# 0000000 0a58 0000 0200 0300 0302 0200 0003 0000
# 0000010 0d00 0000 0500 0000 0100 0000 0200 0000
# 0000020 0300 0000 0400 0000 0500
# 000002a
saveRDS((v2 <- 6:10), file = "/tmp/v2.RDS"); system("gzip -cd -S=gz /tmp/v2.RDS | hexdump")
# 0000000 0a58 0000 0200 0300 0302 0200 0003 0000
# 0000010 0d00 0000 0500 0000 0600 0000 0700 0000