Skip to content

Instantly share code, notes, and snippets.

@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;
}
@sortega
sortega / ImperativeIndentationCheck.scala
Created January 5, 2014 22:30
Imperative solution to the Python indentation problem
import scala.io.Source
import scala.collection.mutable
object ImperativeIndentationCheck {
def isValid(filename: String): Boolean = isValid(Source.fromFile(filename).getLines.toSeq)
def isValid(lines: Seq[String]): Boolean = {
val scopes = mutable.Stack(0)
for(line <- lines) {
@sortega
sortega / IndentationCheck.scala
Created January 5, 2014 22:32
Functional solution to the Python indentation problem check
import scala.io.Source
object IndentationCheck {
def isValid(filename: String): Boolean = isValid(Source.fromFile(filename).getLines.toSeq)
def isValid(lines: Seq[String]): Boolean = {
val initialAccum: Option[Set[Int]] = Some(Set(0))
lines.map(indentationOf).foldLeft(initialAccum) { (accum, i) =>
accum match {
@sortega
sortega / BerlinClock.scala
Created January 19, 2014 08:49
Berlin clock kata (http://content.codersdojo.org/code-kata-catalogue/berlin-clock/) implemented in Scala when pairing with Alberto. By just letting the tests guide the design and merciless refactoring after every step we got to this terse code.
package berlin
object BerlinClock {
sealed trait Light
case object Off extends Light
case object Yellow extends Light
case object Red extends Light
type Clock = List[List[Light]]
import org.scalacheck.Gen
import org.scalatest.{ShouldMatchers, FlatSpec}
import org.scalatest.prop.PropertyChecks
class AllocatorTest extends FlatSpec with ShouldMatchers with PropertyChecks {
val positives = Gen.posNum[Int]
val vectorOfPositives = Gen.nonEmptyContainerOf[Vector, Int](positives)
"A proportional distribution" should "have the same amounts as weights" in {
@sortega
sortega / naive.hs
Created February 2, 2015 15:26
Naïe pick/guess implementation
import System.Environment
import System.Exit
import System.Random
import System.IO
data Feedback = Smaller | Bigger | Done deriving (Show, Eq)
type Guess = Int
type Range = (Int, Int)
range = (1, 100)
@sortega
sortega / warrior.hs
Created February 23, 2015 22:58
Improvement over naive.hs
import Control.Monad.Random
import Data.List
import System.Environment
import System.Exit
import System.Random
import System.IO
data Feedback = Smaller | Bigger | Done deriving (Show, Eq)
type Guess = Int
type Range = (Int, Int)
@sortega
sortega / WithScalaz.scala
Last active August 29, 2015 14:22
Briconsejo option
import scalaz.syntax.std.boolean._
boolExpression.option(computingSomethingComplicated)
@sortega
sortega / Game.scala
Last active August 29, 2015 14:26
Bowling game score. Imperative, OO style
package bowling
class Game {
trait Line {
def addRoll(pins: Int): Unit
def score: Int
}
class Frame(next: Line) extends Line {