Skip to content

Instantly share code, notes, and snippets.

@eamelink
eamelink / trampolines.scala
Last active August 29, 2015 14:14
Trampolines
import scala.annotation.tailrec
object Trampolines {
// This is the regular one, and it stack overflows at a couple of 100k input.
object Stack {
def even(i: Int): Boolean = i match {
case 0 => true
case other => odd(i - 1)
}
@eamelink
eamelink / capture.scala
Last active August 29, 2015 14:13
Capturing type classes
object MyApp extends App {
// The type class we use in all examples
trait Show[A] {
def show(a: A): String
}
implicit val StringShow = new Show[String] { def show(a: String) = "String(" + a + ")" }
implicit val IntShow = new Show[Int] { def show(a: Int) = "Int(" + a.toString + ")" }
@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 / findAndModify.scala
Last active August 29, 2015 14:11
MongoDB findAndModify
// Sane implementation of findAndModify:
findAndModify(update: Record => Record, new: Record) {
getOld match {
case None => save(new)
case Some(oldRecord) => save(update(oldRecord))
}
}
// MongodDb implementation of findAndModify:
findAndModify(update: Record => Record, new: Record, always: Record) {
@eamelink
eamelink / Main.purs
Created October 9, 2014 06:14
Typeclass instance head is invalid
module Main where
import Debug.Trace
import Control.Monad.RWS
import Control.Monad.RWS.Class
import Control.Monad.RWS.Trans
import Control.Monad.Identity
import Data.Monoid
import Data.Tuple
@eamelink
eamelink / gist:bd425f0b2f3cee8e702c
Created July 14, 2014 09:52
Example of converting Future[Try[A]] to EitherT
val a, b, c: Future[Try[String]] = ???
val out = for {
_ <- a.map(t => \/.fromTryCatch(t.get).leftMap(ex => "Error!")) |> EitherT.apply
_ <- b.map(t => \/.fromTryCatch(t.get).leftMap(ex => "Error 2!")) |> EitherT.apply
_ <- c.map(t => \/.fromTryCatch(t.get).leftMap(ex => "Error 3!")) |> EitherT.apply
} yield ()
val result: Future[String \/ Unit] = out.run

Keybase proof

I hereby claim:

  • I am eamelink on github.
  • I am eamelink (https://keybase.io/eamelink) on keybase.
  • I have a public key whose fingerprint is 038D FFCD 9C91 3710 F92A 909D 1358 5D54 8BCA 0616

To claim this, I am signing this object:

@eamelink
eamelink / snippet.scala
Created July 9, 2014 09:10
Ruby to Scala snippet
// Ruby
def test
yield 5
puts "You are in the method test"
yield 100
end
test {|i| puts "You are in the block #{i}"}
// Scala
def test(fn: (Int) => ()) {
@eamelink
eamelink / predicates.scala
Created June 25, 2014 16:06
Composable predicates
class SimpleFile
class ExtendedFile extends SimpleFile
implicit class Predicate[-A](val fn: A => Boolean) {
def &&[B <: A](other: Predicate[B]): Predicate[B] = Predicate { (x: B) =>
fn(x) && other.fn(x)
}
}
@eamelink
eamelink / HelloWorld.purs
Created June 5, 2014 06:25
HelloWorld.purs typing questions
module HelloWorld where
import Data.Either
import Prelude
import Data.Foreign
import Control.Monad.Eff
import qualified Control.Monad.JQuery as J
import Debug.Trace
main = J.ready $ do