Skip to content

Instantly share code, notes, and snippets.

operator prefix ~ {}
@prefix func ~(pattern: String) -> NSRegularExpression! {
var error: NSError?
return NSRegularExpression.regularExpressionWithPattern(
pattern,
options: nil,
error: &error)
}
@kourge
kourge / gist:38c1d3a07a5dee9407c0
Created June 4, 2014 21:19
Fun with double arrow / hash rocket in Swift
operator infix => { associativity left }
func => <K : Hashable, V>(key: K, value: V) -> (K, V) {
return (key, value)
}
let pairs: (String, Int)[] = [
"e" => 10,
"t" => 7,
"i" => 2
]
enum Quadrant {
case BottomLeft
case TopLeft
case BottomRight
case TopRight
static func forPoint(point: CGPoint) -> Quadrant {
switch (point.x, point.y) {
case (let x, let y) where x >= 0 && y >= 0:
return .TopRight
func chunk<T : Sliceable where T.Index : Strideable>(
s: T,
#from: T.Index,
#to: T.Index,
#by: T.Index.Stride
) -> GeneratorOf<T.SubSlice> {
var g = stride(from: from, to: to, by: by).generate()
return GeneratorOf<T.SubSlice> {
if let start = g.next() {
@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