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
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]
@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
@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 / 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 / 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 / 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 / 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 {