Sarah castille's new website
www.sarahcastille.com
Stuff I (Tiest Vilee) am interested in. I have moved my holiday offerings over to http://toastsholiday.blogspot.com/ for now. And here's my home page
{ my_alert: alert_string | alert(alert_string); }
[map: my_alert onto: \["a","b","c"]]
[node addEvent("click", {event| alert('whatever'); }) ]
function my_alert(alertString) { alert(alertString); }
map$onto(alertString, ["a","b","c"]);
node.addEvent("click", function(event){ alert('whatever'); });
lisp(
reduce(
if$(true, add, sub),
map( add_one, [1,2,3])
)
);
// prints 9
function add_one(a_value) {
return add(1, a_value);
}
function LispExpression(originalExpression, expression_arguments) {
this.originalExpression = originalExpression;
this.expression_arguments = expression_arguments;
}
LispExpression.prototype.evaluate = function evaluate(){
return this.originalExpression.apply(null, this.expression_arguments);
}
function lisp(lispExpression){
if( lispExpression instanceof LispExpression) {
return lispExpression.evaluate();
}
return lispExpression;
}
function lisp_function(the_function, arguments) {
return new LispExpression(
the_function,
arguments
);
}
function reduce(reducer, argument_list) {
return lisp_function(
function reduce() {
if(arguments.length > 0) {
var total = null;
reducer = lisp(reducer);
argument_list = lisp(argument_list);
for( var i=0; i<argument_list.length; i++) {
total = lisp(reducer(total, argument_list[i]));
}
return total;
}
return null;
},
arguments
);
}
function map(mapper, argument_list) {
return lisp_function(
function map() {
if(arguments.length > 1) {
var result = [];
for( var i=0; i<argument_list.length; i++) {
result.push( lisp(mapper(argument_list[i])) );
}
return result;
}
return null;
},
arguments
);
}
function if$() {
return lisp_function(
function(condition, trueExpression, falseExpression) {
if( lisp(condition) ) {
return lisp( trueExpression );
} else if(falseExpression) {
return lisp( falseExpression );
}
return null;
},
arguments);
}
function seq() {
return lisp_function(
function () {
var last_argument = arguments.length-1;
for( var i=0; i<last_argument; i++) {
lisp(arguments[i]);
}
return lisp(arguments[last_argument]);
},
arguments
);
}
function add() {
return reduce(
function add(running_total, value){
return running_total == null ? lisp(value) : running_total + lisp(value);
},
arguments);
}
function sub() {
return reduce(
function add(running_total, value){
return running_total == null ? lisp(value) : running_total - lisp(value);
},
arguments);
}
function print() {
return reduce(
function print(running_total, value){
out.println(lisp(value));
return null;
},
arguments);
}
function printAndReturn(aString, aValue) {
return seq(print(aString), aValue);
}
lisp(if$(false, print("got true"), print("got false")));
// got false
// null
function additup() {
return add(
1,
if$(
false,
printAndReturn("two", 2),
printAndReturn("three",3)),
4);
}
lisp(print(1,2,"fkfkfk"));
// 1
// 2
// fkfkfk
lisp( additup() );
// three
// 8
function add_one(a_value) {
return add(1, a_value);
}
lisp( map(add_one, [1,2,3]) );
// 2,3,4
lisp(
reduce(
if$(true, add, sub),
map( add_one, [1,2,3])
)
);
// 9
function ensure_package(package_name, package_block) {
var package_parts = package_name.split(".");
var package_so_far = this;
for(var i=0; i<package_parts.length; i++) {
var package_part = package_parts[i];
if( ! package_so_far[ package_part ] ) {
package_so_far[ package_part ] = {};
}
package_so_far = package_so_far[ package_part ];
}
if( package_block ) {
package_block( package_so_far );
}
return package_so_far;
}
ensure_package("tiest");
tiest.id = 89
ensure_package("tiest");
alert( tiest.id );
ensure_package("tiest.vilee");
tiest.vilee.rocks = true;
ensure_package("tiest.vilee", function(public_interface) {
var private_interface = {};
private_interface.private_method = function() {
return 'private method';
}
public_interface.public_method = function() {
return 'public method:' + private_interface.private_method();
}
});
alert( tiest.vilee.rocks + ' ' + tiest.vilee.public_method() );
function object(o){
if( !o ) {
o = this;
}
var builder = function(){};
builder.prototype = o;
var result = new builder();
result._prototype = o;
return result;
}
function clone(objectToClone, objectsParent) {
if(!objectToClone) {
objectToClone = this;
}
if(objectToClone._prototype) {
objectsParent = objectToClone._prototype;
}
if(!objectsParent) {
throw "no parent defined for this object";
}
// create new object with prototype
var result = object(objectsParent);
// copy over new stuff
for(var key in objectToClone) {
if( objectToClone.hasOwnProperty(key) ) {
result[key] = objectToClone[key];
}
}
return result;
}
Object.prototype.clone = clone;
Object.prototype.beget = object;