Skip to content

Instantly share code, notes, and snippets.

@kourge
kourge / number-upto.js
Last active September 23, 2015 15:38
for (var i of (1).upto(5)) { ... }
Number.prototype.upto = function upto(end) {
var start = this;
function NumberIterator() {
this.current = start;
}
NumberIterator.prototype.next = function next() {
if (this.current > end) {
throw StopIteration;
} else {
return this.current++;
// https://developer.mozilla.org/En/Using_audio_and_video_in_Firefox#Media_events
// https://developer.mozilla.org/en/XPCOM_Interface_Reference/nsIDOMHTMLMediaElement
var Sounds = {
sounds: {},
errors: [null, "ABORTED", "NETWORK", "DECODE", "SRC_NOT_SUPPORTED"],
register: function register(sounds) {
var errors = this.errors;
for (var i = 0, l = sounds.length; i < l; i++) {
let self = require('self'), file = require('file'), url = require('url');
let contextMenu = require('context-menu');
let sym = gensym(), containerID = 'word-cloud-' + sym;
let item = contextMenu.Item({
label: "Show Word Cloud",
onClick: function(context, item) {
let win = context.window, corpus = generateCorpus(win);
let scores = score(corpus), sorted = plist(scores);
sorted.sort(byScore);
@kourge
kourge / gist:820296
Created February 10, 2011 10:47
MacFUSE fs in MacRuby, with custom volume icon
framework 'cocoa'
class HelloFS
PATH = '/hello.txt'
def contentsOfDirectoryAtPath(path, error: error)
[PATH.lastPathComponent]
end
def contentsAtPath(path)
@kourge
kourge / output.png
Created July 2, 2011 21:43
Pixelizing images with ChunkyPNG - Codebrawl
output.png
#!/bin/sh
# Run this script with sudo
launchctl unload /System/Library/LaunchDaemons/org.cups.cupsd.plist
cp /etc/cups/cupsd.conf /etc/cups/cupsd.conf.cse
cat >> /etc/cups/cupsd.conf <<EOF
# Show shared printers on the UW CSE network.
BrowseRemoteProtocols cups
@kourge
kourge / gist:3137021
Created July 18, 2012 15:43
Relaunch current script in MacRuby
module Env
MACRUBY_FRAMEWORK_BIN = 'MacRuby.framework/Versions/Current/usr/bin/macruby'
MACRUBY_CANDIDATES = [
"/System/Library/PrivateFrameworks/#{MACRUBY_FRAMEWORK_BIN}",
"/Library/Frameworks/#{MACRUBY_FRAMEWORK_BIN}"
]
def self.find_macruby
MACRUBY_CANDIDATES.each do |path|
return path if File.exist?(path)
#!/usr/bin/env macruby
framework 'CoreFoundation'
class Range
def to_core_range
location = self.begin
length = (self.exclude_end? ? self.end : self.end.succ) - location
CFRange.new(location, length)
end
@kourge
kourge / es6-generators-stopiteration.js
Created June 6, 2013 17:59
The ES6 spec states that calling the send method on a generator should result in an object. So long as the generator is yielding more values, the object is { value: v, done: false } where v is the value yielded. When the generator stops, the object is { value: undefined, done: true }. Firefox has had a preliminary implementation of generators th…
this.Generator && (function(g, global) {
var __send = g.prototype.send;
g.prototype.send = function send(value) {
var result = __send.call(this, value);
if (result.done) {
throw new StopIteration();
}
return result.value;
};
@kourge
kourge / context_manager.php
Last active August 9, 2023 03:25
A simple user-space PHP `with` implementation that mimics Python context managers.
<?php
class Context {
private $args = array();
private $func = NULL;
public function __construct() {
$args = func_get_args();
$func = array_pop($args);