Skip to content

Instantly share code, notes, and snippets.

Problem

var cache = {};

var _gen = function(name)
{
  if(cache.hasOwnProperty(name))
    return cache[name]; 

// ...

(function($G)
{
var OldRegExp = $G.RegExp;
var _createHashTable =
Object.create && function()
{
return Object.create(null);
} ||
{}.__proto__ && function()
(function($G)
{
var _extend =
Object.getOwnPropertyNames && function(from, to)
{
var keys = Object.getOwnPropertyNames(from);
var i = keys.length; while(i--)
{
var key = keys[i];
var a = new MutableValue(
{
__call__: function(b)
{
this = b;
};
}
);
a(1); // really a = 1

Lazy evaluation

Goals

  • Provide easy transparent lazy evaluation pattern with memorization for values itself(not object properties)

Examples

` var _fn = function(a, b, c) { var d = new Evaluator(

(function($G)
{
var defaultThis = (function(){ return this; })();
var Vector = function(x, y, z)
{
// cast
if(this === defaultThis)
{
return Vector.prototype._calc(x);
Object.prototype.__defineGetter__ = function(name, _fn)
{
var self = this;
var _wrapper = function()
{
return _fn.call(self);
};
this[name] =
{
struct HashTable // Open addressing Hash Table with linear probing
{
int entriesCount;
int id;
HashTableEntry entries[0];
};
void HashTable::_set(Const String key, Value& value)
{
// if key is new
Const Value& HashTable::_lookup(Const String& key)
{
int hash = _hash(key);
int p = hash%entriesCount;
while(entries[p].hash == hash && p < entriesCount)
{
if(_stringCompare(entries[p].key, key))
return entries[p].value;
String String::slice(Int begin, Int end)
{
String a;
// calculate real begin and end
a.begin = buffer->begin + begin;
a.length = end - begin;
a.buffer = buffer;
buffer->refCount++;