Skip to content

Instantly share code, notes, and snippets.

View gclaramunt's full-sized avatar

Gabriel Claramunt gclaramunt

View GitHub Profile
@gclaramunt
gclaramunt / Graph.scala
Created December 19, 2008 02:20
Dijkstra shortest path - my first Scala program
package dfs2;
abstract class Graph {
type Edge
type Node <: NodeIntf
abstract class NodeIntf {
def connectWith(node: Node): Edge
}
def nodes: Set[Node]
def edges: Set[Edge]
@gclaramunt
gclaramunt / gist:99821
Created April 22, 2009 14:24
99 problems in Scala
package exercises
object NinetyNine {
//P01
def last[A](xs:List[A]):A = xs match {
case x::Nil => x
case x::xs => last(xs)
case Nil => throw new NoSuchElementException
}
@gclaramunt
gclaramunt / PlayWithUTF
Created June 2, 2009 04:07
Math and Logic symbols for Scala
package scrapbook
object PlayWithUTF {
val Π = Math.Pi
def √(x:Double)=Math.sqrt(x)
val x= √(9*Π)
def ∑(r:Range)(f:Int =>Int)=r.reduceLeft(_+ f(_))
package webtest
import java.net._
import scala.xml._
object WebTester {
def open(url:URL):HttpURLConnection = url.openConnection match {
case c:HttpURLConnection => c
case _ => error("Only HTTP connections allowed")
@gclaramunt
gclaramunt / gist:142051
Created July 7, 2009 12:37
custom XML parser factory to avoid w3c validation
package workbench;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl;
public class MyXMLParserFactory extends SAXParserFactoryImpl {
@gclaramunt
gclaramunt / gameoflife.scala
Created July 21, 2009 19:57
Game of Life in Scala
package gameoflife
class Board[T](val b:Array[Array[T]]) {
val h=b.size
val w=b(0).size
def conv(idx:Int,max:Int)=if (idx<0) (max-Math.abs(idx %max) ) else (idx % max )
def apply(x:Int,y:Int)=b(conv(x,h))(conv(y,w))
def update(x:Int,y:Int,value:T)= b(conv(x,h))(conv(y,w))=value
@gclaramunt
gclaramunt / tmexcercices1.scala
Created September 4, 2009 19:01
Tony Morris's revised Scala excercises 1
package tm
sealed trait List[+A] {
override def toString = {
def toScalaList(t: List[A]): scala.List[A] = t match {
case Empty => Nil
case Cons(h, t) => h :: toScalaList(t)
}
toScalaList(this).toString
}
@gclaramunt
gclaramunt / tm2.scala
Created September 10, 2009 20:28
Tony Morris's Scala exercises 2
package tm
trait PartialType[T[_, _], A] {
type Apply[B] = T[A, B]
type Flip[B] = T[B, A]
}
trait Fluffy[F[_]] {
def furry[A, B](f: A => B, fa: F[A]): F[B]
}
#lang scheme
(define atom? (let ((f1 pair?) (f2 not)) (lambda (x) (f2 (f1 x)))))
(define lat?
(lambda (lat)
(cond
((null? lat) #t)
((atom? (car lat)) (lat? (cdr lat)))
(else #f)
)))
/**
* Created by IntelliJ IDEA. I
* User: gabriel
* Date: Jun 15, 2010
* Time: 11:55:59 PM
* To change this template use File | Settings | File Templates.
*/
import java.awt.{Color,Container,Graphics,Canvas,Dimension}