Skip to content

Instantly share code, notes, and snippets.

@maxterry
Created July 22, 2008 15:51
Show Gist options
  • Save maxterry/1100 to your computer and use it in GitHub Desktop.
Save maxterry/1100 to your computer and use it in GitHub Desktop.
open web functions
aif = function(_) {
_ = Array.slice(arguments)
return(each(_, function(i) {
if(i % 2 == 0 && !!this) {
return(fn.apply(this))
}
}))
}
function() {
return(args.caller())
}
/**
* There are a slew of subtly different modes of iteration in JavaScript: for, for...in, while, Array.prototype.forEach,
* Array.prototype.map, jQuery.each, and Prototype.js's Array.each. It currently only works with Arrays, but should also support
* Objects.
*
* @example
each([1,2,3], function() {
return(this) // [1,2,3]
})
* @example
(function() {
return(
each(["a", "b", "c"], function(i, _) {
return([i, _]) // [[0,"a"], [1, "b"], [2, "c"]]
})
)
})()
*
*/
each = function(xs, fn) {
for (var it=[], i=0, l=xs.length; i<l; i++) {
it.push(fn.apply(xs[i], xs[i], i))
}
return(
it.length?
it.length == 1 ?
it[0] :
it :
[]
)
}
/*
LispScript
started by maxwell terry
20080722
ancestors:
http://www-formal.stanford.edu/jmc/recursive/recursive.html
http://lib.store.yahoo.net/lib/paulgraham/jmc.lisp
http://javascript.crockford.com/little.js
*/
function cond(/*args*/) {
var args = Array.prototype.slice.call(arguments)
for (var i in args) {
//:
if (args[i] )
}
}
function lisp(x) {
x = lisp(x)
return {
quote: return (function() {
// x
})(),
atom: return (function() {
return typeof x == 'string' || typeof x == 'number' || typeof x == 'boolean'
})(),
eq: function(y) {
return x === y
},
cons: function(y) {
return [x, y]
},
car: x[0],
cdr: x[1],
null: x === [],
and: function(y) {
return cond(x, cond((y, true), (true, [])),
true, [])
},
not: cond(x, [],
true, true),
append: function(y) {
return cond(x.null, y,
true, cons(x.car, x.cdr.append(y)))
},
list: function(y) {
return x.cons(y.cons())
},
pair: function(y) {
return cond(x.null.and(y.null), [],
x.atom.not.and(y.atom.not),
x.car.list(y.car).cons(x.cdr.pair(y.cdr)))
},
assoc: function(y) {
return cond(y.caar.eq(x), y.cadar,
true, x.assoc(y.cdr))
}
}
eval = function(e, a) {
e = lisp(e)
(cond
e.atom, e.assoc(a),
e.car.atom,
cond(
e.car.eq(quote("quote")), e.cadr,
e.car.eq(quote("atom")), eval(e.cadr, a).atom(), //: eval needs lisp methods
e.car.eq(quote("eq")), eval(e.cadr, a).eq(eval(e.caddr, a)),
e.car.eq()
))
// (defun eval. (e a)
// (cond
// ((atom e) (assoc. e a))
// ((atom (car e))
// (cond
// ((eq (car e) 'quote) (cadr e))
// ((eq (car e) 'atom) (atom (eval. (cadr e) a)))
// ((eq (car e) 'eq) (eq (eval. (cadr e) a)
// (eval. (caddr e) a)))
// ((eq (car e) 'car) (car (eval. (cadr e) a)))
// ((eq (car e) 'cdr) (cdr (eval. (cadr e) a)))
// ((eq (car e) 'cons) (cons (eval. (cadr e) a)
// (eval. (caddr e) a)))
// ((eq (car e) 'cond) (evcon. (cdr e) a))
// ('t (eval. (cons (assoc. (car e) a)
// (cdr e))
// a))))
// ((eq (caar e) 'label)
// (eval. (cons (caddar e) (cdr e))
// (cons (list. (cadar e) (car e)) a)))
// ((eq (caar e) 'lambda)
// (eval. (caddar e)
// (append. (pair. (cadar e) (evlis. (cdr e) a))
// a)))))
//
}
evcon = function(c, a) {
// (defun evcon. (c a)
// (cond ((eval. (caar c) a)
// (eval. (cadar c) a))
// ('t (evcon. (cdr c) a))))
}
evlis = function(m, a) {
// (defun evlis. (m a)
// (cond ((null. m) '())
// ('t (cons (eval. (car m) a)
// (evlis. (cdr m) a)))))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment