Skip to content

Instantly share code, notes, and snippets.

View jmgimeno's full-sized avatar

Juan Manuel Gimeno jmgimeno

View GitHub Profile
@jmgimeno
jmgimeno / group.clj
Created May 3, 2011 05:43 — forked from shuaybi/group.clj
Multi-level Grouping and Computations
(ns pas.tree
(:use [clojure.pprint :only [pprint]]))
(def reg-cntry-list
{"America" ["USA" "Canada" "Mexico" "Venezuala" "Brazil" "Argentina" "Cuba"]
"Asia" ["India" "Pakistan" "Singapore" "China" "Japan" "Sri Lanka" "Malaysia"]
"Europe" ["UK" "Germany" "France" "Italy" "Belgium" "Turkey" "Finland"]
"Middle East" ["Saudi Arabia" "Bahrain" "UAE" "Kuwait" "Yemen" "Qatar" "Iraq"]
"Africa" ["Libya" "Tanzania" "South Africa" "Kenya" "Ethiopia" "Morocco" "Zimbabwe"]})
@jmgimeno
jmgimeno / gist:966404
Created May 11, 2011 12:55
Get All Nested Map Values (N levels deep)
(def my-data {"Jay"
{"clojure"
{:name "Jay", :language "clojure", :enjoys true},
"ruby"
{:name "Jay", :language "ruby", :enjoys true}}
"Jon"
{"java"
{:name "Jon", :language "java", :enjoys true}}} )
(defn nth-vals* [a i m]
@jmgimeno
jmgimeno / gist:1012607
Created June 7, 2011 16:31
Use pygments to colorize code and make png file
"Use pyments to output syntax highlighted PNG files of Python code"
# http://paste.pocoo.org/show/402065/
import glob
import re
import sys
import os
from pygments import highlight
from pygments.lexers import PythonLexer
@jmgimeno
jmgimeno / interpolate.js
Created November 26, 2011 19:47
Python-like interpolation for JavaScript
/**
* Provides python-like string interpolation.
* It supports value interpolation either by keys of a dictionary or
* by index of an array.
*
* Examples::
*
* interpolate("Hello %s.", ["World"]) == "Hello World."
* interpolate("Hello %(name)s.", {name: "World"}) == "Hello World."
* interpolate("Hello %%.", {name: "World"}) == "Hello %."
@jmgimeno
jmgimeno / sat.clj
Created January 1, 2012 19:31
SAT in clojure.core.logic
;Code from http://peteriserins.com/2011/12/23/sat-in-clojure-core-logic.html
(defn and-rewrite [expr]
`(conde
(~(goalify (second expr))
~(goalify (second (rest expr))))))
(defn or-rewrite [expr]
`(conde
(~(goalify (second expr)))
@jmgimeno
jmgimeno / test-private-fns.clj
Created January 2, 2012 09:04
Testing private methods
(defmacro with-private-fns [[ns fns] & tests]
"Refers private fns from ns and runs tests in context."
`(let ~(reduce #(conj %1 %2 `(ns-resolve '~ns '~%2)) [] fns)
~@tests))
(with-private-fns [org.foo.bar [fn1 fn2]]
(deftest test-fn1..)
(deftest test-fn2..))
@jmgimeno
jmgimeno / life.clj
Created January 9, 2012 12:36
Conway's Game of Life
; From: http://clj-me.cgrand.net/2011/08/19/conways-game-of-life/
(defn neighbours [[x y]]
(for [dx [-1 0 1] dy (if (zero? dx) [-1 1] [-1 0 1])]
[(+ dx x) (+ dy y)]))
(defn step [cells]
(set (for [[loc n] (frequencies (mapcat neighbours cells))
:when (or (= n 3) (and (= n 2) (cells loc)))]
loc)))
@jmgimeno
jmgimeno / data.js
Created January 23, 2012 19:38
Focus + context
function create_time_series(ystart) {
var start = new Date(1990, 0, 1);
var year = 1000 * 60 * 60 * 24 * 365;
return d3.range(0, 20, .02).map(function(x) {
return {
x: new Date(start.getTime() + year * x),
y: (ystart + .1 * (Math.sin(x * 2 * Math.PI))
+ Math.random() * .1) * Math.pow(1.18, x)
+ Math.random() * .1};
});
@jmgimeno
jmgimeno / README.md
Created January 25, 2012 14:22 — forked from mbostock/.block
Focus + Context (via Brushing)

This examples demonstrates how to use D3's brush component to implement focus + context zooming. Click and drag in the small chart below to pan or zoom.

@jmgimeno
jmgimeno / data.csv
Created January 29, 2012 17:29
Moving Histogram
label value
one 25
two 50
three 78
four 44
five 23
six 90
seven 25
eight 16
nine 34