Skip to content

Instantly share code, notes, and snippets.

@svanellewee
svanellewee / gist:c1f056800a16e189e63c
Created July 18, 2014 06:27
SO Clojure Increment Counter
;;For http://stackoverflow.com/questions/24798831/clojure-increment-a-counter
(ns hack1
;; (:use clojure.contrib.command-line)
)
(def collection-x ['({:customer_id "111", :product_id "222"})
'({:customer_id "333", :product_id "444"}{:customer_id "555", :product_id "666"}{:customer_id "777", :product_id "888"})] )
@svanellewee
svanellewee / debugging_monads.js
Last active August 29, 2015 14:04
Trying to understand Monads using javascript
// borrowed from https://blog.jcoglan.com/2011/03/11/promises-are-the-monad-of-asynchronous-programming/
// also http://blog.jcoglan.com/2011/03/05/translation-from-haskell-to-javascript-of-selected-portions-of-the-best-introduction-to-monads-ive-ever-read/
// and http://blog.sigfpe.com/2006/08/you-could-have-invented-monads-and.html
var pp = console.log
var f = function(x) { return x*3 } // a "Float" value
var g = function(x) { return x+10 } // a "Float" value
// http://en.wikipedia.org/wiki/Monad_(functional_programming)#Formal_definition
// turns monad type from type
@svanellewee
svanellewee / gist:22332046b164ffc6b1a2
Last active August 29, 2015 14:04
inheritance, instanceof
var pp = console.log
function Base(name) {
this.name = name || "unnamed"
}
Base.prototype.giveName= function() {
return "[["+ this.name +"]]"
}
function DataBase(name) {
Base.call(this,name);
@svanellewee
svanellewee / async_walk.js
Last active August 29, 2015 14:04
Got confused with other people's async walk examples... so created my own.
var fs = require("fs")
var path = require("path"); //extname, resolve
var pp= console.log
var FILEPATH = "/home/stephan/Music"
// verify with "ls ~/Music/ |wc -l" in one folder
function cons(x, y) {
return function(w) { return w(x, y) };
};
function car(z) {
return z(function(x, y) { return x });
};
function cdr(z) {
return z(function(x, y) { return y });
@svanellewee
svanellewee / trampoline1.js
Last active August 29, 2015 14:04
Trampolining
// with recursion:
var results = [1,1];
function recufib(N) {
function getN() {
var a = results[results.length-2]
var b = results[results.length-1];
if(!--N) {
return;
@svanellewee
svanellewee / monadexperiement-unfinished.js
Created August 2, 2014 05:13
lazy monads... "fib monad"
var list_monad = {}
// a -> [a]
list_monad.unit = function (value) {
return [value]
}
// (a -> [a]) -> ([a] -> [a])
list_monad.bind = function(fn) {
return function(values) {
@svanellewee
svanellewee / newobjectcreate.js
Last active August 29, 2015 14:04
New vs Object.create
var pp = console.log
var Person = { name : "Frank Cadillac",
greet: function() { return "Hello world, my name is "+this.name+"!!!";}
}
// does not work!
// Person.prototype.getName = function(){
// return this.name;
// }
@svanellewee
svanellewee / novaronproto.js
Last active August 29, 2015 14:04
Never declare variables on prototypes!
var Foo = function (name) { this.name = name; };
Foo.prototype.data = [1, 2, 3]; // setting a non-primitive property
Foo.prototype.showData = function () { console.log(this.name, this.data); };
var foo1 = new Foo("foo1");
var foo2 = new Foo("foo2");
// both instances use the same default value of data
foo1.showData(); // "foo1", [1, 2, 3]
foo2.showData(); // "foo2", [1, 2, 3]
@svanellewee
svanellewee / inheritance.lua
Created August 11, 2014 13:57
Lua inheritance via metatables. Pretty
local function __define_class(base)
local __classmethods = {} -- define static/classmethods here..
function __classmethods.new ( cls, ...) -- classmethod
local self = setmetatable({}, { __index = cls })
if self.init then
self:init(...)
end
return self
end