Skip to content

Instantly share code, notes, and snippets.

View gszeliga's full-sized avatar

Guillermo Szeliga gszeliga

View GitHub Profile
sealed trait Maybe[+A] {
// >>=
def flatMap[B](f: A => Maybe[B]): Maybe[B]
}
case class Just[+A](a: A) extends Maybe[A] {
override def flatMap[B](f: A => Maybe[B]) = f(a)
}
@gszeliga
gszeliga / find_classes_within_jars
Created June 12, 2013 08:54
How to find classes within jars using bash
find . -name "*jar" -print -exec jar -tf '{}' \; | grep -E "jar$|LoggerConfiguration"
@gszeliga
gszeliga / gist:8841972
Last active August 29, 2015 13:56
Standard Phantom Types
import java.util.Date
abstract class Census
abstract class Translation
abstract class Passport
object VisaRequest {
def apply(to: Someone, at: String) = new VisaRequest(to, at)
@gszeliga
gszeliga / gist:8842040
Last active August 29, 2015 13:56
Single Phantom Type
import java.util.Date
trait Translation
trait MissingTranslation
trait AnyTrans extends Translation with MissingTranslation
trait Certificate
trait MissingCertificate
trait AnyCert extends Certificate with MissingCertificate
trait Future[+A] {
private def apply(k: A => Unit): Unit
}
type Par[+A] = ExecutorService => Future[A]
def map2[A, B, C](pa: Par[A], pb: Par[B])(f: (A, B) => C): Par[C] = {
ex =>
{
import java.util.concurrent.ExecutorService
import java.util.concurrent.Callable
import java.util.concurrent.atomic.AtomicReference
import java.util.concurrent.atomic.AtomicInteger
import scala.annotation.tailrec
final case class Actor[A](strategy: Strategy)(handler: A => Unit, onError: Throwable => Unit = throw (_)) {
private class Node[A](var a: A = null.asInstanceOf[A]) extends AtomicReference[Node[A]]
import scala.util.parsing.input.Reader
import scala.util.parsing.input.CharArrayReader.EofCh
import scala.util.parsing.input.Position
case class ByteOffsetPosition(offset: Int) extends Position {
final val line = 1
def column = offset + 1
def lineContents: String = ""
}
trait BencodeConstants {
final val DEFAULT_STRING_ENCODING = "ISO-8859-15"
final val DEFAULT_NUMBER_ENCODING = "US-ASCII"
final val NUMBER_BEGIN: Char = 'i'
final val NUMBER_END: Char = 'e'
final val LIST_BEGIN = 'l'
final val LIST_END = 'e'
@gszeliga
gszeliga / single_digit_parser.scala
Created May 12, 2014 22:36
Single digit parser
trait BencodeParser extends Parsers with BencodeConstants {
type Elem = Byte
implicit def charToParser(ch: Char) = elem(ch.toByte)
def delimitedBy[A](left: Parser[Byte], right: Parser[Byte])(p: Parser[A]): Parser[A] = left ~> p <~ right
def single_digit = new Parser[String] {