Skip to content

Instantly share code, notes, and snippets.

View justinhj's full-sized avatar

Justin Heyes-Jones justinhj

View GitHub Profile
@justinhj
justinhj / FS2TakeWhileRepeat.scala
Created April 9, 2018 16:26
FS2 gist to pull from a stream until N items that return true for the provided predicate
def takeWhileRepeat[F[_],O](n: Long, f: O => Boolean): Pipe[F,O,O] = {
def go(s: Stream[F,O], wasTrueCount : Int) : Pull[F,O,Unit] = {
if(wasTrueCount == n) {
Pull.done
}
else {
s.pull.uncons1.flatMap {
@justinhj
justinhj / takeuntiln.scala
Last active April 9, 2018 18:43
Trying to write a function using fs2 to stream until N items have been seen
def takeUntilNThings[F[_],O](n: Long, thing: O): Pipe[F,O,O] = {
def go(s: Stream[F,O], seenCount : Int) : Pull[F,O,Unit] = {
if(seenCount == n) {
Pull.done
}
else {
s.pull.uncons1.flatMap {
@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")
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 {
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 {
@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 {
@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 / 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
// 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
// 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