Skip to content

Instantly share code, notes, and snippets.

scala> import scalaz.std.list.listInstance
import scalaz.std.list.listInstance
scala> import scalaz.Traverse
import scalaz.Traverse
scala> import scalaz.ValidationNel
import scalaz.ValidationNel
scala> import scalaz.Validation
module Main where
main = ioA where
ioA = putStrLn "Hola"
ioB = putStrLn "Chao!"
import scalaz._
import Scalaz._
case class Cosa(str: String, f: Float, n: Int)
import scalaz.Order.orderBy
val orderByStr = orderBy { c: Cosa => c.str }
val orderByF = orderBy { c: Cosa => c.f }
val orderByN = orderBy { c: Cosa => c.n }
import Data.IORef
readAndPrint :: IORef Int -> IO ()
readAndPrint ioRef = do
n <- readIORef ioRef
print $ "ioRef = " ++ show n
main = do
ioRef <- newIORef 3
readAndPrint ioRef -- "ioRef 3"
def makeFigure(size: Int, spaces: Int, characters: Int, spacesModifier: Int, charactersModifier: Int): String = {
val charsValsStream = Stream.iterate(characters, size)(chars => chars + charactersModifier)
val spacesValsStream = Stream.iterate(spaces, size)(spaces => spaces + spacesModifier)
(charsValsStream zip spacesValsStream).foldLeft(new StringBuilder) { case (builder,(characters,spaces)) =>
builder append (" " * spaces) append ("*" * characters) append "%n".format()
}.toString()
}

ScalaCheck The Definitive Guide

Chapter 1

  • A specification is a definition of a program’s behavior in the general case. Preferably, a specification should not deal with concrete examples of program functionality. Instead it should provide an abstract description of the behavior, that you can use both when implementing the program and when verifying it.

  • Specifications, on the other hand, give us a more complete picture of the program but they are often informal and difficult to verify formally. Instead, tests are used to verify specific cases of a specification.

  • Tests as specification is a concept that is gaining popularity in many test-driven software development processes.

def fa[A0,A1](a: A0): A1 = ???
def head[A](l: List[A]): A = l.head
def fmap[A,B](f: A => B)(l: List[A]): List[B] = l.map(f)
"head and then map is the same as map and then head" {
fa[A0,A1] compose head[A0] == head[A1] compose fmap[A0,A1](fa)
}
def tail[A](l: List[A]): List[A] = l.tail
"tail and then map is the same as map and then tail" {
// a #:: f(a) #:: f(f(a)) #:: f(f(f(a))) #:: ...
def repeat[A](f: A => A)(a: => A): Stream[A] = a #:: repeat(f)(f(a))
def next(n: Double)(x: Double): Double = (x + n/x)/2.0
def within(eps: Double)(s: Stream[Double]): Double = s match {
case a #:: b #:: rest => if(Math.abs(a-b)<=eps) b else within(eps)(rest)
case _ => throw new Exception("El stream debería tener por lo menos dos elementos")
}
import AssemblyKeys._
assemblySettings
name := "scala-test"
version := "0.0.1"
scalaVersion := "2.10.3"