Skip to content

Instantly share code, notes, and snippets.

View raganwald's full-sized avatar

Reg Braithwaite raganwald

View GitHub Profile
@raganwald
raganwald / execute_array_of_functions.js
Created May 11, 2016 03:09
My new favourite interview question
var a = [
function () { console.log(1); },
function () { console.log(2); },
function () { console.log(3); }
];
a.forEach(Function.prototype.call.bind(Function.prototype.call));
function * just (...values) {
yield * values;
};
function first (iterable) {
const iterator = iterable[Symbol.iterator]();
const { done, value } = iterator.next();
if (!done) return value;
};
@raganwald
raganwald / hash_sieve.es6
Last active April 25, 2016 11:42
Hashtable Sieve of Eratosthenes
function * multiplesOf (startingWith, n) {
let number = startingWith;
while (true) {
yield number;
number = number + n;
}
}
class HashSieve {
@raganwald
raganwald / naïve.es6
Last active May 1, 2016 14:18
Naïve Sieve of Eratosthenes
function * nullEveryNth (skipFirst, n, iterable) {
const iterator = iterable[Symbol.iterator]();
yield * take(skipFirst, iterator);
while (true) {
yield * take(n - 1, iterator);
iterator.next();
yield null;
}
@raganwald
raganwald / valid-or-invalid.es6
Created March 15, 2016 15:51
Is this valid or invalid ES6?
function * append (...iterables) {
for (const iterable of iterables) {
yield * iterable;
}
}
const lyrics = append(["a", "b", "c"], ["one", "two", "three"], ["do", "re", "me"]);
for (const word of lyrics) {
console.log(word);
@raganwald
raganwald / fb.es6
Last active December 27, 2017 16:41
An elegant expression of the Fibonacci Sequence using generators https://en.wikipedia.org/wiki/Fibonacci_number
function * zipWith (zipper, ...iterables) {
const iterators = iterables.map(i => i[Symbol.iterator]());
while (true) {
const pairs = iterators.map(j => j.next()),
dones = pairs.map(p => p.done),
values = pairs.map(p => p.value);
if (dones.indexOf(true) >= 0) break;
yield zipper(...values);
@raganwald
raganwald / doSomething.js
Last active November 24, 2015 20:49
Maybe I don’t understand promises, either.
// I expect:
doSomething().then(doSomethingElse())
.then(finalHandler);
// To act like:
/*
doSomething
|-----------------|
@raganwald
raganwald / even-stevens.es6
Created August 10, 2015 13:02
evenStevens is read-only
const evenStevens = (n) => {
if (n === 0) {
return true;
}
else if (n == 1) {
return false;
}
else {
n = n - 2;
return evenStevens(n);

Many an idea or concept not only looks, but is good in its infancy, yet turns destructive later in life. Scaling and maturation are not the obvious processes they appear to be because they take so much time that the accumulated effort is easy to overlook.

To be successful, they must also be very carefully guided by people who can envision the end result, but that makes it appear to many as if it merely "happens." Take a good idea out of its infancy, let it age without guidance so it does not mature, and it generally goes bad.

Erik Naggum on XML

# See https://twitter.com/fogus/status/623312803345117184
def meth a, b, c
[yield(a), yield(b), yield(c)]
end
meth(1, 2, 3) { |x| x * x }
# => [1, 4, 9]
arr = [1962, 6, 14, lambda { |x| x.to_s }]