Skip to content

Instantly share code, notes, and snippets.

@peterpme
Created March 3, 2014 20:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterpme/9334399 to your computer and use it in GitHub Desktop.
Save peterpme/9334399 to your computer and use it in GitHub Desktop.
Recent Work with Javascript Experiments
console.log("========================");
//Closures
(function(x) {
return function(y) {
return function(z) {
console.log("x "+x);
console.log("y "+y);
console.log("z "+z);
console.log(x + y + 3);
return x + y + z;
};
};
})(1)(2)(3);
console.log("========================");
//function X Closures
(function(x) {
console.log(x);
return (function(y){
console.log(y+x);
return x+y;
});
})(1)(2);
console.log("========================");
var num = function jeff(x) {
return (function(y){
return x + y;
});
};
var num2 = num(2);
var num3 = num2(3);
console.log("num2: "+num2);
console.log("num3: "+num3);
console.log("========================");
(function(w){
console.log(w);
return (function(x){
console.log(x);
return (function(y){
console.log(y);
return (function(z){
console.log(z);
console.log(w+x+y+z);
return w +x + y + z;
});
});
});
})(1)(2)(3)(4);
console.log("========================");
(function (x) {
return function (x,y){
return function (w,z){
return function(w){
return x + y + z;
};
};
};
})();
console.log("========================");
(function (Pi) {
return function(diameter){
console.log(diameter*Pi);
return diameter*Pi;
};
})(3.14)(2);
console.log("========================");
(function (diameter) {
return function(Pi){
console.log(diameter*Pi);
console.log(diameter);
return diameter*Pi;
};
})(2)(3.14);
console.log("========================");
(function (diameter) {
var Pi = 3.14;
console.log(diameter * Pi);
return diameter * Pi;
})(3);
console.log("========================");
(function (d) {
var calc = function (diameter) {
var Pi = 3.14;
console.log(diameter * Pi);
return diameter * Pi;
};
console.log ("Circumference: " +calc(d));
return "Circumference: " + calc(d);
})(3);
console.log("========================");
(function (d) {
var Pi = 3.14,
calc = function (diameter) {
console.log(diameter * Pi);
return diameter * Pi;
};
return "Circumference: " + calc(d);
})(4);
console.log("========================");
function foobar () {
(function() {
var foo = 'foo',
bar = 'bar';
console.log(foo +" " + bar);
})();
}
foobar();
console.log("========================");
var bindingName = function actualName() {
};
console.log(bindingName.name);
console.log("========================");
// naming of variables, even is bound within function itself but not outside (great for recursion)
var fn = function even (n) {
if (n===0){
return true;
}
else return !even(n-1);
};
console.log(fn(5));
console.log(fn(2));
console.log("========================");
// Function Declarations - binds name in environment to a named function
function someName2() {
//...
}
//behaves similiar to:
var someName3 = function someName3(){
// ...
};
// this doesn't work:
/* (function() {
var someName;
return someName;
someName = function someName(){
};
})(); */
// this will work: declare without the var
/* console.log((function() {
return someName;
function someName(){
}
}))(); */
// as if you wrote:
/* (function(){
var someName = function someName(){
// ...
};
return someName;
})(); */
// function declaration CANNOT exist inside of any expression, otherwise it's a function express:
//function declaration:
function trueDat() { return true; }
//function expression (paranthesis)
(function trueDat() { return true; })();
//high order functions - functions that either take functions/arguments or return function/both are high order
/* function repeat(num, fn){
var i, value;
for (i=1;i<num; ++i)
value = fn(i);
return value;
} */
function compose (a,b){
return function(c){
console.log(c);
return a(b(c));
};
}
function addOne (number) {
return number + 1;
}
function doubleOf(number) {
return number *2;
}
function doubleOfAddOne(number) {
return doubleOf(addOne(number));
}
console.log(doubleOfAddOne(5));
// set doubleOfAddTwo to compose, doubleofAddTwo(5) is passed through compose into the return function and then b(c) runs, a(c) runs on the result of b(c)
var doubleOfAddTwo = compose(doubleOf, addOne);
console.log(doubleOfAddTwo(5));
console.log("======================");
// function decorators:
function not (fn) {
return function(argument) {
return !fn(argument);
};
}
function something(x) {
return x !== null;
}
console.log(something(5));
function nothing(x) {
return !something(x);
}
console.log(nothing(4));
var nothing = not(something);
console.log(nothing);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment