Skip to content

Instantly share code, notes, and snippets.

-Dconfig.resource=test.conf -Dlogger.file=test/resources/logback-test.xml
// Doesn't compile. Good.
def futureInt(): Future[Int] = Future.value(Future.value(1))
// Compiles. Not good. Caller cannot wait on completion of nested future.
def futureUnit(): Future[Unit] = Future.value(Future.value(()))
// Also compiles. Nice convenience, but not worth the cost of the issues
// it might cause (as above), right?
def futureUnit(): Future[Unit] = Future.value(1)
@drstevens
drstevens / FooSpec.scala
Created January 16, 2014 01:15
Attempt to use defaultReturn to automatically set up return values to methods on mock which the return instance of the mock. See my question to specs2-user mailing list - https://groups.google.com/forum/#!topic/specs2-users/My86VN-NT80
//scala 2.10.2
//"org.scalaz" %% "scalaz-core" % "7.0.3"
//"org.specs2" %% "specs2" % "2.1.1" % "test"
//"org.mockito" % "mockito-all" % "1.9.0" % "test"
import scalaz.State._
import org.specs2.mutable.Specification
import org.specs2.mock._
trait Builder {
@drstevens
drstevens / gist:9201268
Created February 25, 2014 02:03
Map.withDefaultValue is a bad idea
Welcome to Scala version 2.10.3 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_25).
Type in expressions to have them evaluated.
Type :help for more information.
scala> val map0 = Map.empty[Int, Int] withDefaultValue 0
map0: scala.collection.immutable.Map[Int,Int] = Map()
scala> val map1 = map0 mapValues(identity)
map1: scala.collection.immutable.Map[Int,Int] = Map()
@drstevens
drstevens / ApplicativeIdentityFail.scala
Created February 26, 2014 21:27
Applicative for play.api.libs.json.JsResult
Welcome to Scala version 2.10.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_25).
Type in expressions to have them evaluated.
Type :help for more information.
scala> import play.api.libs.json._; import play.api.libs.functional.Applicative;
import play.api.libs.json._
import play.api.libs.functional.Applicative
scala> val expected = JsSuccess(10, JsPath(List(KeyPathNode("somekey"))))
expected: play.api.libs.json.JsSuccess[Int] = JsSuccess(10,/somekey)
@drstevens
drstevens / trampolined-state.scala
Last active August 29, 2015 14:02 — forked from travisbrown/trampolined-state.scala
Trampolining the state monad via applicative combinator appears to work
import scalaz._, Scalaz._
def setS(i: Int): State[List[Int], Unit] = modify(i :: _)
val s = (1 to 10000).foldLeft(state[List[Int], Unit](()).lift[Free.Trampoline]) {
case (st, i) => st *> setS(i).lift[Free.Trampoline]
}
s(Nil).run
@drstevens
drstevens / gist:2b6a9686881c1183ed55
Created September 18, 2014 18:31
Reduce boilerplate when not using primitives
import scalaz.Id.Id
import scalaz.BijectionT._
import scalaz.Lens
trait Bijectionz[A, C] {
def apply(str: C): A
def unapply(a: A): Option[C]
@drstevens
drstevens / gist:77db6bab6b1e995dac13
Created December 18, 2014 23:29
rough timing of alternate implementation of Rng.fill
import com.nicta.rng.Rng
import org.joda.time.DateTime
import scalaz._
import Scalaz._
object Foo extends App {
def time[A](f: => A): (Long, Long, Long) = {
val t0 = System.currentTimeMillis
val a = f
trait Writes[T] {
def writes(o: T): JsValue
}
trait Reads[T] {
def reads(json: JsValue): T
}
trait Format[T] extends Writes[T] with Reads[T]

Advanced Functional Programming with Scala - Notes

Copyright © 2017 Fantasyland Institute of Learning. All rights reserved.

1. Mastering Functions

A function is a mapping from one set, called a domain, to another set, called the codomain. A function associates every element in the domain with exactly one element in the codomain. In Scala, both domain and codomain are types.

val square : Int => Int = x => x * x