Skip to content

Instantly share code, notes, and snippets.

@israelst
israelst / iterator.js
Created September 25, 2011 04:49
An example of how to maintain state with closures
function create_iterator(list){
var index = 0;
return function(){
if(index == list.length){
return false;
}else{
return list[index++];
}
}
}
@israelst
israelst / lazy_fib.js
Last active September 27, 2015 08:27
A lazy fibonacci example written in javascript
function create_fibonacci(){
var n1 = -1,
n2 = 1,
n = 0;
return function(){
n = n1 + n2;
n1 = n2;
n2 = n;
return n;
}
@israelst
israelst / template.php
Created November 5, 2011 00:27
Templating PHP
<?php
function apply_template($template_path, $vars = array()){
ob_start();
extract($vars);
include($template_path);
$ob_contents = ob_get_contents();
ob_end_clean();
return $ob_contents;
}
@israelst
israelst / tester.php
Created November 5, 2011 10:00
Mini tester
<?php
function test($expected, $obtained){
if($expected != $obtained){
return "Falha, $expected é diferente de $obtained";
}else{
return "Ok";
}
}
@israelst
israelst / division.rb
Created March 15, 2012 03:29
Division, Papadimitriou Book
# this one comes from the book
def divide(x, y)
return {:q => 0, :r => 0} if x==0
result = divide(x/2, y)
result[:q] *= 2
result[:r] *= 2
result[:r] += 1 if x.odd?
if result[:r] >= y
result[:r] -= y
result[:q] += 1
#!/bin/bash
p(){
cd $(find ~/Projects/ -name $1)
}
@israelst
israelst / merge-without-slice.py
Created May 5, 2012 11:21
Merging two ordered lists
def merge(x, y):
if len(x) == 0:
return y
if len(y) == 0:
return x
last = y.pop() if x[-1] < y[-1] else x.pop()
# 'merged' is required because the append method is in place
merged = merge(x, y)
merged.append(last)
def max_sum(vector):
return cubic_solution(vector)
def cubic_solution(vector):
max_sum = 0
for i in range(len(vector) + 1):
for j in range(i):
partial_sum = sum(vector[j:i])
max_sum = max(partial_sum, max_sum)
return max_sum
@israelst
israelst / csv2json.py
Last active December 25, 2015 09:49
From csv to json in one twitt
import csv, json
f = open('sample.csv', 'r')
reader = csv.DictReader(f, fieldnames=("col1","col2"))
print json.dumps(list(reader))
@israelst
israelst / insights.html
Last active August 29, 2015 14:20
Show facebook post data with d3.parcoords
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Curto Café Facebook Page</title>
<script src="preprocess.js" charset="utf-8"></script>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script src="http://syntagmatic.github.io/parallel-coordinates/d3.parcoords.js" charset="utf-8"></script>
<link href="http://syntagmatic.github.io/parallel-coordinates/d3.parcoords.css" rel="stylesheet" />
</head>