Skip to content

Instantly share code, notes, and snippets.

View GeertVL-zz's full-sized avatar
💭
Working full time in Azure technologies

Geert Van Laethem GeertVL-zz

💭
Working full time in Azure technologies
View GitHub Profile
Object.extend(Number.prototype, (
function() {
function succ() {
return this + 1;
}
function times(iterator, context) {
$R(0, this, true).each(iterator, context);
return this;
}
// shortened to fit on this slide!
// Part 1.
// Implement a function prototype extension that caches function results for
// the same input arguments of a function with one parameter.
//
// For example:
// Make sin(1) have the result of Math.sin(1), but use a cached value
// for future calls.
//
// Part 2.
// Use this new function to refactor the code example.
<script>
var Person = function(name) {
this.name = name;
this.getName = function() {
return this.name;
};
}
var thomas = new Person('Thomas');
var amy = new Person('Amy');
<script>
// Part 1
// Implement a function prototype extension that caches function results for
// the same input arguments of a function with one parameter.
Function.prototype.withCaching = function withCaching() {
if (!this.cache) { this.cache = {}; }
var method = this;
return function(input) {
if (typeof method.cache[input] == "undefined") {
@GeertVL-zz
GeertVL-zz / gist:3037945
Created July 3, 2012 05:49
Erlang 8.11.2
-module(ring).
-export([clasp/0,shackle/1,send/1]).
clasp() ->
spawn(fun() -> loop([]) end).
shackle(Pid) ->
spawn(fun() -> loop(Pid) end).
send(Pid) ->
@GeertVL-zz
GeertVL-zz / gist:3049213
Created July 4, 2012 19:44
Erlang 8.11.2.2
-module(circle).
-export([start/1]).
start(Msg) ->
CPid = clasp(),
Pid1 = shackle(CPid, CPid),
Pid2 = shackle(Pid1, CPid),
Pid3 = shackle(Pid2, CPid),
send(Pid3,Msg).
@GeertVL-zz
GeertVL-zz / gist:3053553
Created July 5, 2012 13:03
Erlang 8.11.2.3 first step (no counter and refactored)
-module(ring2).
-export([start/1]).
start(Msg) ->
Pid1 = ring(),
Pid2 = ring(),
Pid3 = ring(),
Pid4 = ring(),
shackle(Pid1, Pid2),
shackle(Pid2, Pid3),
@GeertVL-zz
GeertVL-zz / gist:3053799
Created July 5, 2012 13:55
Erlang 8.11.2.3 (not refactored)
-module(ring2).
-export([start/2]).
start(Msg,Max) ->
Pid1 = clasp(Max),
Pid2 = ring(),
Pid3 = ring(),
Pid4 = ring(),
shackle(Pid1, Pid2),
shackle(Pid2, Pid3),
@GeertVL-zz
GeertVL-zz / gist:3053984
Created July 5, 2012 14:27
Erlang 8.11.2.3 (refactor round 1)
-module(ring2).
-export([start/2]).
start(Msg,Max) ->
Head = clasp(Max),
shackle(shackle(shackle(shackle(Head))), Head),
send(Head, Msg).
shackle(LPid) ->
RPid = ring(),
@GeertVL-zz
GeertVL-zz / ring.erl
Created July 5, 2012 14:59
Erlang 8.11.2.3 (refactor round 2)
-module(ring2).
-export([start/3]).
start(Msg,Count,Max) ->
Head = clasp(Max),
connect(Head, Count),
send(Head, Msg).
shackle(LPid, Count) when Count > 1 ->
RPid = spawn(fun() -> loop(0,-1) end),