Skip to content

Instantly share code, notes, and snippets.

View SimonRichardson's full-sized avatar
👻
We're going on an adventure!

Simon Richardson SimonRichardson

👻
We're going on an adventure!
View GitHub Profile
function foreach(a, f) {
var total,
i;
for (i = 0, total = a.length; i < total; i++) {
f(a[i]);
}
}
function Propagate(value){
@SimonRichardson
SimonRichardson / Seq.js
Last active December 21, 2015 06:58
Proxies - in this instance `__noSuchMethod__` indirect invocation on a different method. This is really powerful for wrapping native items (like array, object) and providing methods that point to libraries like underscore, squishy. This following unboxes (`extract`) the `Seq` to works on the array and then boxes the array back into a `Seq` so it…
/**Note: This works in Firefox Nightly but not in Chrome (shame on you!)**/
var s = {
concat: function(a, b) {
return a.concat(b);
},
map: function(a, f) {
var accum = [],
total,
i;
@SimonRichardson
SimonRichardson / lazyAsync.js
Last active December 21, 2015 09:19
Listener/Observable based lazy async. It holds onto a function until it has been fulfilled, then it holds onto the captured response so that any future subscribers are also notified (in a lazy way). This is stateful in many ways and can be used for a generator in Streams and Promises in squishy-pants or similar libraries.
function lazyAsync(f) {
var args = [].slice.call(arguments).slice(1),
listeners = [],
captured;
f.apply(null, [
function() {
captured = [].slice.call(arguments);
for(var i = 0, total = listeners.length; i < total; i++) {
listeners[i].apply(null, captured);
@SimonRichardson
SimonRichardson / index.js
Created August 23, 2013 10:32
requirebin sketch
var Store = require('fantasy-stores'),
target,
store = Store(
function(x) { return target = x; },
function() { return target; }
),
newStore;
store.set(1);
@SimonRichardson
SimonRichardson / yield.js
Created September 3, 2013 09:20
Generators in javascript
function* create() {
for(var i = 0; i < 10; i++) {
yield i;
}
}
var gen = create();
console.log(gen.next());
@SimonRichardson
SimonRichardson / iter.js
Last active December 22, 2015 05:09
Wrapper around Iterators/Generators in javascript so that we can find out if an iterator has a next value or not.
function Iter(machine) {
this.gen = machine();
this.current = this.__iterator__();
}
Iter.prototype.hasNext = function() {
return this.current.hasNext();
};
Iter.prototype.next = function() {
return this.current.next();
};
@SimonRichardson
SimonRichardson / router.js
Last active December 22, 2015 06:18
Building a router using partials.
var squishy = require('squishy-pants/bin/squishy-pants.min'),
log = squishy.IO(squishy.constant(
function(x) {
console.log(x);
}
)),
logger = squishy.curry(
function(prefix, value) {
log.ap(squishy.IO.of(prefix + ' > ' + value)).unsafePerform();
}
@SimonRichardson
SimonRichardson / time.js
Created September 12, 2013 10:23
Time structure
var time = function(milliseconds) {
var value = milliseconds;
return {
concat: function(t) {
return time(value + t.extract());
},
extract: function() {
return value;
},
map: function(f) {
@SimonRichardson
SimonRichardson / time.sjs
Last active December 22, 2015 22:49
Macro time
macro $time {
case {_ + $x:lit h $rest ... } => {
return #{ .map(function(x) { return x + ($x * 1000 * 60 * 60) }) $time $rest ... }
}
case {_ + $x:lit m $rest ... } => {
return #{ .map(function(x) { return x + ($x * 1000 * 60) }) $time $rest ... }
}
case {_ + $x:lit s $rest ... } => {
return #{ .map(function(x) { return x + ($x * 1000) }) $time $rest ... }
}
@SimonRichardson
SimonRichardson / string.sjs
Created September 17, 2013 09:43
Create a string from a series of raw identifiers
macro $string {
case {_ ([$x $inner ...] $rest ...)} => {
var ctx = #{$x},
value = ctx[0].token.value,
str = makeValue(value, #{here});
return withSyntax($val = [str]) {
return #{'[' + $val + $string($inner ...) + ']' + $string($rest ...)}
}
}