Skip to content

Instantly share code, notes, and snippets.

View nickdesaulniers's full-sized avatar

Nick Desaulniers nickdesaulniers

View GitHub Profile
@nickdesaulniers
nickdesaulniers / gist:4221609
Created December 6, 2012 03:36
temp variable
var urls = ['google.com', 'youtube.com', 'cracked.com'];
var fs = require('fs'),
http = require('http'),
url = require('url'),
path = require('path');
var count = 0;
urls.map(url.parse).forEach(function (url) {
http.get(url, function (res) {
@nickdesaulniers
nickdesaulniers / closure.js
Last active December 11, 2015 03:18
Simple Closure in JavaScript
var x = 5;
function myClosure (y) {
return x + 1;
};
console.log(myClosure(10)); // 6
@nickdesaulniers
nickdesaulniers / shadowing.js
Created January 15, 2013 04:57
Shadowing a Variable
var x = 5;
function myClosure (x) {
return x + 1;
};
console.log(myClosure(10)); // 11
@nickdesaulniers
nickdesaulniers / broken_closure.rb
Last active December 11, 2015 03:18
Naive attempt at closure in Ruby
x = 5
def my_closure y
x + 1
end
puts my_closure 10 # NameError: undefined local variable or method `x' for main:Object
@nickdesaulniers
nickdesaulniers / closure.rb
Last active December 11, 2015 03:18
Ruby stabby lambda closure
x = 5
my_closure = -> x do
x + 1
end
puts my_closure.call 10 # 6
@nickdesaulniers
nickdesaulniers / broken_closure.rs
Last active December 11, 2015 03:18
Naive closure implementation in Rust
fn main () {
let x: int = 5;
fn my_closure (y: int) {
x + 1;
}
io::println(my_closure(10)); // error: attempted dynamic environment-capture, unresolved name: x
}
@nickdesaulniers
nickdesaulniers / verbose_closure.rs
Last active December 11, 2015 03:18
Closure in Rust (more robust than needed)
fn main () {
let x: int = 5;
let my_closure = |_: int| -> int {
x + 1
};
io::println(fmt!("%d",my_closure(10))); // 6
}
@nickdesaulniers
nickdesaulniers / succint_closure.rs
Last active December 11, 2015 03:18
Closure in Rust (type inferred)
fn main () {
//let x: int = 5;
let x = 5;
//let my_closure = |_: int| -> int {
let my_closure = |_| {
x + 1
};
io::println(fmt!("%?",my_closure(10))); // 6
@nickdesaulniers
nickdesaulniers / function_pointers.c
Last active December 11, 2015 19:08
C Function Pointer Alternate Syntax
// compiled with:
// clang -Wall -Wextra function_pointers.c -o function_pointers
#include "stdio.h"
void usual_syntax (void (*fn) (int x)) {
puts("usual syntax start");
fn(1);
puts("usual syntax end");
}
@nickdesaulniers
nickdesaulniers / function_pointer
Created January 27, 2013 02:40
C Function Pointer Alternate Syntax Output
hello world
usual syntax start
hello 1
usual syntax end
other syntax start
hello 2
other syntax end