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(' -> ');
'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) {
@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 ]
@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');
(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 / 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
@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