Skip to content

Instantly share code, notes, and snippets.

View jacob414's full-sized avatar
💭
Retired but semi-active, hobby projects & activism

Jacob Oscarson jacob414

💭
Retired but semi-active, hobby projects & activism
View GitHub Profile
@jacob414
jacob414 / pseudogen.coffee
Created February 20, 2012 10:40
CoffeeScript pseudo-generator
counter = (start) ->
n = start
-> n++
c = counter(0)
while 2 >= i = c()
alert i
@jacob414
jacob414 / altered_states_demo1.py
Created March 1, 2012 11:15
Simple demo of Altered States context manager
from altered import state
class A(object): pass
o = A()
o.foo = 'foo'
with(state(o, foo='bar')):
# prints 'bar'
print o.foo
# prints 'foo', o.foo now restored to previous value
@jacob414
jacob414 / altered_states_demo2.py
Created March 1, 2012 11:26
Simple demo of Altered States decorator
from altered import state
org = {'foo': 'foo'}
@state(org, foo='bar')
def fn():
return org['foo']
# prints 'bar'
print fn()
@jacob414
jacob414 / swiss_army_path.py
Created April 11, 2012 08:57
Swiss army path function
import os
def path(*segs):
"""
CWD and `~` aware path construction. Start a segment with `/` for absolute paths.
*(Example, on my machine)*
>>> path('sub', 'dir')
/Users/jacob/src/tmp/sub/dir
>>> path('~', 'src', 'project', 'pkg')
/Users/jacob/src/project/pkg
@jacob414
jacob414 / create_project.sh
Created May 16, 2012 20:18 — forked from shazow/create_project.sh
Setup deployment target for Nginx + Python/uWSGI + Supervisor + Git (for a Linode Stackscript)
#!/bin/bash
# Setup deployment target for Nginx + Python/uWSGI + Supervisor + Git
# From: https://gist.github.com/1210041
function usage() {
cat << EOF
Usage: $0 PROJECT_NAME [DOMAIN] [OPTIONS]
Options:
-y No prompts, assume yes to all.
@jacob414
jacob414 / blocks.m
Created May 24, 2012 19:39
With blocks and varargs Objective C might not be as bad after all
#include <stdarg.h>
#import <Foundation/Foundation.h>
@interface Cls : NSObject {}
-(int) takes_block: (int)limit withBlock:(int (^)(int first, ...))block;
@end
@implementation Cls
-(int) takes_block: (int)limit withBlock:(int (^)(int first, ...))block {
_ = require 'underscore'
any = @any = (x) -> x
none = @none = (x) -> false
gt = @gt = (n) -> (x) -> x > n
eq = @eq = (p) -> (x) -> p == x
ne = @ne = (p) -> (x) -> x != p
match = @match = (P, L) ->
if P.length != L.length then false
@jacob414
jacob414 / ipaddr.m
Created November 14, 2013 16:25
Get IP address of iOS device
#import <ifaddrs.h>
#import <arpa/inet.h>
/**
* Returns the IP adress of the device.
*/
- (NSString*)ipaddr {
NSString *address = nil;
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
@jacob414
jacob414 / closest.coffee
Last active August 29, 2015 14:01
Interpolation of element closest to point by sampling in expanding circles
@closest = (x, y) ->
el = document.elementFromPoint x, y
unless el is null or el.nodeName is 'BODY' then el
else
slice = 2 * Math.PI / 8
for radius in [5...200] by 8
for step in [0...7]
angle = slice * step
[candX, candY] = [x + radius*Math.cos(angle), y + radius*Math.sin(angle)]
cand = document.elementFromPoint candX, candY
@jacob414
jacob414 / backing-off.coffee
Last active August 29, 2015 14:06
backing-off.coffee
# Re-try function with exponential back-off time
# `max`: Maximum amount of attempts
# `wait`: Wait time between attemps, in milliseconds
# `attempt`: Function to make one attempt, accepts arguments <nth try>, <callback upon success>
gently = @gently = (max, wait, attempt) ->
new Promise (fulfill, fail) ->
recur = (max, wait, attempt, n) ->
pending = setTimeout (->
if n is max