Skip to content

Instantly share code, notes, and snippets.

Avatar

Olaf Delgado-Friedrichs odf

View GitHub Profile
@odf
odf / cantor.js
Last active Nov 20, 2019
Flattening an infinite list of infinite lists in JavaScript via Cantor diagonalisation
View cantor.js
class Seq {
toArray() {
const a = [];
for (const x of this)
a.push(x);
return a;
}
toString() {
return this.toArray().join(' -> ');
View asyncMap.js
'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 Aug 29, 2015
Demo for small bug in transducers.js 0.2.3
View Output
Original:
.1.
.1.
.2.
.4.
.3.
.9.
.4.
.16.
[ 1, 1, 2 ]
@odf
odf / flux-csp-dispatcher.es6
Last active Aug 29, 2015
Asynchronous Flux-like dispatcher with deadlock prevention via Go-style CSP
View flux-csp-dispatcher.es6
// 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');
View reduce-true.clj
(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 Oct 7, 2011
Fun with lazy sequences in Ruby
View lazy_seq.rb
class Seq
attr_reader :first
def initialize(first, &rest)
@first = first
@rest = rest
end
def rest
@rest = @rest.call()
View gist:1269754
x = [1,2,3,4]
x.zip([true]).each do |item, first|
puts "\n" unless first
puts item
end
@odf
odf / factorial.coffee
Created Aug 23, 2011
A tail-recursive factorial in Coffeescript with simulated tail-call optimization.
View factorial.coffee
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 Aug 20, 2011
Look Ma, no eval!
View show.coffee
# 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 Aug 10, 2011
Handling cookies in Capybara with Rack::Test so that controllers see them
View gist:1135616
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