Skip to content

Instantly share code, notes, and snippets.

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 {
@sortega
sortega / SimpleSieve.scala
Created August 14, 2015 15:35
Naïve parallelization of the Eratosthenes sieve using Akka
import akka.actor._
object SimpleSieve extends App {
val MaxNumber = 500000
case object NoMoreCandidates
class Filter extends Actor {
@sortega
sortega / BackPressureSieve.scala
Created August 14, 2015 16:02
Parallel Eratosthenes sieve with back pressure
import akka.actor._
object BackPressureSieve extends App {
val DefaultChunkSize = 1024
val MinChunkSize = DefaultChunkSize * 3 / 4
val MaxNumber = 500000
case object GetCandidates
case class Candidates(values: Vector[Int])
@sortega
sortega / Sieve.scala
Created August 14, 2015 16:09
Sequential Eratosthenes sieve
class Sieve(maxNumber: Int) {
private val candidates = Array.fill(maxNumber + 1)(true)
def run(): Unit = {
for (index <- 2 to maxNumber) {
if (candidates(index)) {
crossOutMultiples(index)
}
}
}
@sortega
sortega / Distro.scala
Created August 18, 2015 21:40
Another incarnation of the discrete probability monad
package distro
import scala.annotation.tailrec
import scala.util.Random
trait Distro[A] {
def eventSet: Set[A]
def probOf(event: A): BigDecimal
def map[B](f: A => B): Distro[B]
def flatMap[B](f: A => Distro[B]): Distro[B]
@sortega
sortega / LineCount.scala
Created September 3, 2015 17:02
Line count kata
package lines
import java.io.File
import scala.annotation.tailrec
object LineCount {
def of(file: File): Int = {
val source = scala.io.Source.fromFile(file)
try of(source.toStream)