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
View blocking.scala
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
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
eamelink / recursion-and-trampolines-in-scala.md
Last active November 28, 2023 00:56
Recursion and Trampolines in Scala
View recursion-and-trampolines-in-scala.md

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
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
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
eamelink / traverse.scala
Created November 16, 2015 12:05
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!
Traverse[List].traverseU(myList)(myFun)
// 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