Skip to content

Instantly share code, notes, and snippets.

View ssuravarapu's full-sized avatar

Surya Suravarapu ssuravarapu

View GitHub Profile
@ssuravarapu
ssuravarapu / ElementCount
Created March 19, 2012 23:41
Calculate number of times each element repeats in the List
scala> val l = "a" :: "b" :: "c" :: "b" :: "a" :: List()
l: List[java.lang.String] = List(a, b, c, b, a)
scala> val distinct = l.distinct
distinct: List[java.lang.String] = List(a, b, c)
scala> distinct zip (distinct map (p => l.filter(p == _).size))
res0: List[(java.lang.String, Int)] = List((a,2), (b,2), (c,1))
@ssuravarapu
ssuravarapu / DuplicateWords.java
Created July 22, 2011 03:10
Duplicate Contacts - Java and Scala
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
public class DuplicateWords {
public static void main(String[] args) {
@ssuravarapu
ssuravarapu / AcceptHeader.scala
Created April 27, 2011 00:18
Parser combinators using Scala
case class AcceptHeader(mediaType: String, mediaSubType: String, qualityFactor: Float)
@ssuravarapu
ssuravarapu / WebService.scala
Created January 13, 2011 12:57
For Conditional GET and PUT
package code.service
import xml.{Elem, Node}
import net.liftweb.http._
import rest.RestHelper
import provider.HTTPCookie
import net.liftweb.common.{Empty, Full}
import code.model.Product
object WebService extends RestHelper {
case class NotModifiedResponse(eTag: String) extends LiftResponse with HeaderDefaults {
def toResponse = InMemoryResponse(Array(), "ETag" -> ("\"" + eTag + "\"") :: headers, cookies, 304)
}
trait RestHelper extends LiftRules.DispatchPF {
...
/**
* A function that chooses JSON or XML based on the request..
* Use with serveType
*/
implicit def jxSel(req: Req): Box[JsonXmlSelect] =
if (jsonResponse_?(req)) Full(JsonSelect)
else if (xmlResponse_?(req)) Full(XmlSelect)
else None
serve {
case "api" :: "user" :: id :: _ Get _ => userDetails(id)
}