Skip to content

Instantly share code, notes, and snippets.

View gclaramunt's full-sized avatar

Gabriel Claramunt gclaramunt

View GitHub Profile
@gclaramunt
gclaramunt / Ingester.scala
Created June 27, 2011 16:37
ER to extract subsonic data
object Ingester {
/**
* [2011-06-09 20:19:59,288] INFO PlaylistInputStream - guillermo.mackinnon listening to "(1984) Ride The Lightning/07 - Creeping Death.mp3"
* [2011-06-09 20:26:35,536] INFO PlaylistInputStream - guillermo.mackinnon listening to "(1984) Ride The Lightning/08 - The Call Of Ktulu.mp3"
*/
val regxp="""\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d{3}\] INFO PlaylistInputStream - (\S+) listening to \"(.+).mp3"""".r
val sample= """[2011-06-06 12:10:45,109] INFO PlaylistInputStream - alejandro.nieto listening to "Elephant [2003]/the white stripes - elephant - 01 - seven nation army.mp3""""
@gclaramunt
gclaramunt / RestConsumer.scala
Created July 12, 2011 15:01
simple REST WS consumer client (but better just use dispatch)
package webtest
import com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl;
import java.net._
import scala.xml._
import javax.xml.parsers.SAXParserFactory
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl;
@gclaramunt
gclaramunt / Functor.scala
Created August 11, 2011 18:11
Functor, Applicative Functor, Monad typeclasses in Haskell and their (simplistic) Scala translation
trait Functor[T[_]]{
def fmap[A,B](f:A=>B)(ta:T[A]):T[B]
}
trait Applicative[T[_]] extends Functor[T]{
def pure[A](a:A):T[A]
def <*>[A,B](tf:T[A=>B])(ta:T[A]):T[B]
}
trait Monad[M[_]] extends Applicative[M]{
@gclaramunt
gclaramunt / SafeUnsafe.scala
Created August 15, 2011 17:20
Scala's Option *CAN* save you from NullPointerExceptions
package demo
class SafeUnsafe {
def unsafe[T](x: =>T):Option[T]= try {
Option(x)
} catch {
case _ => None
}
@gclaramunt
gclaramunt / LittleSchemerChp4.scm
Created August 22, 2011 15:16
Little Schemer Chapter 4
(define add1 (let ((f +)) (lambda (x) (f x 1))))
(define sub1 (let ((f -)) (lambda (x) (f x 1))))
(define atom? (let ((f1 pair?) (f2 not)) (lambda (x) (f2 (f1 x)))))
(define sum1 ( lambda (x) (+ 1 x)))
(define sub1 ( lambda (x) (- x 1)))
(define plus ( lambda (x y)
@gclaramunt
gclaramunt / MapTableField
Created August 29, 2011 03:36
Lift code to generate a K/V create/update/delete
import net.liftweb._
import net.liftweb.common._
import common.Full
import http._
import util._
import js._
import JsCmds._
import xml.NodeSeq
import collection.immutable.Map
@gclaramunt
gclaramunt / IDfunctions.scala
Created October 3, 2011 03:26
Turing Machine in purely functional Scala
val I= left _
val D= right _
@gclaramunt
gclaramunt / Exists.coq
Created October 3, 2011 13:53
Trivial proof
Theorem AE: forall k:U,A(k)->(exists x:U,A(x)).
Proof.
intros k Ha; exists k; apply Ha.
Qed.
@gclaramunt
gclaramunt / unsafe.scala
Created October 7, 2011 20:23
Safely call Java from Scala
def unsafeCall[T](x: =>T):Option[T]= try {
Option(x)
} catch {
case _ => None
}
@gclaramunt
gclaramunt / NaiveTail.java
Created November 4, 2011 14:24
Naive tail in Java (to keep up with basic skills)
import java.io.BufferedReader;
import java.io.FileReader;
class NaiveTail{
public static void main(String[] args) throws Exception{
int size=new Integer(args[0]);
String[] circularBuffer=new String[size];
int i=0;
BufferedReader in=new BufferedReader(new FileReader(args[1]));
String line;