Skip to content

Instantly share code, notes, and snippets.

View cbsmith's full-sized avatar

Christopher Smith cbsmith

View GitHub Profile
@cbsmith
cbsmith / two_stars_98.cpp
Created May 21, 2013 17:09
C++98 version of two_stars
// -*- compile-command: "clang++ -ggdb -o two_stars_98 -std=c++98 -stdlib=libc++ two_stars_98.cpp" -*-
#include <algorithm>
#include <iostream>
using namespace std;
bool points_to_two(int* x) {
return *x == 2;
}
@cbsmith
cbsmith / test.cpp
Last active December 23, 2015 20:29
C++11 lambda sorcery
// -*- compile-command: "clang++ -ggdb -o test -std=c++11 -stdlib=libc++ test.cpp" -*-
#include <iostream>
float add(float x, float y) {
return x+y;
}
auto add_fourty_two = +[](float x) { return add(x, 42); };
auto add_pie_and_fourty_two = +[]() { return add_fourty_two(3.1415926); };
@cbsmith
cbsmith / test.cpp
Last active December 23, 2015 21:19
Attempt to create an operator||=
// -*- compile-command: "clang++ -Weverything -Wno-c++98-compat -ggdb -o test -std=c++11 -stdlib=libc++ test.cpp" -*-
#include <iostream>
#include <type_traits>
template <typename T, typename U, bool boolable = std::is_convertible<T,bool>::value, bool assignable = std::is_assignable<T, U>::value >
struct or_assign_helper;
template <typename T, typename U>
struct or_assign_helper<T, U, true, true> {
@cbsmith
cbsmith / endian.cpp
Last active December 30, 2015 13:29
My demonstration of how byte swap can still be useful despite http://commandcenter.blogspot.com/2012/04/byte-order-fallacy.html
// -*- compile-command: "g++ -O3 -march=native -std=c++11 -Wall -Werror endian.cpp -o endian" -*-
#include <fstream>
#include <ostream>
#include <istream>
#include <cstdint>
#include <algorithm>
#include <cassert>
#include <string>
#include <vector>
#include <limits>
@cbsmith
cbsmith / better_random.py
Last active January 4, 2016 10:19
My thoughts on enhancements to make random support a broader set of iterables.
import random
import itertools
def sample(population, k):
"How random.sample should really be defined."
if callable(getattr(population, '__len__', None)) and callable(getattr(population, '__getitem__', None)):
return random.sample(population, min(k, len(population)))
p = iter(population)
@cbsmith
cbsmith / sql_join.py
Last active August 29, 2015 13:56
An attempt to help this reddit poster use databases more effectively: http://www.reddit.com/r/Python/comments/1y521t/using_dictionaries_to_join_to_sql_results/
#!/usr/bin/env python
import sqlite3
from_location_group = {
'OBD1': 'Online',
'OBD2': 'Online',
'CB07': 'Retail',
'CB08': 'Retail',
'CB09': 'Retail',
@cbsmith
cbsmith / tad_test.cpp
Created March 1, 2014 02:38
Some examples of template argument deduction
// -*- compile-command: "clang++ -ggdb -std=c++11 -stdlib=libc++ -Wall -Werror tad_test.cpp -o tad_test" -*-
#include <iostream>
using namespace std;
template <typename T, typename U>
struct Foo {
T v;
U w;
@cbsmith
cbsmith / Copy.java
Created March 25, 2014 05:38
A simply gist demonstrating how to copy from stdin to stdout in Java, in response to discussion of: https://news.ycombinator.com/item?id=7463671
public class Copy {
public static void main(String[] args) throws Throwable {
int next;
while ((next = System.in.read()) != -1) {
System.out.write(next);
}
System.out.flush();
}
}
@cbsmith
cbsmith / idempotent_path_add.sh
Last active October 16, 2017 12:31
An example on how to do idempotent PATH manipulation.
#!/usr/local/bin/bash
#How to manipulate the path in an intelligent fashion with bash. Note: this doesn't work with Bourne shell.
function idempotent_path_add {
DIR="$1"
PREPEND=$2
if [[ ! "$PATH" =~ (^|:)"$DIR"(:|$) ]]
then
if [ $PREPEND ]
then
@cbsmith
cbsmith / format_strings.py
Last active August 29, 2015 14:13
An example of how one could wrap pyodbc's wildcard prepared statements with Python's format string to have keyword based prepared statements.
from string import Formatter
import sys
def keywords_to_questionmarks(format_string, *args, **kwargs):
'''Convert a format string keyword formatted query to a ? wildcarded prepared statement
that is pyodbc compatible, along with the args tuple to pass to it.
* format_string: Python format compatible representation of a query
* args: positional args for the format string
* kwargs: keyword args for the format string