Skip to content

Instantly share code, notes, and snippets.

View devth's full-sized avatar
Deep Work

Trevor Hartman devth

Deep Work
View GitHub Profile
@devth
devth / jstack
Created December 30, 2015 02:41
2015-12-29 19:41:09
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.25-b02 mixed mode):
"Attach Listener" #11 daemon prio=9 os_prio=31 tid=0x00007f8d4b826800 nid=0x3a0b waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Thread-0" #10 daemon prio=5 os_prio=31 tid=0x00007f8d4e8af800 nid=0x5703 waiting on condition [0x0000700001658000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at org.flatland.drip.Main.killAfterTimeout(Main.java:38)
(def ^:dynamic *foo*)
(defprotocol A
(t [_]))
(defrecord AA []
A
(t [_] *foo*))
(t (->AA)) ; Unbound as expected
(def ^:dynamic *foo* nil)
(binding [*foo* 1]
(prn *foo*) ;;=> 1
(future (prn *foo*)) ;;=> 1
(.start (Thread.
(fn [] (print *foo*) ;;=> nil
))))
(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
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]))
// 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)
/** 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 =>
// 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)
/***
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._
import scalaz._, Scalaz._
val expected = "foo".node("bar".leaf, "baz".node("qux".leaf))
val loc = expected.loc
loc.firstChild
loc.firstChild.right