Skip to content

Instantly share code, notes, and snippets.

@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

@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 }
@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] = {
@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]
@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 / scodec-streaming.markdown
Last active December 30, 2015 12:19
Some early design notes on adding a streaming layer to scodec

Much of this is out of date, but leaving it here for posterity. See the scodec-stream and scodec-bits projects, where all the details got worked out.


Current signature of Codec in scodec is:

trait Codec[A] {
  /** Attempts to encode the specified value in to a bit vector. */
 def encode(a: A): String \/ BitVector
@folone
folone / gist:6258410
Last active April 27, 2018 14:09
Ackermann function
def ackermann(m: Int, n: Int): Int = {
(m, n) match {
case (0, _) ⇒ n + 1
case (m, 0) if m > 0 ⇒ ackermann(m - 1, 1)
case (m, n) if m > 0 && n > 0 ⇒ ackermann(m - 1, ackermann(m, n - 1))
}
}
import scalaz._, Scalaz._, Free.{suspend ⇒ _, _}, Trampoline._
def ackermannTramp(m: Int, n: Int): Trampoline[Int] = {
@travisbrown
travisbrown / noncompilation.scala
Last active January 13, 2016 18:00
Testing for compiler errors with untyped macros.
scala> import scala.language.experimental.macros
import scala.language.experimental.macros
scala> import scala.reflect.macros.{ Context, TypecheckException }
import scala.reflect.macros.{Context, TypecheckException}
scala> object NoncompilationTests {
| def compiles(code: _): Boolean = macro compiles_impl
| def compiles_impl(c: Context)(code: c.Tree) = c.literal(
| try {
@dscleaver
dscleaver / Fib.scala
Created February 27, 2013 14:45
Port of http://t.co/KvoY7PDGSg. Created in a Scala IDE worksheet.
import scalaz._
import Scalaz._
object monads {
def fix[A](f: (=> A) => A): A = f(fix(f)) //> fix: [A](f: => A => A)A
type Gen[A] = (=> A) => A
def gFib: Gen[Int => Int] = (self) => n =>
@retronym
retronym / sbt-quickstart.txt
Created January 22, 2013 21:27
try a library with sbt-extras
~/code/scratch1 sbt -sbt-create -scala-version 2.10.0 'set libraryDependencies += "org.scalaz" %% "scalaz-core" % "7.0.0-M7"' 'console'
Detected sbt version 0.12.2-RC2
Using /Users/jason/.sbt/0.12.2-RC2 as sbt dir, -sbt-dir to override.
[info] Set current project to default-821d14 (in build file:/Users/jason/code/scratch1/)
[info] Defining */*:log-level
[info] The new value will be used by no settings or tasks.
[info] Reapplying settings...
[info] Set current project to default-821d14 (in build file:/Users/jason/code/scratch1/)
[info] Defining {.}/*:scala-version
[info] The new value will be used by no settings or tasks.