Skip to content

Instantly share code, notes, and snippets.

View justinhj's full-sized avatar

Justin Heyes-Jones justinhj

View GitHub Profile
@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)
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 / 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")
@justinhj
justinhj / translate.org
Created November 1, 2016 17:18
Translate tables of English words to Mandarin using python translate and org babel

-*- mode: org; org-confirm-babel-evaluate: nil; -*-

from translate import Translator
translator=Translator(to_lang="zh")
translated=translator.translate(translatetext)
@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 / fs2redissubscriber.scala
Last active April 17, 2018 01:47
Subscribe to a redis channel using an FS2 stream
package com.heyesjones.fs2redis
import cats.effect.{Effect, IO}
import com.redis._
import com.typesafe.scalalogging.LazyLogging
import fs2.{async, _}
import scala.concurrent.ExecutionContext
import scala.util.{Failure, Success, Try}
@justinhj
justinhj / gist:c67f6a239a0670bec79551628d9befba
Created May 2, 2018 21:18 — forked from folone/gist:6089236
Table of unicode operators in scalaz 6.0.x