Skip to content

Instantly share code, notes, and snippets.

@eamelink
eamelink / blocking.scala
Last active January 27, 2017 14:44
Demonstration of scala.concurrent.blocking
import scala.concurrent._
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global
// 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)
@eamelink
eamelink / dependent-tasks.scala
Last active July 27, 2016 14:55
Dependent tasks
// 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
eamelink / recursion-and-trampolines-in-scala.md
Last active April 10, 2024 15:57
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 {

import cats.data.Coproduct
import cats.free.Inject
object CoproductInject {
trait A[T]
trait B[T]
trait C[T]
type Two[T] = Coproduct[A, B, T]
@eamelink
eamelink / DisjunctionFilter.scala
Created December 23, 2015 12:07
Custom filter on disjunction, avoid need for Monoid
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
eamelink / traverse.scala
Created November 16, 2015 12:05
Traverse and TraverseU
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!
Traverse[List].traverseU(myList)(myFun)
// Pimpin' to the rescue!
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
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
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"))
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