Skip to content

Instantly share code, notes, and snippets.

View noelwelsh's full-sized avatar
💭
Hacking on Doodle when I get time

Noel Welsh noelwelsh

💭
Hacking on Doodle when I get time
View GitHub Profile
@noelwelsh
noelwelsh / loop.scala
Created October 15, 2015 16:18
Example of monadic loop using the State monad in Cats
import cats.{Id,Monad}
import cats.state.State
import cats.std.function._
import scala.language.higherKinds._
// Call Example.example.run to see the example running
object Example {
type MyState[A] = State[Int, A]
@noelwelsh
noelwelsh / Nullable.scala
Created April 17, 2015 10:38
Nullable types in Scala
object Nullable {
sealed trait NullableTag
type Nullable[A] = A with NullableTag
def nullAs[B]: Nullable[B] =
null.asInstanceOf[Nullable[B]]
implicit class ToNullable[A](val a: A) extends AnyVal {
def `?`: Nullable[A] = a.asInstanceOf[Nullable[A]]
}
@noelwelsh
noelwelsh / Example.scala
Created February 20, 2015 16:06
Robust Error Handling in Scala
import scalaz.\/
import scalaz.syntax.either._
object Example2 {
// This example simulates error handling for a simple three tier web application
//
// The tiers are:
// - the HTTP service
// - a user authentication layer
// - a database layer
// Type class
trait Burp[A] {
def burp(in: A): String
}
object Burp {
def apply[A](implicit burp: Burp[A]): Burp[A] = burp
}
case class Foo(name: String)
import scalaz.{Functor, Monad}
import scalaz.std.option._
import scalaz.syntax.monad._
import scalaz.syntax.functor._
object SyntaxExamples {
// This works fine
Monad[Option].lift((x: Int) => x + 1)
// could not find implicit value for parameter F: scalaz.Functor[F]
@noelwelsh
noelwelsh / Mixpanel.scala
Created January 22, 2013 20:38
Mixpanel API client for Play 2.0
package myna
package app
import bigtop.util.Uuid
import myna.model.external.User
import myna.util.Base64
import play.api.libs.ws._
import play.api.libs.json._
import play.api.libs.concurrent.Promise
import play.api.mvc.Request
@noelwelsh
noelwelsh / Project.scala
Created March 2, 2011 14:48
Code to set the run.mode to production within WAR files.
val jettyWebPath = "src" / "main" / "webapp" / "WEB-INF" / "jetty-web.xml"
lazy val installProductionRunMode = task {
FileUtilities.copyFile("project" / "jetty-web.xml",
jettyWebPath,
log)
log.info("Copied jetty-web.xml into place")
None
} describedAs("Install a jetty-web.xml that sets the run mode to production")