Skip to content

Instantly share code, notes, and snippets.

@tokland
tokland / js.js
Created January 19, 2012 17:49
Some thoughts on Roy
exports.getProperty = function(name, obj) {
return obj[name];
};
@tokland
tokland / chain.py
Created January 24, 2012 19:22
Playing (informally) with the Either monad in Python
# Rationale: How to chain functions with type (a -> transformation_of_a) #
# as they were (a -> a)?
#
# If the functions were "normal" (a -> a), we'd write simply: fun3(fun2(fun1(1)))
def fun1(x):
return ("ok", 1*x) if x > 1 else ("fail", "fun1: value of x is ugly: %s" % x)
def fun2(x):
return ("ok", 2*x) if x > 2 else ("fail", "fun2: value of x is ugly: %s" % x)
@tokland
tokland / blynx-README.md
Created February 22, 2012 19:19
Design notes for Blynx, a functional programming language.

Blynx

Blynx is a functional and statically-typed programming language.

It generates Javascript code, so it can be used both client-side (in a browser) or server-side (with node.js).

State: in development

Features

@tokland
tokland / extract-all.sh
Last active October 5, 2015 09:57
extractor for the most usual archive formats
#!/bin/bash
# all-in-one extractor for the most usual archive formats. Requires Bash >= 4
set -e -u -o pipefail
declare -A COMMANDS=(
[z]="compress -d"
[gz]="gunzip"
[bz2]="bunzip2"
[zip]="unzip -qo"
[rar]="unrar x"
@tokland
tokland / coursera-download-videos.rb
Last active November 24, 2016 08:14
Download video lectures from Coursera courses.
#!/usr/bin/ruby
#
# Download lecture videos of courses from Coursera (http://www.coursera.org).
#
# Install requirements:
#
# $ gem install curb trollop nokogiri capybara ruby-progressbar
#
# Example -- Download all video lectures of courses "Calculus: Single Variable"
# and "Introduction to Astronomy":
#!/bin/sh
#
# Requirements: echoprint-codegen, curl, js-beautify
#
set -e -u -o pipefail
SERVER="http://developer.echonest.com/api/v4/song/identify"
API_KEY=FILDTEOIK2HBORODV
FILE=$1
echoprint-codegen "$FILE" |
# Or: require 'facets/enumerable/find_yield'
module Enumerable
def map_detect
self.each do |member|
if (result = yield(member))
return result
end
end
nil
end
@tokland
tokland / monitor.sh
Last active December 17, 2015 18:29
Simple generic monitoring tool using a finite-state-machines implementation.
#!/bin/bash
#
# Simple monitoring using a finite state machines
set -e -u -o pipefail
# Print debug output to standard error
debug() {
echo "[${FUNCNAME[1]}] $@" >&2
}
@tokland
tokland / importer.rb
Last active December 20, 2015 20:59 — forked from regedarek/importer.rb
class Importer
CODES = {:header, "00F", :data => "00I"}
def self.import_products(fileobj)
fileobj.lines.each_slice(4).map do |group_lines|
type, *data_lines = group_lines.map(&:strip)
case type
when CODES[:header]
# what to do/return here?
when CODES[:data]
@tokland
tokland / video-resize.sh
Last active December 25, 2017 22:31
Resize videos to a fixed size with avconv
#!/bin/sh
#
# Resize videos to a given size.
# Depedencies: libav
debug() {
echo "$@" >&2
}
to_num() { local NUM_STRING=$1