Skip to content

Instantly share code, notes, and snippets.

@francescoagati
francescoagati / private_method.coffee
Created July 3, 2011 21:31
an example of private method in coffeescript with body class writable style ruby
class Animal
private_method= -> 1+1
@::sum= -> private_method()
dog=new Animal()
alert dog.sum()
class Fish extends Animal
delfin=new Fish()
@francescoagati
francescoagati / pipe.coffee
Created July 5, 2011 12:12
jQuery:deferred an example of promise chained deferred with pipe and done. Coffeescript and javascript
promise= (fn) -> df= $.Deferred(fn).promise()
df=promise (df) -> setTimeout( (-> df.resolve(1000)), 1 )
df.pipe (x) ->
promise (df) -> setTimeout( (-> df.resolve(x+1000)), 2 )
.pipe (x) ->
promise (df) -> setTimeout( (-> df.resolve(x+1000)), 2 )
.pipe (x) ->
promise (df) -> setTimeout( (-> df.resolve(x+1000)), 2 )
.pipe (x) ->
@francescoagati
francescoagati / annotations.hx
Created July 5, 2011 20:42
Why haxe is a better php (1) - mixin - annotations - array deferencing - closure_and_chaining_function_call
//support for annotations
// https://wiki.php.net/rfc/annotations
@class_annotation({a:1,b:2})
class Main {
@range(1,100)
@francescoagati
francescoagati / autoboxing.hx
Created July 5, 2011 21:04
Why haxe is a better php (2) - autoboxing, enumerations, macros
//support for autoboxing
// https://wiki.php.net/rfc/autoboxing
class Main {
public static function main() {
var hash = new Hash<Dynamic>();
hash.set("key1","value1");
@francescoagati
francescoagati / wrap.coffee
Created July 6, 2011 22:47
wrapping a method of a prototype Object in javascript e coffeescript
class A
say_something: -> alert "hello"
class B extends A
#create new instance of B and call say_something
new B().say_something()
#wrapping method say_something of prototype A
@francescoagati
francescoagati / example.coffee
Created July 17, 2011 01:57
jquery declarative named scope
$.fn.declare.add
'set.color': (color) -> @css({'color':color})
'set.bgcolor': (color) -> @css({'background-color':color})
$("div").declare('set.color','green').declare('set.bgcolor','red')
@francescoagati
francescoagati / alternate.js
Created July 17, 2011 23:20
example of jquery.declare
$.fn.declare.add({
'row.alternate': function(opt) {
this.find("tr.odd")
.css("background-color",opt.odd_color)
.end()
.find("tr.even")
.css("background-color",opt.even_color);
}
});
@francescoagati
francescoagati / fibonacci.lua
Created July 31, 2011 20:51
fibonacci lua vs luajit
-- Code thaked from http://en.literateprograms.org/Fibonacci_numbers_(Lua)?
-- for benchmarking lua and luajit
-- at bottom of files the result of benchmark
-- Copyright (c) 2011 the authors listed at the following URL, and/or
-- the authors of referenced articles or incorporated external code:
-- http://en.literateprograms.org/Fibonacci_numbers_(Lua)?action=history&offset=20081019232035
--
-- Permission is hereby granted, free of charge, to any person obtaining
@francescoagati
francescoagati / chainable.coffee
Created January 5, 2012 09:54
coffeescript: chainable methods
chained_method= (obj,method,fn) -> obj::[method]= ()-> fn.apply(@,arguments);@
class Base
constructor: -> @n=0
chained_method @,'sum', (x) -> @n=@n+x
chained_method @,'minus', (x) -> @n=@n-x
chained_method @,'pp', (x) -> console.log @n
(new Base).sum(3).minus(10).pp()
@francescoagati
francescoagati / Main.hx
Created January 14, 2012 15:53
haxe dependency injection style syringe pimple
class Main {
public static function getInjector() {
var inject=new Injector();
inject.service("logger",function(context) {
trace(context);
return 9999;
});