This file contains 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 ^:dynamic *foo*) | |
(defprotocol A | |
(t [_])) | |
(defrecord AA [] | |
A | |
(t [_] *foo*)) | |
(t (->AA)) ; Unbound as expected |
This file contains 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 ^:dynamic *foo* nil) | |
(binding [*foo* 1] | |
(prn *foo*) ;;=> 1 | |
(future (prn *foo*)) ;;=> 1 | |
(.start (Thread. | |
(fn [] (print *foo*) ;;=> nil | |
)))) |
This file contains 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
(defprotocol F | |
(echo [this msg])) | |
(def anon (reify F (echo [this msg] (str msg " anon " this)))) | |
(echo anon "bar") | |
;;=> "bar anon yetibot.core.adapters.adapter$reify__8141@6ebc46fd" | |
(deftype Foo [x y] | |
F |
This file contains 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 DissertationRow( | |
name: Option[String]=None, | |
birthYear: Option[Int]=None, | |
dissertation: Option[String]=None) extends Row { | |
def setValueForOrdinal[A](ord: Int, value: A): DissertationRow = | |
ord match { | |
case 0 => this.copy(name = Some(value.asInstanceOf[String])) | |
case 1 => this.copy(birthYear = Some(value.asInstanceOf[Int])) | |
case 2 => this.copy(dissertation = Some(value.asInstanceOf[String])) |
This file contains 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
// Compute center-weighted moving average of equally spaced values, some of which may be None | |
// period should be odd | |
def centerWeightedAverage (values: Iterable[Option[Double]], period: Int): Seq[Option[Double]] = { | |
// prepend and append period/2 Nones | |
val valuesWithBuffers = List.fill(period / 2)(None) ++ values ++ List.fill(period / 2)(None) | |
valuesWithBuffers | |
.sliding(period) | |
.map(_.flatten) | |
.map(x => if (x.size > 0) Some(x.sum.toDouble / x.size) else None) | |
.take(values.size) |
This file contains 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
/** Implicit class to add idiomatic Scala methods to the | |
* InterProcessReadWriteLock read/write lock */ | |
implicit class RichInterProcessReadWriteLock(lock: InterProcessReadWriteLock) { | |
private def acquireAndRelease[A](mutex: InterProcessMutex, fn: => A): Either[Throwable, A] = | |
Try { | |
// blocking operation | |
mutex.acquire() | |
logger.info(s"Lock obtained: $mutex") | |
Right(fn) | |
}.recover { case e => |
This file contains 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
// Running this code: | |
// scala -Dscala.color -feature -classpath ~/.m2/raptor2/org/scalaz/scalaz-core_2.11/7.1.2/scalaz-core_2.11-7.1.2.jar typeclass.scala | |
import scalaz._, Scalaz._ | |
import scala.language.implicitConversions | |
import scala.language.postfixOps | |
object Typeclass { | |
class C(val name: String) |
This file contains 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
/*** | |
scalaVersion := "2.11.6" | |
libraryDependencies ++= Seq("org.scalaz" %% "scalaz-core" % "7.1.0") | |
*/ | |
// Code listing for http://devth.com/2015/thrush-cond-is-not-a-monad/ | |
object Main extends App { | |
import scalaz._, Scalaz._ |
This file contains 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 scalaz._, Scalaz._ | |
val expected = "foo".node("bar".leaf, "baz".node("qux".leaf)) | |
val loc = expected.loc | |
loc.firstChild | |
loc.firstChild.right |
This file contains 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 getOrCreate[A](m: JMap[Any, Any], key: Any, create: A): A = { | |
if (m.containsKey(key)) m.get(key).asInstanceOf[A] | |
else { | |
m.put(key, create) | |
create | |
} | |
} | |
def storeAtPath(m: JMap[Any, Any], path: Seq[String], values: Seq[Any]) { | |
path.toList match { |