Skip to content

Instantly share code, notes, and snippets.

@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 {

@eamelink
eamelink / transformers.scala
Last active November 26, 2021 15:36
Working with Eithers in Futures
import scala.concurrent.Future
object either {
// Scala standard library Either is sometimes used to distinguish between 'failure' and 'success' state. Like these two methods:
def getUser(id: String): Either[String, User] = ???
def getPreferences(user: User): Either[String, Preferences] = ???
// The Right side contains the success value by convention, because right is right, right?
@eamelink
eamelink / iteratees-by-example.scala
Created May 23, 2013 21:40
Scala-IDE worksheet with some examples of iteratees, enumerators and enumeratees and how to use and compose them. Originated from a presentation at Dutch Scala Enthusiasts.
import play.api.libs.iteratee._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent._
import scala.concurrent.duration._
import play.api.libs.concurrent.Promise
object iteratees {
// Implicit conversion to add 'await' to a Future
implicit class WFuture[A](val inner: Future[A]) extends AnyVal {
@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")
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]
trait ElasticSearchComponent {
val esClient: EsClient
}
trait ElasticSearchIndex { self: ElasticSearchComponent =>
def findAll() = esClient.doAQueryForAll()
}
@eamelink
eamelink / Application.scala
Created November 15, 2013 12:31
Proxying a web service with Play
package controllers
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import play.api.libs.iteratee.{ Concurrent, Iteratee }
import play.api.libs.ws.{ ResponseHeaders, WS }
import play.api.mvc.{ Action, Controller, SimpleResult }
import scala.concurrent.Promise
object Application extends Controller {
@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 / fields.scala
Last active December 13, 2015 17:18
Checking how constructor parameters do or do not end up as fields
/*
* bar is not a real field: after the lazy val bork is initialized,
* 'bar' is set to null.
*
* It's a normal field when we make bork a def, add 'val' or 'var' to bar or make it a case class.
* It's not a field at all when we make bork a val.
*/
class MyClass(param: String) {
lazy val field = param