Skip to content

Instantly share code, notes, and snippets.

View stormbrew's full-sized avatar

stormbrew stormbrew

  • Edmonton, Alberta, Canada
View GitHub Profile
@stormbrew
stormbrew / cdo.sh
Last active December 27, 2015 10:29
# Changes to directory passed in $1 and runs the command specified
# by the remaining arguments.
function cdo() {
at=${1}
shift
( # enter a subshell to perform the action
cd "${at}"
"$@"
)
}
#include <vector>
template <typename T>
void blah(T) {}
template <typename T>
std::vector<T> blorp(T) {
std::vector<T> x;
return x;
}
PS1_LASTEXIT='$(LE=$?; if [ ${LE} -ne 0 ]; then LED=$(perl -le "\$!+=${LE};print \$!"); echo "\[\e[31;1m\]^${LE}:${LED}\[\e[32;1m\]> "; fi)'
PS1_USERNAME="\[\e[37;1m\]\u\[\e[32;1m\]> "
PS1_JOBS='$(JB=$(jobs -l | wc -l); if [ ${JB} -gt 0 ]; then echo "\[\e[37;1m\]&\j\[\e[32;1m\]> "; fi)'
PS1_GIT='$(GPS=$(__git_ps1 %s); if [ -n "${GPS}" ]; then STAT=$(git status --porcelain | cut -c1-2 | sed -E "s/ /-/" | sort | uniq | xargs echo | cut --delimiter=" " -f1- --output-delimiter=","); echo "\[\e[37;1m\]git:${GPS}:\[\e[35;1m\]${STAT}\[\e[32;1m\]> "; fi)'
PS1_WD="\[\e[37;1m\]\w\[\e[32;1m\]> "
PS1_PROMPTNUM="\[\e[37;1m\]!\!\[\e[32;1m\]"
# \! in PS2 actually prints the next command number because the command so far has already been appended to the history.
# Instead, get it from fc, which can tell you the last command number.
PS2_PROMPTNUM="\[\e[37;1m\]!\$(fc -l -1 | head -1 | awk '{print \$1}')\[\e[32;1m\]"
typedef int AA;
void foo()
{
AA AA; /* OK - define variable AA of type AA */
int BB = AA * 2; /* OK - AA is just a variable name here */
}
Python object model in a nutshell (with deliberate errors for simplicity):
- A class is a dict
- The dict contains methods (not in python syntax):
cls = { 'a': function(self, blah) {...}, 'b': function(self, blorp) {...} }
- An object is also a dict that contains an __class__ member that points to the class:
obj = { '__class__': cls }
- When you fetch a method from obj:
obj.a
it looks on __class__ for 'a', and finding it wraps it in a functor that adds the self parameter:
function(blah) { obj['__class__']['a'](obj, blah) }
#include <string>
#include <cstdio>
using namespace std;
class X
{
public:
string str;
X(const string &s) : str(s) { printf("Constructor %s 0x%p\n", str.c_str(), this); }
~X() { printf("Destructor %s 0x%p\n", str.c_str(), this); }
@stormbrew
stormbrew / gist:2521852
Created April 28, 2012 20:39
Devil's advocate argument for case based on its term-less form
# Ugly poorly aligned if
if a == b
elsif b == c
elsif z == d
else
end
#include <stdio.h>
int my_atoi(const char *str)
{
int neg = 1;
int x = 0;
if (*str == '-')
{
neg = -1;
*str++;
a = 10
l = ARGV.collect {|x| x.to_i }
c = 0
i = 0
while i < (l.size - 2)
j = i + 1
while j < (l.size - 1)
k = j + 1
while k < (l.size)
C++ code:
Data * data = m_cur_chunk->alloc(alloc_size);
data->init(size, type, m_cur_pool, false);
Data * alloc(size_t size)
{
Data * ret = (Data*)(m_data + m_used);
m_used += size;
return ret;
}