Skip to content

Instantly share code, notes, and snippets.

@mpilquist
mpilquist / unary.scala
Created May 15, 2013 16:06
Example of using tagged boolean types in Scalaz 7
import scalaz.std.option._ // Import the some and none functions
import scalaz.syntax.std.option._ // Import the syntax for calling ~ on Option
import scalaz.std.AllInstances._ // Import all typeclass instances for standard datatypes
~some(10) // 10
~none[Int] // 0
~some("test") // "test"
~none[String] // ""
// Put in src/test/scala
object Foo {
// From main slf4j-api JAR
val logger = org.slf4j.LoggerFactory.getLogger("Foo")
// From test slf4j-api JAR
val bp = new org.slf4j.helpers.BogoPerf
}
@mpilquist
mpilquist / CompositeClassLoader.scala
Last active December 19, 2015 02:19
Example of creating an akka ActorSystem in OSGi without using akka-osgi built in support.
import scala.collection.JavaConverters._
import scalaz.std.option._
import java.net.URL
import java.util.Enumeration
class CompositeClassLoader(parent: ClassLoader, members: List[ClassLoader]) extends ClassLoader(parent) {
override def findClass(name: String): Class[_] = {
def tryLoadClass(cl: ClassLoader): Option[Class[_]] =
try some(cl.loadClass(name))
@mpilquist
mpilquist / OsgiConfigFactory.scala
Created July 15, 2013 18:07
Automatic discovery of reference.conf files in OSGi
import scala.collection.JavaConverters._
import java.net.URL
import java.util.Enumeration
import com.typesafe.config._
import org.osgi.framework.Bundle
import org.osgi.framework.wiring._
object OsgiConfigFactory {
@mpilquist
mpilquist / test.scala
Last active December 22, 2015 08:59 — forked from milessabin/gist:6448380
import shapeless._
trait F[A] {
def xmap[B](f: A => B, g: B => A): F[B]
def as[B] = new AsAux[B]
class AsAux[B] {
def apply[C](implicit gen: Generic.Aux[B, C], ev: A =:= C): F[B] =
xmap(a => gen.from(ev(a)), b => gen.to(b).asInstanceOf[A])
}
}
scala> import org.scalatest._
scala> import Matchers._
scala> import Inspectors._
scala> trap { List('a', 'b') should have length 1 }
res0: Throwable = org.scalatest.exceptions.TestFailedException: List(a, b) had length 2 instead of expected length 1
scala> trap { List(1, 2, 3) should contain (4) }
res1: Throwable = org.scalatest.exceptions.TestFailedException: List(1, 2, 3) did not contain element 4
@mpilquist
mpilquist / notes.md
Last active January 3, 2016 06:19
Distribution of `Option`, `Either`, `Try`, `scalaz.\/`, `scalaz.Validation`

Distribution of Option, Either, Try, scalaz.\/, scalaz.Validation on a large project that started in 2009 and has about ~425,000 SLOC of Scala:

› find . -name '*.scala' -exec grep -o "Option" {} \; | wc -l
    7747
› find . -name '*.scala' -exec grep -o "Either" {} \; | wc -l
     547
› find . -name '*.scala' -exec grep -o "Try" {} \; | wc -l
     129         
› find . -name '*.scala' -exec grep -o "\\\/" {} \; | wc -l

2159

import sbt.complete.DefaultParsers
object MyApp extends App {
println(DefaultParsers.validID("test"))
}
package scodec.bits
import java.nio.ByteBuffer
import java.util.concurrent.atomic.AtomicLong
import scala.collection.GenTraversableOnce
/**
* Persistent vector of bits, stored as bytes.
*
* Bits are numbered left to right, starting at 0.
@mpilquist
mpilquist / Example.scala
Last active August 29, 2015 13:57
Integrating scalaz.Lens with scalaz.stream.Process1
import scalaz.{ Lens, LensFamily }
import scalaz.stream.{ Process, process1 }
import Process._
object Example extends App {
/**
* Lifts a `Process1[B, B]` to a `Process1[A, A]` using the provided lens.
*
* Values fed to this process are converted to type `B` and fed to