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/ReTweetRec.scala
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),
@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 {
@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:
@jboner
jboner / LoggableException.scala
Created September 23, 2009 07:56
Loggable exception class, with trackable unique id
/**
* Base trait for all classes that wants to be able use the logging infrastructure.
*
* @author <a href="http://jonasboner.com">Jonas Bon&#233;r</a>
*/
trait Logging {
@transient var log = Logger.get(this.getClass.getName)
}
/**
import com.facebook.thrift.{TBase, TDeserializer}
import java.io.{BufferedInputStream, DataInputStream, File, FileInputStream, InputStream}
import java.util.zip.GZIPInputStream
import net.lag.logging.Logger
import scala.reflect.Manifest
// you'll want to import your generated Thrift package too!
class ThriftFileScanner[T <: TBase](implicit man: Manifest[T]) {
val log = Logger.get
var bufferedIn: BufferedInputStream = null
import java.io.{BufferedReader, File, InputStream, InputStreamReader, IOException, PrintWriter, Writer}
import java.net.{InetAddress, ServerSocket, Socket, SocketException}
import java.util.concurrent.Executors
import net.lag.logging.Logger
import scala.actors.Actor
import scala.actors.Actor._
import scala.tools.nsc._
class RemoteDebugServer(port: Int) {