Skip to content

Instantly share code, notes, and snippets.

@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)))
(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 / 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)]
@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 / 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 / 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 / NameInverter.scala
Created June 22, 2013 16:36
NameInverter in Scala by me
package cleancode
object NameInverter {
def apply(name: String): String =
swapFirstAndLast(splitName(name).dropWhile(isHonorific)).mkString(" ")
def swapFirstAndLast(parts: List[String]) = parts match {
case first :: last :: postNominals => last + "," :: first :: postNominals
case _ => parts
@sortega
sortega / Tree.scala
Created July 6, 2013 23:54
Xmas tree kata
package xmas
object Tree {
def apply(n: Int) = {
val width = n * 2 - 1
def center(line: String) = {
val padding = " " * ((width - line.length) / 2)
padding ++ line ++ padding
}.mkString
def line(row: Int) = row * 2 + 1
@sortega
sortega / nullitis.pl
Created July 13, 2013 15:39
Quick and dirty script to measure how null-checking prevalence in Java programs.
#!/usr/bin/env perl
use strict;
use warnings;
sub locs {
my $repo = shift;
`sloccount $repo | grep ^java` =~ /^java:\s+(\d+)/;
return $1;
}