Skip to content

Instantly share code, notes, and snippets.

View lazywithclass's full-sized avatar

Alberto Zaccagni lazywithclass

View GitHub Profile
@lazywithclass
lazywithclass / gist:1378190
Created November 19, 2011 00:26
Invert a string (the inefficient way)
public static String invert(String invertMe) {
StringBuilder output = new StringBuilder();
for (int i=invertMe.length(); i>0; i--) {
output.append(invertMe.charAt(i-1));
}
return output.toString();
}
@lazywithclass
lazywithclass / gist:1388891
Created November 23, 2011 14:59
xfce settings
[SeatDefaults]
greeter-session=lightdm-gtk-greeter
user-session=xubuntu
@lazywithclass
lazywithclass / gist:1411161
Created November 30, 2011 21:56
Array covariance
public class Test {
class Parent {}
class Child extends Parent {}
public void accept(Parent[] parent) {
System.out.println("accepted");
}
public static void main(String[] args) {
@lazywithclass
lazywithclass / gist:1506587
Created December 21, 2011 16:14
Quick each (took from here https://gist.github.com/500145 without spaces)
// The `quickEach` method will pass a non-unique jQuery instance
// to the callback meaning that there will be no need to instantiate
// a fresh jQuery instance on each iteration. Most of the slow-down
// inherent in jQuery's native iterator method (`each`) is the constant
// need to have access to jQuery's methods, and so most developers
// see constructing multiple instances as no issue... E.g.
// $(...).each(function(){ $(this)... $(this)... $(this)... });
// A better approach would be `quickEach`.
jQuery.fn.quickEach = (function(){
jQuery.fn.quickEach = (function() {
var jq = jQuery([1]);
return function(c) {
var i = -1, el, len = this.length;
try {
while (++i < len && (el = jq[0] = this[i]) && c.call(jq, i, el) !== false);
} catch (e) {
delete jq[0];
throw e;
}
@lazywithclass
lazywithclass / gist:1511267
Created December 22, 2011 18:13
Builder pattern with compile time check
public class Complex {
private Complex() {}
public static void main(String[] args) {
new ComplexBuilder()
.setFirst("first")
.setSecond("second")
.setThird( "third" )
.build();
@lazywithclass
lazywithclass / gist:1582626
Created January 9, 2012 11:50
TDD functions for emacs
(global-set-key (kbd "C-c C-r") 'run-mocha)
(defun run-mocha()
"Runs all the tests in the current buffer"
(interactive)
(let* (command result exit-value)
(setq command (concat "mocha -r should " (buffer-name)))
(setq exit-value (shell-command command))
(color-modeline exit-value)))
@lazywithclass
lazywithclass / gist:1713723
Created January 31, 2012 23:19
POST with @mikeal request
request({
method: "POST",
uri: "http://example.com/action",
json: {
foo: "foo",
bar: "bar"
}
}, function (error, response, body) {
if(response.statusCode === 201){
console.log("posted");
@lazywithclass
lazywithclass / gist:1748552
Created February 6, 2012 00:26
Logging in nodejs
//I've launched my app as so: NODE_ENV=development nodemon server.js
var Log = new require("log"),
log = new Log(process.env.NODE_ENV === "production" &&
"error" || "debug");
@lazywithclass
lazywithclass / gist:1790981
Created February 10, 2012 17:08
TDD functions revised (added chance to run a single test)
(defun mocha-run(test-file)
"Runs all the tests in the passed file"
(interactive "b")
(let* (command result exit-value)
(setq command (concat "mocha -r should --globals i --timeout 10000 " test-file))
(setq exit-value (shell-command command))
(color-modeline test-file)))
(defun color-modeline(test-file)
"Colors the modeline, green success red failure"
(interactive)