Skip to content

Instantly share code, notes, and snippets.

@sunetos
sunetos / iterize.jl
Last active August 29, 2015 14:05
Julia macro to generate iterator versions of collection functions
using Devectorize
macro makeiter(func, name)
expr = quote
immutable $name{X}
x::X
end
Base.start(i::$name) = start(i.x)
Base.next(i::$name, s) = ((v, s) = next(i.x, s); ($func(v), s))
@sunetos
sunetos / duke-cc.js
Created August 2, 2014 11:49
Duke Chromecast Test
/**
Copyright (C) 2013 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
@sunetos
sunetos / fizzbuzz_explained.py
Last active March 28, 2022 00:39
A breakdown of my condensed, obfuscated fizzbuzz for educational purposes
#!/usr/bin/env python
__author__ = 'Adam R. Smith (adam@adamia.com)'
# Loop from 0 through 99. Need zero-based for the later cyclic bit-shifting.
for x in xrange(100):
# There are 4 possible outputs on each iteration of the loop.
# 4 possibilities requires 2 bits to store (0, 1, 2, 3):
# 0 == 00b == the number (x + 1)
# 1 == 01b == 'Fizz' when a multiple of just 3
@sunetos
sunetos / update-julia
Created May 22, 2014 14:05
Update to latest Julia nightly on a Mac
#!/bin/sh
VERSION="0.3.0"
URL="http://status.julialang.org/download/osx10.7+"
APP="/Applications/Julia-$VERSION-prerelease.app"
echo "Downloading Julia v$VERSION nightly..."
dmg="/tmp/julia-v$VERSION-installer.dmg"
curl -fsSL "$URL" > "$dmg"
@sunetos
sunetos / maybe.jl
Last active August 29, 2015 14:01
Julia macro for typed optional function arguments
# Mark a function argument of any type as optional by generating a Union.
# If a default value is not defined, it assumes "nothing".
#
# Examples:
# function(x::Int, @maybe y::MyType)
# function(x::Int, @maybe y::MyType=someval)
macro maybe(argexpr)
default = :nothing
if argexpr.head == :(=)
argexpr, default = argexpr.args
@sunetos
sunetos / udacity-video-expand.js
Last active August 29, 2015 13:57
Udacity Video Auto-Expand
// ==UserScript==
// @name Udacity Video Auto-Expand
// @namespace http://about.me/adam.r.smith
// @version 1.0
// @description Make Udacity videos automatically fill the page. Handy with Chromecast/Airplay.
// @match https://www.udacity.com/course/viewer
// @copyright 2014+, http://about.me/adam.r.smith
// ==/UserScript==
var w = 1280,
@sunetos
sunetos / bitey_struct.py
Created November 4, 2012 00:12
Bitey Autogeneration of Struct Member Names with Ctags
#!/usr/bin/env python
"""Patch bitey to auto-generate struct member names with exuberant ctags."""
__author__ = 'adam@adamia.com (Adam R. Smith)'
from collections import OrderedDict
import logging as log
import os
import subprocess
@sunetos
sunetos / gist:1068329
Created July 6, 2011 21:07
Simple Python ID Allocation Pool
__author__ = 'Adam R. Smith'
class IDPool(object):
'''
Create a pool of IDs to allow reuse. The "new_id" function generates the next
valid ID from the previous one. If not given, defaults to incrementing an integer.
'''
def __init__(self, new_id=None):
if new_id is None: new_id = lambda x: x + 1
import redis
from contextlib import contextmanager
@contextmanager
def redisLock(r, key):
'''
Acquire a lock on a particular key (makes a new mutex key).
This is not meant for very high concurrency, just to avoid
data corruption in simple cases.
'''
/**
* @preserve
*
* Javascript Hill Climb Algorithm.
* @author Adam R. Smith http://codi.st/
*
* Licensed under the new BSD License:
* http://www.opensource.org/licenses/bsd-license.php
*/