View gist:939
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 |
View gist:64367
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) |
View ReTweetRec.scala
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 |
View gist:156629
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), |
View Actor providing JMX over REST (fault-tolerant)
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> |
View Immutable domain model in Scala
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 { |
View gist:182740
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: |
View LoggableException.scala
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
/** | |
* Base trait for all classes that wants to be able use the logging infrastructure. | |
* | |
* @author <a href="http://jonasboner.com">Jonas Bonér</a> | |
*/ | |
trait Logging { | |
@transient var log = Logger.get(this.getClass.getName) | |
} | |
/** |
View ThriftFileScanner.scala
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 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 |
View RemoteDebug.scala
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 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) { |
OlderNewer