Skip to content

Instantly share code, notes, and snippets.

trait Proletariat {
type BigNumberT
def retirementAge: BigNumberT
val bippy = "hi mum"
def twiddle = 42
def twaddle = 1
}
trait BiggerTwaddle {
@kevinwright
kevinwright / minevaluator.sh
Created May 1, 2014 20:36
minified evaluator
#!/bin/bash
set -f
SUBBED=($(sed -e "s,multiply,*,g" -e "s,divide,/,g" -e "s,add,+,g" -e "s,subtract,-,g" -e "s,apply,,g" -e "s, *,,g" $1))
EXPR=""
for (( idx=${#SUBBED[@]}-1 ; idx>=0 ; idx-- )) ; do EXPR="($EXPR ${SUBBED[idx]})"; done
echo $(($EXPR))
@kevinwright
kevinwright / stacktrace.txt
Created July 1, 2014 19:03
epic fail in macros...
java.lang.NullPointerException
at scala.reflect.internal.Scopes$Scope.unlink(Scopes.scala:202)
at scala.reflect.internal.Scopes$Scope.unlink(Scopes.scala:222)
at org.scalamacros.paradise.typechecker.Namers$Namer$MaybeExpandeeCompleter$$anonfun$destroy$1.apply(Namers.scala:301)
at org.scalamacros.paradise.typechecker.Namers$Namer$MaybeExpandeeCompleter$$anonfun$destroy$1.apply(Namers.scala:299)
at scala.collection.IndexedSeqOptimized$class.foreach(IndexedSeqOptimized.scala:33)
at scala.collection.mutable.WrappedArray.foreach(WrappedArray.scala:35)
at org.scalamacros.paradise.typechecker.Namers$Namer$MaybeExpandeeCompleter.destroy(Namers.scala:299)
at org.scalamacros.paradise.typechecker.Namers$Namer$$anon$2.maybeExpand(Namers.scala:393)
at org.scalamacros.paradise.typechecker.Namers$Namer$MaybeExpandeeCompleter.completeImpl(Namers.scala:320)
@kevinwright
kevinwright / DeferredActorRef.scala
Last active August 29, 2015 14:07
Sending to an actor you don't know yet...
import akka.actor._
import scala.collection.mutable
case class DeferredActorRef(implicit factory: ActorRefFactory) {
private[this] val innerActor = factory.actorOf(Props(new TheActor))
private[this] case class SetRef(ref: ActorRef)
private[this] class TheActor extends Actor with ActorLogging {
val buffer: mutable.Queue[(Any, ActorRef)] = mutable.Queue.empty
@kevinwright
kevinwright / LabelledGenericOddness.scala
Created October 21, 2014 10:55
LabelledGeneric Oddness
import shapeless._
case class Wibble(x: Int, y: String)
val wib = Wibble(42, "towel")
def recOf[T](obj: T)(implicit gen: LabelledGeneric[T]) = gen.to(obj)
recOf(wib) // works beautifully
class ClassyRecOf[T](implicit gen: LabelledGeneric[T]) {
def doIt(obj: T) = gen.to(obj)
@kevinwright
kevinwright / autoThingy.scala
Last active August 29, 2015 14:07
AutoThingy
import shapeless._
trait Thingy[T] { def stringy(t:T): String }
implicit def autoThingy[T <: Product](implicit gen: LabelledGeneric[T]) : Thingy[T] = new Thingy[T] {
def toRecord[T <: Product, Repr](obj: T)(implicit lg: LabelledGeneric.Aux[T, Repr]): Repr = lg.to(obj)
def stringy(t: T) = {
val rec = toRecord(t)
val keys = rec.keys
keys.mkString
@kevinwright
kevinwright / test.scala
Last active August 29, 2015 14:07
Exploring LabelledGeneric
case class Wibble(x: Int, y: String)
val wib = Wibble(42, "towel")
val gen = LabelledGeneric.product[Wibble]
// shapeless.LabelledGeneric[Wibble]{type Repr = shapeless.::[shapeless.record.FieldType[shapeless.tag.@@[Symbol,String("x")],Int],shapeless.::[shapeless.record.FieldType[shapeless.tag.@@[Symbol,String("y")],String],shapeless.HNil]]} = $1$$1@56670aec
:t gen
//shapeless.LabelledGeneric[Wibble]{type Repr = shapeless.::[shapeless.record.FieldType[shapeless.tag.@@[Symbol,String("x")],Int],shapeless.::[shapeless.record.FieldType[shapeless.tag.@@[Symbol,String("y")],String],shapeless.HNil]]}
val out = gen.to(wib)

What is a file?

It's more than just a path, you can have a well formed path that refers to a non-existant file.

It's not just a sequence of bytes on a storage medium, it also involves other metadata - such as a path, modification timestamp, etc.

Then we have "files" under procfs on linux systems, and named pipes, and network streams - all accessible fia "file handles". There are different and deeply interwoven concepts that are all known as "file" in different contexts. Some of these can be immutable.

First, there's the path. This is completely immutable. Paths can have many operations, you can get the parent path, find the path of the some subdirectory(as a new immutable path instance), etc. Path needn't have an operation to determine if it's well-formed, as this can be done at construction time.

case class Person(firstName: String, lastName: String)
trait Constructable[T] {
def construct: T
}
implicit object IntIsConstructable extends Constructable[Int] {
def construct = 42
}
implicit object StringIsConstructable extends Constructable[String] {
def construct = "Hello World"