Skip to content

Instantly share code, notes, and snippets.

View justinhj's full-sized avatar

Justin Heyes-Jones justinhj

View GitHub Profile
object Xmas1 {
object Move {
def fromString(arg: String): Option[Move] = arg match {
case s if arg.startsWith("R") || arg.startsWith("L") =>
try {
val turn = if(arg.charAt(0) == 'R') Right else Left
import scala.collection.{immutable, mutable}
object Xmas1 {
object Move {
def fromString(arg: String): Option[Move] = arg match {
case s if arg.startsWith("R") || arg.startsWith("L") =>
try {
// Solution to http://adventofcode.com/2016/day/2
object Xmas2 {
case class Transition(name: Char, target: Digit)
case class Digit(value: Int, var transitions: Vector[Transition] = Vector[Transition]()) {
def addTransitions(newTransitions: Vector[Transition]) = {
transitions = transitions ++ newTransitions
// Adapting part 1 to part 2 was a simple matter of changing the network of digits and transitions between them
object Xmas2 {
case class Transition(name: Char, target: Digit)
case class Digit(value: Int, var transitions: Vector[Transition] = Vector[Transition]()) {
def addTransitions(newTransitions: Vector[Transition]) = {
transitions = transitions ++ newTransitions
@justinhj
justinhj / conc1.scala
Created January 27, 2017 17:37
Sample Scala code for creating a list of futures then executing them in parallel then shutting down when they are complete.
import java.util.concurrent.Executors
import scala.concurrent.{ExecutionContext, Future, Await}
import scala.concurrent.duration._
object conc1 {
val numThreads = 4
@justinhj
justinhj / functorsmonadsandapplicatives.scala
Last active March 1, 2017 22:20
Functors Monads and Applicatives scala worksheet
import scalaz.{Applicative, Functor, Monad}
// Based on https://thedet.wordpress.com/2012/04/28/functors-monads-applicatives-can-be-so-simple/
object FMA {
// A simple effectful type constructor
// All it does is wrap a value of any type
case class MyBox[T](val value:T)
@justinhj
justinhj / FutureTimeout.scala
Last active September 29, 2021 08:22
How to write a future timeout with the Scala/Java standard library
package justinhj.concurrency
import java.util.concurrent.TimeoutException
import java.util.{Timer, TimerTask}
import scala.concurrent.duration.FiniteDuration
import scala.concurrent.{ExecutionContext, Future, Promise}
import scala.language.postfixOps
object FutureUtil {
import java.util.concurrent.TimeoutException
import java.util.{Timer, TimerTask}
import scala.concurrent.duration.FiniteDuration
import scala.concurrent.{ExecutionContext, Future, Promise}
import scala.language.postfixOps
object FutureUtil {
import org.scalatest._
import scala.concurrent.{Future, TimeoutException}
import scala.concurrent.duration._
class TestFutureUtil extends AsyncFlatSpec with Matchers with OptionValues with Inside with Inspectors {
implicit override def executionContext = scala.concurrent.ExecutionContext.Implicits.global
"futureWithTimeout" should "complete the users future when it returns before the timeout" in {
@justinhj
justinhj / ParallelFuture.scala
Created July 28, 2017 04:27
Example of running futures in parallel
import scala.concurrent.duration._
import scala.concurrent.duration.FiniteDuration
import scala.concurrent.{Await, Future}
import scala.language.postfixOps
import scala.concurrent.ExecutionContext.Implicits.global
object ParallelFuture {
def printWithTimeAndThreadID(s : String): Unit = println(s"${System.currentTimeMillis()} thread id ${Thread.currentThread().getId} : $s")