Skip to content

Instantly share code, notes, and snippets.

Keybase proof

I hereby claim:

  • I am drstevens on github.
  • I am drstevens (https://keybase.io/drstevens) on keybase.
  • I have a public key whose fingerprint is FC3A 884C F064 48BC 315E 205C B797 C6B5 C281 822B

To claim this, I am signing this object:

@drstevens
drstevens / MonocleTesting.scala
Last active November 28, 2016 23:14
Monocle Optional and Traverable
import monocle.function.Each
import monocle.macros.GenLens
import monocle.{Lens, Optional}
sealed trait Foo
object Foo {
// This is similar to scalaz.PLens
val _foo1Opt: Optional[Foo, Foo1] = Optional[Foo, Foo1] {
case f1: Foo1 => Some(f1)

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
@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 {
// 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)
-Dconfig.resource=test.conf -Dlogger.file=test/resources/logback-test.xml
@drstevens
drstevens / Foo.scala
Created December 16, 2013 21:32
Experiments with scalaz-stream. Attempt to maintain state by calls to next.
import scala.collection.immutable
import scalaz.std.string._
import scalaz.std.list._
import scalaz.std.anyVal._
import scalaz.std.option._
import scalaz.syntax.show._
import scalaz.concurrent.Task
import scalaz.stream.{process1, io, Process}
import scalaz.stream.Process._
@drstevens
drstevens / gist:5579549
Last active December 17, 2015 08:19
Attempt to sequence List[StateT[Trampoline,A,B]]] with Trampoline in Scalaz 7. Unable to get this to work in scala 2.9.2 REPL. Works in 2.10.1 REPL. See https://gist.github.com/larsrh/5579789 also
import scalaz.std.anyVal._
import scalaz.std.tuple._
import scalaz.std.option._
import scalaz.StateT
import scalaz.State
import scalaz.Free
import scalaz.Free._
import scalaz.State._
import scalaz.Trampoline._
import scalaz.syntax.traverse._
@drstevens
drstevens / OptionMap.scala
Last active December 15, 2015 19:49
This example demonstrates benefits of using fold/cata instead of map and getOrElse on Option[T]. There are, of course, occasions when pattern matching is preferable as well.
Welcome to Scala version 2.10.1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_15).
Type in expressions to have them evaluated.
Type :help for more information.
scala> val opt: Option[Int] = Some(10)
opt: Option[Int] = Some(10)
scala> val typeCheckFail = opt map (_ + "2") getOrElse (3)
typeCheckFail: Any = 102
@drstevens
drstevens / Futures.scala
Created January 21, 2013 21:28
Define Apply[Future] in order to sequence. Written for Akka 1.3.1 (old, I know) and Scalaz 6.0.4. I imagine similar could be written for Akka 2.1.x and Scalaz 7. I've used this in many places so far without issue, but I've only used it for sequencing through something like `Validation[S, Future[Validation[S, Future[B]]]`. If I'm breaking some la…
object FuturePimps {
/**
* Use this instead of new AlreadyCompletedFuture[T] to prevent accidental use of default Akka timeout
**/
def alreadyCompletedFuture[T](v: T)(implicit timeout: Timeout): AlreadyCompletedFuture[T] =
new AlreadyCompletedFuture[T](Right(v), timeout.duration.toMillis)
class FutureFuture[T](f: () => Future[Future[T]]) {
def flatten: Future[T] = f().flatMap(identity)