Skip to content

Instantly share code, notes, and snippets.

View jboner's full-sized avatar

Jonas Bonér jboner

View GitHub Profile
@jboner
jboner / gist:939
Created July 22, 2008 13:29
Scala HashCode builder
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
@jboner
jboner / gist:64367
Created February 14, 2009 13:55
Nullify: dealing with null in Scala
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)
@jboner
jboner / ReTweetRec.scala
Created July 17, 2009 22:26 — forked from Villane/gist:149304
ReTweetRec
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
@jboner
jboner / gist:156629
Created July 27, 2009 16:59
Example of Akka's binary Scala serialization protocol
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),
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)
@jboner
jboner / Transactors: Actors and STM
Created July 30, 2009 14:37
Transactors: Actors + STM
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.
@jboner
jboner / Actor providing JMX over REST (fault-tolerant)
Created August 11, 2009 08:48
Actor providing JMX over REST (fault-tolerant)
/**
* 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>
@jboner
jboner / Immutable domain model in Scala
Created August 24, 2009 15:06
Immutable Domain Model with JPA
// 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 {
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
@jboner
jboner / gist:182740
Created September 8, 2009 05:44
DI using var's and partial functions
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: