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
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 |
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 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 { |
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
// 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 |
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
// 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 |
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 java.util.concurrent.Executors | |
import scala.concurrent.{ExecutionContext, Future, Await} | |
import scala.concurrent.duration._ | |
object conc1 { | |
val numThreads = 4 |
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.{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) |
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
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 { |
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 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 { |
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 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 { |
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 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") |