Skip to content

Instantly share code, notes, and snippets.

@egonelbre
egonelbre / gist:2374904
Created April 13, 2012 07:37
golang scope function test
package main
import "errors"
func actionOk() (int, error) {
return 1, nil
}
func actionErr() (int, error) {
return 0, errors.New("err")
@egonelbre
egonelbre / gist:3259101
Created August 4, 2012 18:13
terenary operator
// testing place
http://egonelbre.com/js/bigram/
// test input
a = c ? t + 4 : f + 3 ? a : b
// replace the tokens on the right side with these:
// version: C ?( T ): F
// V1
public class Foo {
}
public class KungFoo extends Foo {
public KungFoo(string owner){
// ...
}
}
@egonelbre
egonelbre / gist:3437746
Created August 23, 2012 15:27
State Machine
function Machine(first, $){
var cur = {}, next = $[first];
var self = {
go : function(to){ next = next ? next : ($[to] || $.undefined); },
trigger : function(event){ var t = cur.tx && cur.tx[event]; t && self.go(t); }
};
return function(){
if(next){
cur.exit && cur.exit.call(self);
cur = next; next = undefined;
function Machine(first, $){
var cur = {}, next = $[first];
var self = {
go : function(to){ next = next ? next : ($[to] || $.undefined); },
trigger : function(event){ var t = cur.tx && cur.tx[event]; t && self.go(t); }
};
return function(){
if(next){
cur.exit && cur.exit.call(self);
cur = next; next = undefined;
@egonelbre
egonelbre / gist:4115360
Created November 20, 2012 01:28
go2js Interface pesudo implementation
function Interface(decl){
this.decl = decl;
this.impl = {}; // map funcname -> type
}
Interface.prototype = {
check : function(typedef){
if(typedef.uuid === undefined){
typedef.uuid = GenerateGloballyUniqueId();
}
function sayExt(obj){
return obj.say();
}
Zoo = Context(function(player){
Lion = Role({
say : function(){ return 'meow'; }
});
function sayInt(obj){
@egonelbre
egonelbre / dci.js
Last active December 10, 2015 01:29
// allows easily making an Actor with a Role
// if it's already an actor the context will be attached.
Object.defineProperty(Object.prototype, "as", {enumerable: false, value : function(role){
// this will add a role to the object
if(this.__isActor__) {
this.__addRole__(role);
return this;
}
// if this is not an actor -
public class Battle<LionPlayer, BearPlayer> {
private LionPlayer Lion;
private BearPlayer Bear;
private void Lion$fight(){
System.out.println(Lion.getName() + ": meow...");
};
private void Bear$fight(){
System.out.println(Bear.getName() + ": grrr...");
Battle = Context(function(bearPlayer, lionPlayer){
Bear = bearPlayer;
Lion = lionPlayer;
Bear.fight();
Lion.touch();
Lion.fight();
},{
Lion : {
fight : function(){
console.log(this.name + " : meow");