Skip to content

Instantly share code, notes, and snippets.

@jedws
jedws / IOActor.scala
Last active August 29, 2015 13:56
An Actor equivalent of SafeApp
package stuff
import akka.actor.{ Actor, actorRef2Scala }
import kadai.log.Logging
import spray.http.{ HttpFailure, HttpResponse }
import spray.http.HttpEntity.apply
trait IOActor extends Actor {
log: Logging =>
@pchiusano
pchiusano / tqueue.scala
Last active August 29, 2015 14:04
Type aligned queue implementation
package typealigned
import scalaz.{Leibniz,Monad,Need}
import Leibniz.===
/** Typeclass for type-aligned sequences. */
trait Sequence[Q[_[_,_],_,_]] { self =>
import syntax._
def empty[C[_,_],a]: Q[C,a,a]
def singleton[C[_,_],a,b](c: C[a,b]): Q[C,a,b]
@pchiusano
pchiusano / counters.scala
Created August 15, 2014 17:27
Amortized O(1) and deamortized O(1) functional counters, generalized to arbitrary semigroups
object Amortized {
/**
* Amortized constant time counter. Maintains the invariant that
* `chunks` are of exponentially decreasing sizes. May perform a
* logarithmic number of `op` reductions per `snoc` (worst case),
* but a series of `n` snocs will on average require `2n` reductions.
*/
case class Counter[A](chunks: Vector[A]) {
def snoc(size: A => Long, op: (A,A) => A)(a: A): Counter[A] = {
@propensive
propensive / heteroargs.scala
Created January 31, 2015 20:03
Easy Heterogeneous Varargs in Scala
// Define the general Arg type and companion object:
import language.higherKinds, language.implicitConversions, language.existentials
object Arg { implicit def toArg[Tc[_], T: Tc](t: T): Arg[T, Tc] = Arg(t, implicitly[Tc[T]]) }
case class Arg[T, Tc[_]](value: T, typeclass: Tc[T])
// Say, for example we have a typeclass for getting the length of something, with a few instances
trait Lengthable[T] { def length(t: T): Int }
implicit val intLength = new Lengthable[Int] { def length(i: Int) = 1 }
implicit val stringLength = new Lengthable[String] { def length(s: String) = s.length }
@djspiewak
djspiewak / streams-tutorial.md
Created March 22, 2015 19:55
Introduction to scalaz-stream

Introduction to scalaz-stream

Every application ever written can be viewed as some sort of transformation on data. Data can come from different sources, such as a network or a file or user input or the Large Hadron Collider. It can come from many sources all at once to be merged and aggregated in interesting ways, and it can be produced into many different output sinks, such as a network or files or graphical user interfaces. You might produce your output all at once, as a big data dump at the end of the world (right before your program shuts down), or you might produce it more incrementally. Every application fits into this model.

The scalaz-stream project is an attempt to make it easy to construct, test and scale programs that fit within this model (which is to say, everything). It does this by providing an abstraction around a "stream" of data, which is really just this notion of some number of data being sequentially pulled out of some unspecified data source. On top of this abstraction, sca