This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Questions about \/ and Validation | |
object scalaz { | |
/* | |
This explanation might help. This is a compilable source file with revisions | |
available at https://gist.github.com/tonymorris/8263051 | |
Fact: All monads are applicative functors. As has been seen we can witness the | |
`Applicative` that arises from the `Monad` primitives. Let's illustrate this: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.twitter.scalding._ | |
import com.twitter.algebird.{ MinHasher, MinHasher32, MinHashSignature } | |
/** | |
* Computes similar items (with a string itemId), based on approximate | |
* Jaccard similarity, using LSH. | |
* | |
* Assumes an input data TSV file of the following format: | |
* | |
* itemId userId |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.ref.WeakReference | |
/** An option-like type that doesn't keep | |
* the pointed item in scope | |
*/ | |
trait WeakRef[+T] { | |
// If the reference is still live, return it, else None | |
def toOption: Option[T] | |
// If either reference is dead, return Empty, else give the live ref |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class WriteOnce[T] { | |
private val ref = new java.util.concurrent.atomic.AtomicReference[Option[T]](None) | |
def init(t: T) { if(!ref.compareAndSet(None, Some(t))) sys.error("Already written"); } | |
def get: Option[T] = ref.get | |
} | |
scala> val w = new WriteOnce[Int] | |
w: WriteOnce[Int] = WriteOnce@215d55cb | |
scala> w.get |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Taken from: http://jnordenberg.blogspot.com/2008/08/hlist-in-scala.html | |
object HListTest { | |
sealed trait HList | |
final class HNil extends HList { | |
def ::[T](v : T) = HCons(v, this) | |
} | |
val HNil = new HNil() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait Semigroup[A] { | |
def op(a1: A, a2: A): A | |
} | |
trait Monoid[A] extends Semigroup[A] { | |
def id: A | |
} | |
object Monoid { | |
implicit def ListMonoid[A]: Monoid[List[A]] = |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait Entity | |
trait User extends Entity | |
trait Product extends Entity | |
case class Id[T<:Entity](id:String) | |
def buy(pId:Id[Product],uId:Id[User])="Bought product %s for user %s".format(pId.id,uId.id) | |
val pId=new Id[Product]("1") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int print_board(int board[][8]) { | |
for (int i = 0; i < 8; i++) { | |
for (int j = 0; j < 8; j++) { | |
if (board[i][j]) | |
printf("Q "); | |
else | |
printf(". "); | |
} | |
printf("\n"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** GADTs in Scala and their limitations */ | |
/** Background: what is an algebraic data type (ADT) ? | |
* ADT: (possibly) recursive datatype with sums and products | |
* In scala - a trait with case classes (case class is product, subtyping is sum) | |
*/ | |
/** Motivation: untyped embedded DSL doesn't prevent nonsensical expressions */ | |
sealed trait Expr { | |
def apply(other: Expr) = Ap(this, other) |
NewerOlder