Skip to content

Instantly share code, notes, and snippets.

View johnynek's full-sized avatar

P. Oscar Boykin johnynek

View GitHub Profile
@johnynek
johnynek / toBytesWritable.scala
Created March 4, 2013 18:06
Convert to bytes writable
def toBytesWritable(p: Pipe, f: Fields): Pipe = {
import Dsl._ // Make sure we have the scalding DSL in scope:
import Fields
asList(f).foldLeft(p) { (oldPipe, fld) =>
oldPipe.map(fld -> fld) { str: String => new ImmutableBytesWritable(str.getBytes) }
}
}
// Now call:
supervisor1 {
akka {
actor {
provider = "akka.remote.RemoteActorRefProvider"
}
remote {
enabled-transports = ["akka.remote.netty.tcp"]
netty.tcp {
hostname = "0.0.0.0"
port = 2554
// Run with
// unflatten -l 7 ritchieboykin.gv | dot -Tpng -o ritchieboykin.png && open ritchieboykin.png
digraph RitchieBoykin {
page = "8.2677165,11.692913";
overlap=false;
ratio = "auto";
mincross = 2.0;
graph [style="filled, rounded", color=lightgrey, fillcolor=khaki, fontsize=10];
subgraph cluster_0 {
@johnynek
johnynek / heterfn.scala
Created June 18, 2013 04:29
An example showing how to make a function that takes one of many input types.
// Taken from: http://jnordenberg.blogspot.com/2008/08/hlist-in-scala.html
object HListTest {
sealed trait HList
final class HNil extends HList {
def ::[T](v : T) = HCons(v, this)
}
val HNil = new HNil()
@johnynek
johnynek / LoggingMonad.scala
Last active December 18, 2015 22:28
An example of a Monadic wrapper on Log4j
import org.apache.log4j.Logger
sealed trait LogLevel
case object Info extends LogLevel
case object Warn extends LogLevel
case object Error extends LogLevel
sealed trait Logged[T] {
def map[U](fn: T => U): Logged[U]
@johnynek
johnynek / async_stream.scala
Last active December 18, 2015 23:09
A simple API using Future and Promise to create an async stream.
// could easily be ported to Scala Future
import com.twitter.util.{Promise, Future}
trait Source[+T] { self =>
def head: Option[T]
def tail: Future[Source[T]]
def map[U](fn: T => U): Source[U] = new Source[U] {
def head = self.head.map(fn)
def tail = self.tail.map { _.map(fn) }
}
@johnynek
johnynek / gist:5940600
Created July 6, 2013 17:29
chill failure with scalaVersion := "2.9.3"
[info] Loading project definition from /Users/oscarb/workspace/chill/project
[info] Compiling 1 Scala source to /Users/oscarb/workspace/chill/project/target/scala-2.9.2/sbt-0.12/classes...
[info] Set current project to chill-all (in build file:/Users/oscarb/workspace/chill/)
[info] Updating {file:/Users/oscarb/workspace/chill/}chill-all...
[info] Updating {file:/Users/oscarb/workspace/chill/}chill-java...
[info] Resolving org.scala-tools.testing#specs_2.9.3;1.6.9 ...
[info] downloading http://oss.sonatype.org/content/repositories/releases/org/scalacheck/scalacheck_2.9.3/1.10.0/scalacheck_2.9.3-1.10.0-sources.jar ...
[info] [SUCCESSFUL ] org.scalacheck#scalacheck_2.9.3;1.10.0!scalacheck_2.9.3.jar(src) (1248ms)
[info] downloading http://oss.sonatype.org/content/repositories/releases/org/scala-tools/testing/specs_2.9.3/1.6.9/specs_2.9.3-1.6.9-sources.jar ...
[info] [SUCCESSFUL ] org.scala-tools.testing#specs_2.9.3;1.6.9!specs_2.9.3.jar(src) (1838ms)
class WriteOnce[T] {
private val ref = new java.util.concurrent.atomic.AtomicReference[Option[T]](None)
def init(t: T) { if(!ref.compareAndSet(None, Some(t))) sys.error("Already written"); }
def get: Option[T] = ref.get
}
scala> val w = new WriteOnce[Int]
w: WriteOnce[Int] = WriteOnce@215d55cb
scala> w.get
@johnynek
johnynek / compression.scala
Created August 12, 2013 05:40
Compress Lists of any item (as long as they are immutable and have sane equals and hashCode). This is basically the Lempel-Ziv algorithm
/**
scala> compress(List.fill(1000)(1))
res17: List[Either[Int,Int]] = List(Left(1), Right(0), Left(1), Right(1), Left(1), Right(2), Left(1), Right(3), Left(1), Right(4), Left(1), Right(5), Left(1), Right(6), Left(1), Right(7), Left(1), Right(8), Left(1), Right(9), Left(1), Right(10), Left(1), Right(11), Left(1), Right(12), Left(1), Right(13), Left(1), Right(14), Left(1), Right(15), Left(1), Right(16), Left(1), Right(17), Left(1), Right(18), Left(1), Right(19), Left(1), Right(20), Left(1), Right(21), Left(1), Right(22), Left(1), Right(23), Left(1), Right(24), Left(1), Right(25), Left(1), Right(26), Left(1), Right(27), Left(1), Right(28), Left(1), Right(29), Left(1), Right(30), Left(1), Right(31), Left(1), Right(32), Left(1), Right(33), Left(1), Right(34), Left(1), Right(35), Left(1), Right(36), Left(1), Right(37), Left(1), Ri...
scala> compress(List.fill(1000)(1)).size
res18: Int = 88
scala> decompress(res17)
res19: List[Int] = List(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
scala> class PostFix[A](a: A) {
| def |>[B](fn: A => B): B = fn(a)
| }
defined class PostFix
scala> implicit def toPf[A](a: A) = new PostFix(a)
toPf: [A](a: A)PostFix[A]
scala> val inc = { x: Int => x + 42 }
inc: Int => Int = <function1>