Skip to content

Instantly share code, notes, and snippets.

View dwijnand's full-sized avatar

Dale Wijnand dwijnand

View GitHub Profile
@dwijnand
dwijnand / AvailableMessageDigests.scala
Last active December 31, 2015 18:25
Find out available MessageDigest implementations
import scala.collection.JavaConverters._
object T {
def main(args: Array[String]): Unit =
java.security.Security.getProviders.iterator
.flatMap(p => p.keySet.asScala)
.map(_.asInstanceOf[String].split(" ")(0))
.collect {
case k if k startsWith "MessageDigest." => k substring 14
case k if k startsWith "Alg.Alias.MessageDigest." => k substring 24
import collection.generic.CanBuildFrom
import collection.SeqView
class EitherSplit {
// Viktor Klang (@viktorklang), returns weird type
def split1(data: List[Either[String, Int]]): Either[SeqView[String, Seq[_]], SeqView[Int, Seq[_]]] =
data.partition(_.isLeft) match {
case (Nil, ints) => Right(for (Right(i) <- ints.view) yield i)
case (strings, _) => Left(for (Left(s) <- strings.view) yield s)
}
@dwijnand
dwijnand / BasicStrategy.scala
Created May 5, 2014 23:44
Blackjack basic strategy
object Suit extends Enumeration {
type Suit = Value
val Spades, Hearts, Diamonds, Clubs = Value // Bridge ordering
}
import Suit._
case class Card(rank: Int, suit: Suit)
abstract sealed class Command
@dwijnand
dwijnand / dynver.sbt
Last active January 23, 2016 01:34
dynver
// sbt.version = 0.13.9
lazy val dynver = taskKey[String]("Determine the dynamic version")
dynver := {
// Inspired by Mercurial's own self-versioning:
// http://www.selenic.com/hg/file/61b333b982ea/setup.py#l184
// Outputs either:
// * v1.0.0 (if building on v1.0.0 tag, w/o local changes)
// * v1.0.0+20140707 (if building on v1.0.0 tag with local changes)
// * v1.0.0+3-1234abcd (if building on commit 1234abcd: 3 commits after v1.0.0 tag, w/o local changes)

Keybase proof

I hereby claim:

  • I am dwijnand on github.
  • I am dwijnand (https://keybase.io/dwijnand) on keybase.
  • I have a public key whose fingerprint is BC85 E8D3 D426 748A 7DF5 1BD4 0D86 E659 4879 5693

To claim this, I am signing this object:

package ngrams
import org.specs2.matcher.MatchResult
import org.specs2.mutable.{Specification, Tables}
class NgramsSpec extends Specification with Tables {
import Ngrams._
def expect[A](matchResults: MatchResult[A]*): MatchResult[A] = matchResults reduce (_ and _)
def digest(in: InputStream, md: MessageDigest): Array[Byte] = {
val buffer = new Array[Byte](0x1000)
@tailrec
def loop(count: Int): Array[Byte] = {
if (count == -1)
md.digest
else {
md.update(buffer, 0, count)
loop(in read buffer)
}
import java.io.{ByteArrayOutputStream, PrintStream}
import java.security.MessageDigest
/** Identifies throwables by creating checksums (like MD5 or SHA1) of its stacktrace. */
object ErrId {
/** Returns the absolute checksum of the specified throwable. */
def absolute(t: Throwable): String = {
val bytes = stackBytes(t)
val md = sha1er
@dwijnand
dwijnand / NowIso8601.scala
Last active September 7, 2016 22:43
nowIso8601 without JodaTime
def nowIso8601() = {
val df = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
df setTimeZone (java.util.TimeZone getTimeZone "UTC")
df format new java.util.Date()
}
import akka.actor.{ Actor, ActorRef, ActorSystem, Props }
import scala.reflect.ClassTag
object CrossActorSystemMessageTest {
def main(args: Array[String]): Unit = {
val fooActorSystem = ActorSystem("foo")
val barActorSystem = ActorSystem("bar")
val foo = fooActorSystem.mkActor("fooActor", new Foo)