Skip to content

Instantly share code, notes, and snippets.


Erik Bakker eamelink

View GitHub Profile
eamelink / blocking.scala
Last active Jan 27, 2017
Demonstration of scala.concurrent.blocking
View blocking.scala
import scala.concurrent._
import scala.concurrent.duration._
// Experiment with and without the `blocking` call in this program!
object Test extends App {
val x = Future.traverse(1 to 30){ i =>
Future {
println("Starting #" + i)
View dependent-tasks.scala
// Can we implement `dependentTasks` in a functional way?
// Is it a terrible idea to implement this? Why?
def dependentTasks: (String => Task[String], String => Task[String]) = ???
val (f1, f2) = dependentTasks
val t1 = f1("foo")
val t2 = f2("bar")
assert(t1.unsafeRun == "foo-bar")
assert(t2.unsafeRun == "bar-foo")
eamelink /
Last active Jul 14, 2022
Recursion and Trampolines in Scala

Recursion and Trampolines in Scala

Recursion is beautiful. As an example, let's consider this perfectly acceptable example of defining the functions even and odd in Scala, whose semantics you can guess:

def even(i: Int): Boolean = i match {
  case 0 => true
  case _ => odd(i - 1)

def odd(i: Int): Boolean = i match {

View CoproductInject.scala
object CoproductInject {
trait A[T]
trait B[T]
trait C[T]
type Two[T] = Coproduct[A, B, T]
eamelink / DisjunctionFilter.scala
Created Dec 23, 2015
Custom filter on disjunction, avoid need for Monoid
View DisjunctionFilter.scala
import scalaz._
import scalaz.Scalaz._
object Test extends App{
trait Zero[A] {
def zero: A
implicit class RichDisjunction[A, B](value: A \/ B) {
def withFilter(p: B => Boolean)(implicit za: Zero[A]): A \/ B = value match {
eamelink / traverse.scala
Created Nov 16, 2015
Traverse and TraverseU
View traverse.scala
val myList = List("foo")
val myFun: (String) => EitherT[Future, Exception, Int] = (in: String) => EitherT { Future { try { \/-(in.toInt) } catch { case NonFatal(e: Exception) => -\/(e) } }}
// Regular traverse
Traverse[List].traverse[({type G[A]=EitherT[Future,Exception,A]})#G, String, Int](myList)(myFun)
// TraverseU to the rescue!
// Pimpin' to the rescue!
View Aff-catchError.purs
module Main where
import qualified Debug.Trace as T
import Control.Alt
import Control.Monad.Aff
import Control.Monad.Eff
import Control.Monad.Eff.Exception
import Control.Monad.Error.Class
import Data.Either
View Affexperiments.purs
module Main where
import Debug.Trace
import Control.Monad.Aff
main = liftEff' $ trace "Hello world!"
{- Gives this output:
* Building project in /Users/foo/bar/quux
View Nine.scala
case class Nine private(inner: String)
object Nine {
def apply(value: String): Option[Nine] =
if(value.last == "9") Some(new Nine(value)) else None
val x = Nine("123") // None
val y = Nine("789") // Some(Nine("789"))
View Try.scala
scala> import scala.util.Try
scala> object Borked { sys.error("Boom!") }
defined object Borked
scala> Try(Borked)
java.lang.RuntimeException: Boom!
at scala.sys.package$.error(package.scala:27)
... 38 elided