Skip to content

Instantly share code, notes, and snippets.

class A
class A2 extends A
class B
trait M[X]
//
// Upper Type Bound
//
def upperTypeBound[AA <: A](x: AA): A = x
case class ElectricCar(b: Battery) { def batteryLevel = b.filledPercentage }
case class GasolineCar(g: GasTank) { def gasLevel = g.filledPercentage }
case class Battery(filledPercentage: Int) { def fill: Battery = Battery(100) }
case class GasTank(filledPercentage: Int) { def fill: GasTank = GasTank(100) }
trait Fills[C] {
def fill(car: C): C
@rsuniev
rsuniev / Head.scala
Created June 3, 2011 18:39 — forked from purefn/Head.scala
print the first 10 chunks of input
import scalaz.{Failure => _, _}
import Scalaz._
import effects._
import iteratees._
import java.io._
object Head {
def main(args: Array[String]) {
val enum = enumStream[Seq[Byte], IO]((1 to 50).toStream.map(i => ("line " + i + "\n").getBytes.toSeq))
@rsuniev
rsuniev / gist:1010938
Created June 6, 2011 19:46 — forked from markhibberd/gist:1009072
Alt D List Example
package scalaz.example
import scalaz.AltDList._
object ExampleAltDList {
def main(args: Array[String]) = run
import scalaz._, Scalaz._
import IterV._
@rsuniev
rsuniev / MapImplicits.scala
Created June 8, 2011 21:48 — forked from nuttycom/MapImplicits.scala
Map implicits for scalaz
implicit def MapMonoid[K, V](implicit valueSemigroup: Semigroup[V]): Monoid[Map[K, V]] = new Monoid[Map[K, V]] {
override val zero = Map.empty[K, V]
override def append(m1: Map[K, V], m2: => Map[K, V]) = {
val (from, to, semigroup) = {
if (m1.size > m2.size) (m2, m1, (v1: V, v2: V) => valueSemigroup.append(v1, v2))
else (m1, m2, (v1: V, v2: V) => valueSemigroup.append(v2, v1))
}
from.foldLeft(to) {
case (to, (k, v)) => to + (k -> to.get(k).map(semigroup(_, v)).getOrElse(v))
@rsuniev
rsuniev / Head.scala
Created July 13, 2011 15:26 — forked from purefn/Head.scala
print the first 10 chunks of input
import scalaz.{Failure => _, _}
import Scalaz._
import effects._
import iteratees._
import java.io._
object Head {
def main(args: Array[String]) {
val enum = enumStream[Seq[Byte], IO]((1 to 50).toStream.map(i => ("line " + i + "\n").getBytes.toSeq))
@rsuniev
rsuniev / accounts.clj
Created July 13, 2011 16:18 — forked from stuartsierra/accounts.clj
Accounts concurrency example in Clojure
;; An example of the "accounts" program for Venkat Subramaniam's
;; Programming Concurrency Workshop, part 1
;;
;; Original Java code by Venkat Subramaniam (@venkat_s)
;; available at http://www.agiledeveloper.com/downloads.html
;; under "Workshop: Programming Concurrency"
;;
;; This code example by Stuart Sierra (@stuartsierra)
;;
;; Überconf 2011, Denver, Colorado
@rsuniev
rsuniev / gist:1120957
Created August 2, 2011 19:18 — forked from dcsobral/gist:1120811
Existential _ vs Higher-kind _
scala> def f[A[_] <: Seq[_]](f: A[Int]) = f.head
f: [A[_] <: Seq[_]](f: A[Int])A
scala> def f[A[_] <: Seq[t] forSome { type t }](f: A[Int]) = f.head
f: [A[_] <: Seq[_]](f: A[Int])A
scala> def f[A[t] <: Seq[_] forSome { type t}](f: A[Int]) = f.head
f: [A[t] <: Seq[_] forSome { type t }](f: A[Int])A
@rsuniev
rsuniev / ApplicativeMagic.scala
Created August 4, 2011 19:19 — forked from jsuereth/ApplicativeMagic.scala
reason #37 Why macros against Function1->22 would be useful..
trait ApplicativeMagic[F[_]] {
def apply[C](f: ApplicativeMagicFunctionHolder[FunctionArg => C]): F[C]
type FunctionArg
}
class ApplicativeMagicFunctionHolder[F](val f: F)
object ApplicativeMagicFunctionHolder {
implicit def fix2[A,B,C](f: (A,B) => C): ApplicativeMagicFunctionHolder[Tuple2[A,B] => C] =
new ApplicativeMagicFunctionHolder({ case (a,b) => f(a,b) })
implicit def fix3[A,B,C,D](f: (A,B,C) => D): ApplicativeMagicFunctionHolder[Tuple2[Tuple2[A,B],C] => D] =
new ApplicativeMagicFunctionHolder({ case ((a,b),c) => f(a,b,c) })
@rsuniev
rsuniev / gist:1166584
Created August 23, 2011 21:21 — forked from milessabin/gist:1164885
Boxed lazy values
// Is this even faintly novel? The closest I've seen is
//
// http://stackoverflow.com/questions/2618891/using-lazy-evaluation-functions-in-varargs
//
// which is a bit clunky by comparison. But this is so trivial someone must have
// done it this way before.
// UPDATE:
// Thanks to the Twittersphere (@etorreborre, @pchiusano and @loverdos) for a few sightings of related things,
//