Skip to content

Instantly share code, notes, and snippets.

View seanparsons's full-sized avatar
💭
Setting A Status Message

Sean Parsons seanparsons

💭
Setting A Status Message
View GitHub Profile
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)
@seanparsons
seanparsons / FirstSome.scala
Last active December 14, 2015 15:19 — forked from channingwalton/FirstSome.scala
Cunning use of monoids with Function1.
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>
@seanparsons
seanparsons / gist:5023353
Created February 24, 2013 10:31
Alternative List implementation.
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 {
@seanparsons
seanparsons / gist:4737775
Created February 8, 2013 09:47
Implicit constructor wiring example.
case class SomethingRepository()
case class SomethingService(implicit somethingRepository: SomethingRepository)
implicit val somethingRepository = new SomethingRepository()
implicit val somethingService = new SomethingService
@seanparsons
seanparsons / gist:4595750
Last active December 11, 2015 11:39
Composing Maps.
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
}
}
}
@seanparsons
seanparsons / gist:4508855
Created January 11, 2013 08:14
Start experimenting with scalaz from scratch.
// 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:
@seanparsons
seanparsons / CaliperBenchmark.scala
Last active April 28, 2019 16:05
List.foldRight performance evaluation (run against Scala 2.10.0 with OpenJDK 1.7.0).
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
@seanparsons
seanparsons / gist:4453577
Created January 4, 2013 15:44
Decode encode example with argonaut.
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)
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
@seanparsons
seanparsons / gist:4423197
Created December 31, 2012 22:08
List.newBuilder makes things fast. :(
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