Skip to content

Instantly share code, notes, and snippets.

View odf's full-sized avatar

Olaf Delgado-Friedrichs odf

View GitHub Profile
@odf
odf / flux-csp-dispatcher.es6
Last active August 29, 2015 14:05
Asynchronous Flux-like dispatcher with deadlock prevention via Go-style CSP
// Asynchronous Flux-like dispatcher with deadlock prevention via Go-style CSP.
//
// - my libraries ceci-core and ceci-channel are available from npm
// - pre-compile this file with regenerator
// - build with webpack or browserify
'use strict';
var ceci = require('ceci-core');
var chan = require('ceci-channels');
@odf
odf / Output
Last active August 29, 2015 14:08
Demo for small bug in transducers.js 0.2.3
Original:
.1.
.1.
.2.
.4.
.3.
.9.
.4.
.16.
[ 1, 1, 2 ]
'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) {
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'
@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" ],
@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 / 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 } )
#
x = [1,2,3,4]
x.zip([true]).each do |item, first|
puts "\n" unless first
puts item
end
@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()
(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)