Skip to content

Instantly share code, notes, and snippets.

#!/bin/bash
#
# Toggle the state (STOPPED/RUNNING) of a process, clicking to its window.
set -e
debug() { echo "$@" 2>&1; }
die() { local MESSAGE=$1
debug "Error: $MESSAGE"
exit 1
@tokland
tokland / codejam_2010_1b_file_fix_it.rb
Created March 15, 2011 20:34
Solution for codejam 2010_1b A. File Fix-it (Ruby)
#!/usr/bin/ruby
# http://code.google.com/codejam/contest/dashboard?c=635101#s=p0
#
# Author: tokland@gmail.com
class Problem
# Create a directory (as array of slugs) in an existing filesystem and
# return the final filesytem and the total creation cost.
def self.create_directory(filesystem, slugs)
if (head = slugs.first)
@tokland
tokland / challenge8.rb
Created May 8, 2011 08:57
Solution to challenge 8 from "El País"
# http://www.elpais.com/videos/sociedad/cubo/suma/cero/elpepusoc/20110505elpepusoc_2/Ves/
faces_vertices = [[0,1,2,3], [0,1,4,5], [4,5,6,7], [2,3,6,7], [0,3,4,7], [1,2,5,6]]
vertices_solution = ([+1, -1].repeated_permutation(8)).select do |vertices_values|
faces_values = faces_vertices.map do |vertices|
vertices_values.values_at(*vertices).inject(:*)
end
(vertices_values + faces_values).inject(:+) == 0
end
@tokland
tokland / problem11
Created May 27, 2011 11:16
Problem 11 El Pais
#!/usr/bin/ruby-1.9
require 'set'
class Problem11
def self.solve(boxes, nails_per_box, weights)
(1..nails_per_box).to_a.repeated_combination(boxes).select do |combination|
weights.repeated_permutation(boxes).map do |weights|
weights.zip(combination).map do |weight, value|
weight * value
end.inject(:+)
# Usage:
# $loopc 3 echo 'hello world'
# hello world
# hello world
# hello world
#
function loopc {
LIMIT=$1
shift 1
@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"
#!/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" |