Skip to content

Instantly share code, notes, and snippets.

@sortega
sortega / NameInverter.java
Created June 22, 2013 16:32
Name inverter in Java by Uncle Bob
package nameInverter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class NameInverter {
String invertName(String name) {
if (name == null || name.length() <= 0)
return "";
@sortega
sortega / osmos.core.clj
Created June 12, 2013 09:02
Osmos problem from Google Code Jam used as a programming kata for the FP community of TID.
(ns osmos.core)
(defn absorb [[size additions] mote]
(loop [size size
additions additions]
(cond
(< size 2) [size Double/POSITIVE_INFINITY]
(< mote size) [(+ size mote) additions]
:else (recur (+ size (dec size))
(inc additions)))))
@sortega
sortega / Distro.scala
Created April 30, 2013 13:38
Discrete probability distribution monad in Scala.
import scala.collection.immutable
class Distro[T](probs: Seq[(T, Double)]) {
require((probs.map(_._2).sum - 1.0d).abs < Distro.delta)
val probMap: Map[T, Double] = mergeProbs(probs)
def map[B](f: T => B): Distro[B] =
new Distro(for ((event, prob) <- probMap.toSeq) yield (f(event), prob))
def flatMap[B](f: T => Distro[B]): Distro[B] =
@sortega
sortega / pyindent.test.core.clj
Created April 3, 2013 10:03
Test for last gist
(ns pyindent.test.core
(:use midje.sweet
pyindent.core))
(fact "Tabs increment to the next multiple of 8"
(tabulate 0) => 8
(tabulate 8) => 16
(tabulate 7) => 8
(tabulate 9) => 16)
@sortega
sortega / pyindent.core.clj
Created April 3, 2013 09:48
Check python indentation rules
(ns pyindent.core)
(def tabsize 8)
(defn tabulate [column]
(- (+ column tabsize)
(rem column tabsize)))
(defn indent-level [^String line]
(let [[_ spaces] (re-matches #"^(\s*).*" line)]
(ns berlin.core)
(defn composed-lights [amount]
[(quot amount 5)
(rem amount 5)])
(defn seconds-lights [ss]
(if (even? ss) 1 0))
(defn lights [[hh mm ss]]
(ns berlin.core)
(defn composed-lights [amount]
[(quot amount 5)
(rem amount 5)])
(defn seconds-lights [ss]
(if (even? ss) 1 0))
(defn lights [[hh mm ss]]
@sortega
sortega / counted_fun.clj
Created November 27, 2012 11:58
Decorate a function to know how many times is called
(defn counted [f]
(let [times (atom 0)] ; thread safe counter
(with-meta (fn [& args]
(swap! times inc)
(apply f args))
{:times times}))) ; associated as metadata
(defn times-runned [f]
@(:times (meta f)))
@sortega
sortega / trenes.clj
Created May 9, 2012 18:19
Some cache simulation code to play around
(ns trenes
(:require [clojure.string :as str]))
(defn empty-cache [c]
{:capacity c
:values {}
:time 0
:misses 0
})
@sortega
sortega / force_feed_macports
Created April 3, 2012 12:29
Force feed macports
Just copy the archive to /opt/local/var/macports/distfiles/{port_name}/