Skip to content

Instantly share code, notes, and snippets.

View pchiusano's full-sized avatar

Paul Chiusano pchiusano

View GitHub Profile
C:\projects>git clone https://pchiusano@github.com/scalaz/scalaz.git
Initialized empty Git repository in C:/projects/scalaz/.git/
Password:
* Couldn't find host github.com in the _netrc file; using defaults
* About to connect() to proxy corp-55w-proxy.mhc port 8080 (#0)
* Trying 152.159.23.21... * Connected to corp-55w-proxy.mhc (152.159.23.21) port 8080 (#0)
* Establish HTTP proxy tunnel to github.com:443
* Proxy auth using Basic with user 'blahblah'
> CONNECT github.com:443 HTTP/1.1
Host: github.com:443
@pchiusano
pchiusano / DemoParser.scala
Created February 8, 2011 02:40
The parser live-coded at the Feb 7 BASE meeting
package combinatorsdemo
import scala.util.parsing.combinator.RegexParsers
import scala.util.parsing.input._
import scala.util.matching.Regex
// type Parser[A] = List[T] => M[(A,List[T])]
// parser to recognize a single token
// parser that always succeeds without consuming input - constant parser
// parser that always fails without consuming input
@pchiusano
pchiusano / XMLEventParser.scala
Created February 11, 2011 15:26
Adapter to use scala's parser combinators for XML parsing
package xmlcombinators
import scala.util.parsing.combinator.Parsers
import scala.util.parsing.input.{NoPosition, Reader}
import javax.xml.stream.events.{Attribute, EndElement, XMLEvent}
import javax.xml.stream.{XMLEventReader, XMLInputFactory}
import collection.mutable.ArrayBuffer
import java.io.File
/**
@pchiusano
pchiusano / Remote.hs
Created November 10, 2011 15:54
Using the free monad for observable sharing in DSLs
{-#LANGUAGE ExistentialQuantification, Rank2Types #-}
-- using free monad for observable sharing
data Free f a = Pure a | Free (f (Free f a))
data RemoteF a =
N Int
| Ref Int
| Ap a a
@pchiusano
pchiusano / GADTs.scala
Created November 16, 2011 04:30
GADT support in Scala
/** 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)
@pchiusano
pchiusano / microbenchmark.markdown
Created December 2, 2011 13:49
Simple microbenchmarks comparing Scala vs Java mutable map performance

I was curious about the results reported here, which reports that Scala's mutable maps are slower than Java's: http://www.infoq.com/news/2011/11/yammer-scala

In my tests, Scala's OpenHashMap equals or beats java's HashMap:

Insertion 100k elements (String keys) time in ms:

  • scala HashMap: 92.75
  • scala OpenHashMap: 14.03125
  • java HashMap: 15.78125
@pchiusano
pchiusano / InvertControl.hs
Created December 27, 2011 23:05
Programmatic translation to iteratees from pull-based code
{-# LANGUAGE Rank2Types, ImplicitParams #-}
import Control.Monad.Reader
import Control.Monad.Trans
-- Skip nodes let us do filtering without lookahead
-- uncons is then a simple loop that removes any Skip nodes
data Step a s = Empty | Yield a s | Skip s
newtype ListT f a = ListT { runListT :: f (Step a (ListT f a)) }
@pchiusano
pchiusano / gist:2236706
Created March 29, 2012 12:16
Infinite jar
object ProjectBuild extends Build {
val pname = "ark"
lazy val project = Project (
pname,
file ("."),
settings = Defaults.defaultSettings ++ List(
name := pname,
organization := "org." + pname,
version := "0.1",
// logLevel in publishLocal := Level.Debug
// simplest thing
trait ViewImpl {
def de(items: List[OBItem]): Omnibox[OBItem]
}
// if you want a tighter bound on the result than OBItem, like if all the elements of the list are of type SomeItem, and you want the result to also be of type Omnibox[SomeItem]
trait ViewImpl {
def de[OB <: OBItem](items: List[OB]): Omnibox[OB]
}
@pchiusano
pchiusano / Server.hs
Created November 9, 2012 02:53
Typed, bidirectional stream transducers
{-# Language GADTs, Rank2Types #-}
module Server where
data Server li lo ri ro where
Request :: lo a -> (forall b . li b -> Server li lo ri ro) -> Server li lo ri ro
Respond :: ro a -> (forall b . ri b -> Server li lo ri ro) -> Server li lo ri ro
Stop :: Server li lo ri ro
(|>) :: Server li lo ri ro -> Server ro ri ri2 ro2 -> Server li lo ri2 ro2