This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
counter = (start) -> | |
n = start | |
-> n++ | |
c = counter(0) | |
while 2 >= i = c() | |
alert i |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from altered import state | |
org = {'foo': 'foo'} | |
@state(org, foo='bar') | |
def fn(): | |
return org['foo'] | |
# prints 'bar' | |
print fn() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_ = 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |