Skip to content

Instantly share code, notes, and snippets.

View gclaramunt's full-sized avatar

Gabriel Claramunt gclaramunt

View GitHub Profile
@gclaramunt
gclaramunt / HTree.scala
Created August 14, 2012 14:13
Heterogeneous tree toy example
trait HTree
case class HTNode[T, LT <: HTree,RT <: HTree](node:T, lt:LT, rt:RT ) extends HTree
case class HTNil() extends HTree
/*
exmaple:
scala> HTNode ( 1, HTNode("a",HTNode(1.0,HTNil(),HTNil()), HTNode('z',HTNil(),HTNil())), HTNode(List(1,2,3),HTNil(),HTNil()))
@gclaramunt
gclaramunt / Ids.scala
Created September 9, 2012 03:05
Tagged Ids using Phantom types
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")
@gclaramunt
gclaramunt / gist:3789915
Created September 26, 2012 19:06
Install Scala support for VIM in one line
mkdir -p ~/.vim/{ftdetect,indent,syntax} && for d in ftdetect indent syntax ; do wget -O ~/.vim/$d/scala.vim https://raw.github.com/scala/scala-dist/master/tool-support/src/vim/$d/scala.vim ; done
@gclaramunt
gclaramunt / TypeClass.hs
Created October 11, 2012 12:12 — forked from tonymorris/TypeClass.hs
Type-class hierarchy
{-# LANGUAGE NoImplicitPrelude, MultiParamTypeClasses, Rank2Types, TypeOperators #-}
newtype Id a =
Id a
data a :\/ b =
Left a
| Right b
data a :/\ b =
import java.sql.Connection
trait Closeable[T]{
def close(t:T)
}
object Closeable{
implicit val connectionCloseable= new Closeable[Connection]{
override def close(c:Connection){ c.close() }
}
@gclaramunt
gclaramunt / fizzbuzz-faster.scala
Created November 20, 2012 20:47 — forked from travisbrown/fizzbuzz-faster.scala
FizzBuzz in the type system (faster)
import shapeless._, Nat._
trait FizzBuzz[N <: Nat] {
type R3 <: Nat
type R5 <: Nat
def ti: ToInt[N]
def prev: List[String]
def d3: R3 =:= _0
def d5: R5 =:= _0
I've been pondering this myself the last few days, and I'd like to go ahead and amplify this. Don't worry about leaping to the "Haskelly" solution at first. Start by just bashing something out that essentially works. Go nuts. Do it all in IO. Write partial functions. Copy and paste without fear. Use undefined to fill in gaps in the functions. Realize halfway through you're sort of doing it wrong, but don't stop to correct it, just keep going straight for something that works for some task. Not even the whole task, just something.
(Do avoid mutation, though, that will wreck up the rest of this process. Stay out of IORef, and for the moment I'd even suggest avoiding Control.Monad.State, although that's much less bad.)
You will quite likely look at the resulting code, and wonder why you bothered doing it in Haskell. It looks like crap, was harder to write than whatever language you are comfortable in (for the compiler was probably still pretty cranky at you even so), and looks nothing like the beautiful code you
@gclaramunt
gclaramunt / cc.sh
Last active October 20, 2021 21:49
This is my stupid script to have sbt-like recompiling in Haskell (now with cabal)
CALL=${1:-"cabal build"}
$CALL
while inotifywait -qq -r -e modify .; do
echo '<><><><> Starting build <><><><>';
echo `date`;
$CALL;
echo '<><><><> Done <><><><>';
sleep 1
done
def avg(xs:List[Int]):Float={
val (sum,length)=xs.foldLeft((0,0))( { case ((s,l),x)=> (x+s,1+l) })
sum/length
}
@gclaramunt
gclaramunt / sortIps.scala
Created June 29, 2013 20:55
Sort a list of ips in Scala
List("129.95.30.40","5.24.69.2","19.20.203.5","1.2.3.4","19.20.21.22","5.220.100.50").
map(_.split('.').map(_.toInt)).
map( { case Array(x1,x2,x3,x4) => (x1,x2,x3,x3) } ).sorted.
map { case (x1,x2,x3,x4) => "%s.%s.%s.%s".format(x1,x2,x3,x4)}