Skip to content

Instantly share code, notes, and snippets.

View odf's full-sized avatar

Olaf Delgado-Friedrichs odf

View GitHub Profile
@odf
odf / cantor.js
Last active November 20, 2019 09:01
Flattening an infinite list of infinite lists in JavaScript via Cantor diagonalisation
class Seq {
toArray() {
const a = [];
for (const x of this)
a.push(x);
return a;
}
toString() {
return this.toArray().join(' -> ');
@odf
odf / factorial.coffee
Created August 23, 2011 00:06
A tail-recursive factorial in Coffeescript with simulated tail-call optimization.
bounce = (val) -> val = val() while typeof val == 'function'; val
factorial = (n) ->
fact = (a, n) -> if n > 0 then -> fact a * n, n - 1 else a
bounce fact 1, n
console.log factorial 10
(defn tails [s]
"The sequence of sequences starting at each position in the given sequence 's'."
(lazy-seq (when-let [s (seq s)]
(cons s (tails (rest s))))))
(defn reduce-true [f val coll]
"A short-circuiting version of 'reduce'. Stops evaluation when the
accumulated value is logically false."
(let [v (->> (reductions f val coll)
(tails)
@odf
odf / lazy_seq.rb
Created October 7, 2011 12:41
Fun with lazy sequences in Ruby
class Seq
attr_reader :first
def initialize(first, &rest)
@first = first
@rest = rest
end
def rest
@rest = @rest.call()
x = [1,2,3,4]
x.zip([true]).each do |item, first|
puts "\n" unless first
puts item
end
@odf
odf / show.coffee
Created August 20, 2011 04:08
Look Ma, no eval!
# Displays a line of code alongside the type and value of its output.
#
# For example, the line
#
# show -> 1 * 2 * 3
#
# or, in Javascript
#
# show( function () { return 1 * 2 * 3 } )
#
@odf
odf / gist:1135616
Created August 10, 2011 00:21
Handling cookies in Capybara with Rack::Test so that controllers see them
def set_cookie(key, value)
# This is how one needs to set cookies in Rack::Test.
jar.merge "#{key.to_s}=#{value};domain=#{domain};path=/"
end
def get_cookie(key)
jar[key.to_s]
end
def jar
@odf
odf / time_in_words.rb
Created July 27, 2011 05:44
Converts time in seconds into English.
module TimeInWords
CONVERSIONS =
[ [:seconds, :years, 31556952], # the average year in the Gregorian calendar
[:seconds, :minutes, 60],
[:minutes, :hours, 60],
[:hours, :days, 24],
[:days, :weeks, 7] ]
UNIT_NAMES =
[ [:years, "year", "years" ],
require File.expand_path(File.dirname(__FILE__) + '/../acceptance_helper')
feature "Approve", %q{
In order to manage workloads and the placement of material
As a manager
I want to approve studies and assign them to archivists
} do
background do
create_user 'Alice'
'use strict';
var csp = require('plexus-csp');
// Creates a detached (i.e. never yielded) asynchronous task with some error
// reporting upon failure. The explicit error handling is necessary because
// error propagation to the calling task only works if we yield the result.
var detach = function(task) {