This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val SEED = 23 | |
def hash(seed: Int, value: Boolean): Int = firstTerm(seed) + (if (value) 1 else 0) | |
def hash(seed: Int, value: Char): Int = firstTerm(seed) + value.asInstanceOf[Int] | |
def hash(seed: Int, value: Int): Int = firstTerm(seed) + value | |
def hash(seed: Int, value: Long): Int = firstTerm(seed) + (value ^ (value >>> 32) ).asInstanceOf[Int] | |
def hash(seed: Int, value: Float): Int = hash(seed, JFloat.floatToIntBits(value)) | |
def hash(seed: Int, value: Double): Int = hash(seed, JDouble.doubleToLongBits(value)) | |
def hash(seed: Int, anyRef: AnyRef): Int = { | |
var result = seed |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> def nullify[T >: Null <: AnyRef](x : Option[T]) = x getOrElse null | |
nullify: [T >: Null <: AnyRef](Option[T])T | |
scala> val result1 : Option[String] = Some("hello") | |
result1: Option[String] = Some(hello) | |
scala> val result2 : Option[String] = None | |
result2: Option[String] = None | |
scala> nullify(result1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package retweetrec | |
import scala.xml.XML | |
import java.net.URL | |
import java.io.InputStream | |
object ReTweetRec { | |
def getFollowers(id: String) = { | |
val data = new URL("http://twitter.com/followers/ids/" + id + ".xml").getContent |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class User(val usernamePassword: Tuple2[String, String], | |
val email: String, | |
val age: Int) | |
extends Serializable.SBinary[User] { | |
def this() = this(null, null, 0) | |
import sbinary.DefaultProtocol._ | |
implicit object UserFormat extends Format[User] { | |
def reads(in : Input) = User( | |
read[Tuple2[String, String]](in), | |
read[String](in), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class MyMessage(val id: String, val value: Tuple2[String, Int]) extends ScalaJSON | |
val message = MyMessage("id", ("hello", 34)) | |
val json = message.toJSON | |
val messageCopy = Serializer.ScalaJSON.in(json) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Transactors: Actors + STM | |
Actors are excellent for solving of problems where you have many independent processes | |
that can work in isolation and only interact with other Actors through message passing. | |
This model fits many problems. But the actor model is unfortunately a terrible model for | |
implementing truly shared state. E.g. when you need to have consensus and a stable view of | |
state across many components. The classic example is the bank account to clients to | |
deposits and withdrawals in which each operation needs to be atomic. For detailed | |
discussion on the topic see this JavaOne presentation: | |
http://www.slideshare.net/jboner/state-youre-doing-it-wrong-javaone-2009. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* REST interface to Akka's JMX service. | |
* <p/> | |
* Here is an example that retreives the current number of Actors. | |
* <pre> | |
* http://localhost:9998/management | |
* ?service=service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi | |
* &component=se.scalablesolutions.akka:type=Stats | |
* &attribute=counter_NrOfActors | |
* </pre> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Sketch of an immutable domain model in Scala | |
// We used this scheme together with this JPA module: | |
// http://github.com/jboner/skalman/blob/d1e03a85be3964b9012f9e79dd726b0546342b2b/core/src/main/scala/JPA.scala | |
// ...and this GenericRepository: | |
// http://github.com/jboner/skalman/blob/d1e03a85be3964b9012f9e79dd726b0546342b2b/core/src/main/scala/GenericRepository.scala | |
abstract class Entity[T](val id: Int) | |
object User { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import se.scalablesolutions.akka.serialization.Serializer | |
import se.scalablesolutions.akka.actor.Actor | |
import com.rabbitmq.client.ConnectionParameters | |
object ExampleSession { | |
import AMQP._ | |
val SERIALIZER = Serializer.Java | |
val CONFIG = new ConnectionParameters | |
val HOSTNAME = "localhost" | |
val PORT = 5672 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object HttpBuilder { | |
var buildHttp: () => HttpThingIntf = new NormalHttpThing | |
} | |
// I want to change what is built by the HttpBuilder: | |
HttpBuilder.buildHttp = () => new MockHttpThing | |
// And to build a new HttpThing: |
OlderNewer