Skip to content

Instantly share code, notes, and snippets.

@kch
kch / scanl.rb
Last active December 31, 2023 10:22
ruby scanl
# Operating with plain arrays:
class Array
def scanl(init, &f)
return [init] if empty?
reduce([init]){ |xs, x| xs << f.(xs.last, x) }
end
def scanl1(&f)
return if empty? # might consider returning []
@kch
kch / ftff-events.rb
Created August 3, 2010 13:34
mac: watch home dir via fsevents and set encoding attr and reveal extension for txt and rb files
#!/usr/bin/ruby
# encoding: UTF-8
require 'rubygems'
require 'fsevent'
require 'shellwords'
class Time
def age_in_seconds(now = Time.now)
now - self
// ==UserScript==
// @name ⌘⌫ Element
// @description ⌘⌫ deletes element under cursor, ⌘Z puts back
// @match *://*.*
// ==/UserScript==
let undoWindow = 3000 // can undo for this long
let undoCooldown = 1500 // will prevent default undo for this long after last undo
let scheduledDeletion = null // timeout id for deleteElements
let lastHide = 0 // timestamp for last element was hidden
@kch
kch / plist-to-yaml.rb
Created August 4, 2010 09:54
converts plist files to really pretty, aligned yaml files with mostly literal utf8 strings
#!/usr/local/bin/ruby
# encoding: UTF-8
require 'yaml'
require "base64"
require 'rexml/document'
class PList < BasicObject
ELEMENT_PROCESSORS = {}
def self.process_element_named(name, &block); ELEMENT_PROCESSORS[name.to_s] = block; end
def self.process_element(elt) ; ELEMENT_PROCESSORS[elt.name][elt]; end
// ==UserScript==
// @name Youtube ensure HD
// @description Sets youtube player to highest resolution if drops from an HD res
// @match *://www.youtube.com/watch*
// @match *://youtu.be/watch*
// ==/UserScript==
let intervalID = setInterval(()=> {
let qs = (q) => document.querySelector(q)
let qsa = (q) => [...document.querySelectorAll(q)]
@kch
kch / Rewind 30 Seconds.rb
Created May 2, 2010 21:40
Rewind currently playing track 30 seconds in iTunes
#!/usr/bin/env ruby1.9
# encoding: UTF-8
require 'appscript'
include Appscript
itunes = app("iTunes")
pos = itunes.player_position.get
pos = pos > 30 ? pos - 30 : pos / 2
itunes.player_position.set pos
@kch
kch / git-where-am-i.sh
Created January 5, 2010 13:47
git get name of current branch
# Previous versions of this gist brought to you by major OCD.
# I now leave you with my pick.
git branch | ack -o '(?<=^\* ).*'
@kch
kch / yaml-to-plist.rb
Created August 4, 2010 14:50
Writes ruby object structures to plists. When called directly, takes a YAML file.
#!/usr/bin/env ruby
# encoding: UTF-8
require 'yaml'
require 'rexml/document'
class PlistWriter
PLIST_STUB_DOC = %q[
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0"></plist>]
@kch
kch / infix-functions.swift
Last active July 17, 2016 20:30
Use any binary function as infix operator in Swift
infix operator « { precedence 131 associativity left }
func «<A,B,C>(a:A, f:(A,B) -> C) -> B -> C { return { f(a,$0) } }
infix operator » { precedence 131 associativity left }
func »<B,C>(f:(B) -> C, b:B) -> C { return f(b) }
infix operator < { precedence 131 associativity left }
func <<A,B,C>(a:A, f:(A,B) -> C) -> B -> C { return { f(a,$0) } }
infix operator > { precedence 131 associativity left }
@kch
kch / wgt.rb
Created May 12, 2016 12:29
sort wgt bands by time, append location
#!/usr/bin/env ruby
s = ARGF.read =~ /Freitag, 13. Mai 2016.*?(?=\n\n)/m && $&
s.gsub! /^(\d\d)\.(\d\d)\u00A0Uhr\n/, "\\1:\\2h -- "
days = []
s.lines.each do |l|
case l
when /^\d\d:\d\dh -- Einlaß/ then :pass
when /Mai 2016$/ then days << [l, []]
when /^\d\d:\d\dh -- / then days.last[1] << l.sub(/\Z/, " -- @ #{$x}")