Skip to content

Instantly share code, notes, and snippets.

scala> import cats._, cats.instances.all._
import cats._
import cats.instances.all._
scala> :paste
// Entering paste mode (ctrl-D to finish)
sealed trait TrafficLight
object TrafficLight {
def red: TrafficLight = Red
/**
* A simple Java data class with two immutable fields. If I want
* to add a third field (e.g. seconds), I have to update a ton of
* places and remember them all, because a compiler will not tell
* you what you missed. If I generate equals/hashCode/toString, I
* have to remember to do so with every change. And I cannot
* overload constructors and assume default values for parameters,
* because in this class, each field is an int and they'd have the
* same signature, so I have to use the Builder Pattern to do
* default values. This is a maintenance nightmare compared to
import akka.actor.Actor
import akka.actor.OneForOneStrategy
import akka.actor.SupervisorStrategy.Stop
import akka.actor.ActorSystem
import akka.actor.Props
import akka.actor.ActorInitializationException
class TestActorParent extends Actor {
override val supervisorStrategy = OneForOneStrategy() {
case a: ActorInitializationException => {
Initialization order is always tricky (not just in Scala). The order is top-down, so all these results are explainable through that. Using def will solve the initialization problem, as long as you can use defs (sometimes you don’t want to recompute the value on each access).
I find the following rule of thumb to work well:
- use abstract defs in traits by default, but...
- use a lazy val if the field will be used to access inner types (you need a stable path, so p.UserService works only when p is
a val). So this is not only an implementation concern, it opens up new possibilities for callers of this code.
- downgrade to a val if you are absolutely sure you don’t need laziness, or if concurrency implications of using a lazy value
are important (it synchronizes on the enclosing this to protect multiple threads from attempting to do it for the first time
simultaneously).
@jamie-allen
jamie-allen / gist:8571694
Created January 23, 2014 02:21
A complaint to US Airways
I'm Chairman's Preferred, but I'm switching to another airline to stay on the Star Alliance the second the changeover occurs in March. I'm tired of US Airways' cattle cars with no services except wifi. You don't even give power outlets to first class travelers. That's absurd.
I'm tired of being told ludicrous explanations that my flight from SFO to Zurich is "direct," despite stopping in PHL, changing planes and likely having to switch terminals. I'm tired of flying in outdated 767s that leak water on me. I'm tired of your flight attendants' attitudes, that when I point out the water leaking on me from the water-damaged internal fuselage, tells me there's nothing they can do about it instead of WRITING IT DOWN SO A MAINTENANCE PERSON CAN FIX IT. I'm tired of complaining about poor service and having another flight attendant complain back at me that they lost their personal closets in the latest plane overhauls, as if I should care about that. I'm tired of not having a personal movie system in economy
@jamie-allen
jamie-allen / gist:7833551
Created December 6, 2013 22:54
How to bypass Scala trait linearization
scala> trait A { def p: Unit }
defined trait A
scala> trait B extends A { def p = println("b") }
defined trait B
scala> trait C extends A { def p = println("c") }
defined trait C
scala> class D extends B with C { override def p = { super[B].p } }
80 pages in pre-production length
Preface
Part I. Actor Application Types
1. Domain-Driven
Domain-driven messages are “facts”
2. Work Distribution
Routers and routees
Random
➜ ~ scala
Welcome to Scala version 2.10.0 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_10-ea).
Type in expressions to have them evaluated.
Type :help for more information.
scala> :cp /Users/jamie/.ivy2/local/com.typesafe/config/0.4.2-SNAPSHOT/bundles/config.jar
Added '/Users/jamie/.ivy2/local/com.typesafe/config/0.4.2-SNAPSHOT/bundles/config.jar'. Your new classpath is:
".:/Users/jamie/.ivy2/local/com.typesafe/config/0.4.2-SNAPSHOT/bundles/config.jar"
Nothing to replay.
import akka.actor.OneForOneStrategy
import akka.actor.SupervisorStrategy._
import akka.actor.ActorSystem
import akka.actor.Actor
import akka.util.duration._
import akka.actor.Props
case object Start
case object MakeCrash
class AccountBalanceRetrieverFinal(savingsAccounts: ActorRef, checkingAccounts: ActorRef, moneyMarketAccounts: ActorRef) extends Actor {
def receive = {
case GetCustomerAccountBalances(id) => {
val originalSender = sender
implicit val ec: ExecutionContext = context.dispatcher
context.actorOf(Props(new Actor() {
val promisedResult = Promise[AccountBalances]()
var checkingBalances, savingsBalances, mmBalances: Option[List[(Long, BigDecimal)]] = None
def receive = {