This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> (1 to 10).map(n => Future.fork(Future.delay{Thread.sleep(1000); n})).toList.sequence.run | |
res0: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> import scalaz._,Scalaz._ | |
import scalaz._ | |
import Scalaz._ | |
scala> val isOne = (i: Int) => {println("isOne"); if (i == 1) Some(1) else None} | |
isOne: Int => Option[Int] = <function1> | |
scala> val isTwo = (i: Int) => {println("isTwo"); if (i == 2) Some(2) else None} | |
isTwo: Int => Option[Int] = <function1> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.annotation._ | |
sealed trait List[@specialized A] { | |
import List._ | |
def headOption: Option[A] = this match { | |
case ListElem(head, _) => Some(head) | |
case EmptyList() => None | |
} | |
def tailOption: Option[List[A]] = this match { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class SomethingRepository() | |
case class SomethingService(implicit somethingRepository: SomethingRepository) | |
implicit val somethingRepository = new SomethingRepository() | |
implicit val somethingService = new SomethingService |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def composeMaps[A, B, C](mapAB: Map[A, B], mapBC: Map[B, C]): Map[A, C] = { | |
mapAB.foldLeft(Map.empty[A, C]){case (workingMap, (key, value)) => | |
mapBC.get(value) match { | |
case Some(mappedValue) => workingMap.updated(key, mappedValue) | |
case _ => workingMap | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1) If on Mac OS X or Linux grab the following file, put it on your path and make it executable: https://raw.github.com/paulp/sbt-extras/master/sbt With Windows the Typesafe Stack should install SBT. | |
// 2) Create a directory and move inside that directory from your terminal/command prompt and run "sbt -sbt-create". | |
// 3) Run the following commands in SBT: | |
// set libraryDependencies += "org.scalaz" %% "scalaz-core" % "7.0.0-M7" | |
// set initialCommands := "import scalaz._,Scalaz._" | |
// session-save | |
// 4) Run the console command in SBT (from now on you can just run this immediately) and start playing: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class ListFoldRightBenchmark() extends SimpleScalaBenchmark { | |
import scala.collection.mutable.ArrayStack | |
@Param(Array("0", "1", "2", "3", "5", "10", "100", "500", "1000", "2000")) | |
val length: Int = 0 | |
var list: List[Int] = _ | |
override def setUp() { | |
// set up all your benchmark data here |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class Person(name: String, age: Int) | |
implicit val DecodePerson: DecodeJson[Person] = jdecode2L(Person(_: String, _: Int))("name", "age") | |
implicit val EncodePerson: EncodeJson[Person] = jencode2L((p: Person) => (p.name, p.age))("name", "age") | |
val decoded: Option[Person] = """{"name":"Fred","age":"40"}""".decodeOption[Person] | |
val encoded: Option[String] = decoded.map(_.jencode.nospaces) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val username: Option[String] = for { | |
parsed <- requestJson.parseOption // Parse the JSON. | |
jsonObject <- parsed.obj // Get the JSON as a JsonObject instance. | |
userIDJson <- jsonObject("userid") // Get the "userid" field from the JsonObject. | |
userID <- userIDJson.string // Get the value of the "userid" field. | |
user <- lookupUser(userID) // Get an instance of User for the user ID. | |
} yield user.username |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/benchmark/src/main/scala/argonaut/benchmark/CaliperBenchmark.scala b/benchmark/src/main/scala/argonaut/benchmark/CaliperBenchmark.scala | |
index a3c4fc1..68a3759 100644 | |
--- a/benchmark/src/main/scala/argonaut/benchmark/CaliperBenchmark.scala | |
+++ b/benchmark/src/main/scala/argonaut/benchmark/CaliperBenchmark.scala | |
@@ -6,6 +6,7 @@ import Argonaut._ | |
import net.liftweb.json.{JsonParser => LiftJsonParser} | |
import scalaz._ | |
import Scalaz._ | |
+import scala.collection.mutable.ListBuffer | |