Skip to content

Instantly share code, notes, and snippets.

@ppurang
ppurang / mapOnABinaryTree.scala
Created April 20, 2011 21:51
In 2.9.0 RC0 issues encountered while defining a map using fold on BTree
/*
Given a map defined like:
def map[A, B](f: A => B)(btree: BTree[A]): BTree[B] =
fold((_:A) => Leaf(f(_)).asInstanceOf[BTree[B]])((x:BTree[B]) => (y:BTree[B]) => Fork(x,y))(btree)
when applied on a tree ((1 2) (3 4))
map((x:Int) => x*2)(BTree(1 :: 2 :: 3 :: 4 :: Nil))
results in ((<function1> <function1>) (<function1> <function1>))
@ppurang
ppurang / REPL session
Created July 1, 2011 10:36 — forked from gseitz/REPL session
Create 1-line extractors for traits
scala> trait YouCantMatchMe
defined trait YouCantMatchMe
scala> object YesICan extends Traitor[YouCantMatchMe]
defined module YesICan
scala> def uncatchable_?(any: Any) = any match {
| case YesICan(uncatchable) => Some(uncatchable)
| case _ => None
| }
@ppurang
ppurang / qsort.md
Created July 21, 2011 16:45
quicksort

quicksort

scala

def qsort[A <% Ordered[A]](v: Seq[A]): Seq[A] = v.headOption match {
  case Some(x) => (qsort(v.tail.filter(_ < x)) :+ x) ++ qsort(v.tail.filter(_ >= x))
  case _ => Seq()
}

haskell

//performance is not everything
//can this be parallel?
def isPalindrome(list: List[Int]) = {
(list zip list.reverse filter (a => a._1 != a._2) size) == 0
}
@ppurang
ppurang / SlogBuild.scala
Created August 22, 2011 17:19
scala sbt 10 multi module project
import sbt._
import Keys._
import sbt.Package._
import java.util.jar.Attributes.Name._
import com.github.siasia.WebPlugin._
//unashamed copy of scalaz's build
object SlogBuild extends Build {
override lazy val settings = super.settings :+
@ppurang
ppurang / shebang
Created November 22, 2011 22:12
scala script including a proper classpath
#!/bin/sh
L=`dirname $0`/../lib
cp=`echo $L/*.jar|sed 's/ /:/g'`
exec scala -classpath $cp -savecompiled $0 $@
!#
# shebang magic from http://www.eamonn.org/blog/archives/425
println("hello world")
@ppurang
ppurang / directions.hs
Created December 4, 2011 11:27
map a list of functions to a single value
-- import Control.Monad (ap)
type Pos = (Int, Int)
type Direction = Pos -> Pos
direction :: Pos -> [Direction] -> [Pos]
direction p [] = []
direction p (x:xs) = x p : direction p xs
-- another way
-- direction p ds = ap ds [p]
@ppurang
ppurang / gist:1895676
Created February 23, 2012 23:24
extreme stratup
val add = """.*: what is (\d*) plus (\d*)""".r
q match {
case add(a, b) =>(a.toInt + b.toInt).toString
case bond() => // well we don't want to reveal too much
case banana() => //but bond and banana ought to get the attention.
case _ => ""
}
@ppurang
ppurang / TCBedCon2012.scala
Created March 30, 2012 13:27
bedcon 2012 presentation script
:silent
//the following are helper functions
def p = if(readLine() == "q") sys.exit
def pp[A]: ( => A) => Unit = a => {readLine() ; a}
object > {
def > : String => Unit = println _
def >[A] : (String, => A) => Unit = (a,b) => {print("> " + a); readLine(); readLine(); println(" ----> " + b); println}
}