Skip to content

Instantly share code, notes, and snippets.

View diogobaeder's full-sized avatar

Diogo Baeder diogobaeder

View GitHub Profile
/**
Just some helpers to accelerate jQuery object instantiation, when we are using element IDs or classes as references.
It seems stupid, but using these helpers actually boosts the performance a lot.
Try the code below on the jQuery homepage, and see the results by yourself (must have Firebug installed)
*/
// Helper for getting by ID
function $id(id, context) {
/**
* Use the code below as a reference if you're sure you're using
* elements' ID's. In my system, the pure version ended in 243ms while
* the accelerated version ended in 33ms. The speed difference is not
* noticeable for a small number of traversals, though.
*/
jQuery(function(){
(function($, doc){
var nElements = 100, iterations = 10000;
/**
* Little trick to list the MP3 paths
* from the great Jim Beard's web pages
* (one of the best Jazz composers I've known).
* Enjoy.
*/
var pattern = /Beard\smp3s\/.+\.mp3/,
base = "http://www.jimbeard.com/",
paths = [];
$$('a').forEach(function(el, i){
// Just a simple array division to share with a friend
function divideArray(array, parts) {
if (parts == 0) return [];
var length = array.length,
blockSize = ((length + (length % parts)) / parts),
blocks = [], i;
for (i = 0; i < length; i += blockSize) {
blocks.push(array.slice(i, i + blockSize));
}
return blocks;
/*
A simple helper for cleaning MongoDB collections in NodeJS, using mongoose.
They act as setup/teardown cleaners
*/
// lib/tools.js
exports.chainedCalls = function() {
var funcs = Array.prototype.slice.call(arguments, 0);
function callNext() {
if (funcs.length) {
@diogobaeder
diogobaeder / factorialdigitssum.py
Created June 22, 2011 16:57
Soma dos dígitos do fatorial
from math import factorial
from operator import add
def factorial_digits_sum(number):
return reduce(
add,
[int(string_digit) for string_digit in str(
factorial(number)
)]
@diogobaeder
diogobaeder / tornado-zmq.py
Created July 11, 2011 06:24
Little dumb experiment with Tornado and 0MQ
# This was a little experiment I've put together in some minutes while in TDC2011 (The Developers Conference), at São Paulo
# It's just a silly almost-Hello-World, just to show the assynchronous communication working with non-blocking I/O through event loops, mixed with a simple HTTP server
# First, put this on a file called 'server.py', and run:
#$ ./server.py 8001 &
#$ ./server.py 8002 &
#$ ./server.py 8003 &
#!/usr/bin/env python
@diogobaeder
diogobaeder / frange.py
Created September 24, 2011 23:16
Floating-point range generator (IEEE-754-proof)
#!/usr/bin/env python
'''
This is an attempt of an alternative to
http://code.activestate.com/recipes/577068-floating-point-range/
but generating the expected floating-point elements in the range,
avoiding floating-point arithmetic problems.
Currently it works rather well, although without the original
validation steps (which seem to be overengineering to me), considering
@diogobaeder
diogobaeder / sorter.py
Created May 27, 2012 06:45
Quicksort memorization
#!/usr/bin/env python
'''
Just another quicksort implementation, to see if I memorized the algorithmn.
Apparently, yes. :-)
Gotchas during the implementation:
- forgot to check min and max in the beginning, got IndexError
- choosing nonsense as pivot led me to almost-sorted list (worked when
I stopped being stupid)
'''
@diogobaeder
diogobaeder / observer.py
Created June 8, 2012 06:02
Observer example for GOOS discussion list
#!/usr/bin/env python
'''
Just a silly example for an Observer implementation using the "push event" method. See discussion about it:
https://groups.google.com/forum/?fromgroups#!topic/growing-object-oriented-software/KwJPxUZ0l_M
'''
class OrderTrackingEvent(object):
def __init__(self, order_number, status):
self.order_number = order_number
self.status = status