Skip to content

Instantly share code, notes, and snippets.

View adilakhter's full-sized avatar

Adil Akhter adilakhter

View GitHub Profile
lazy val predicate: PackratParser[Expression] =
predicate ~ ("and" ~> relExpression) ^^ {case l ~ r => AndExp(l, r)} |
predicate ~ ("or" ~> relExpression) ^^ {case l ~ r => OrExp(l, r)} |
relExpression
lazy val relExpression: PackratParser[Expression] =
relExpression ~ ("gt" ~> expression) ^^ {case l ~ r => GreaterThanExp(l, r)} |
relExpression ~ ("lt" ~> expression) ^^ {case l ~ r => LessThanExp(l, r)} |
relExpression ~ ("eq" ~> expression) ^^ {case l ~ r => EqualToExp(l, r)} |
relExpression ~ ("ne" ~> expression) ^^ {case l ~ r => NotEqualToExp(l, r)} |
lazy val queryOperationDef: PackratParser[Seq[Expression]] =
"?" ~> repsep(filter | select | top | skip | orderBy, "&")
lazy val filter: PackratParser[Expression] =
"$filter" ~> "=" ~> predicate ^^ Filter
lazy val top: PackratParser[Expression] =
"$top" ~> "=" ~> number ^^ Top
lazy val skip: PackratParser[Expression] =
test("Parse /Customers?$top=2&$filter=concat(City, Country) eq 'Berlin, Germany'"){
val uri = "http://odata.io/odata.svc/Schema(231)/Customer?$top=2&$filter=concat(City, Country) eq 'Berlin, Germany'"
val actual = p.parseThis(mainParser,uri).get
println(uri + "=>" + actual)
val expectedAst=
ODataQuery(
URL("http://odata.io/odata.svc"),
ResourcePath("Schema",Number("231"),ResourcePath("Customer",EmptyExp(),EmptyExp())),
QueryOperations(
test("Parse /Products?$select=Name"){
val uri = "http://services.odata.org/OData.svc/Products?$select=Name,Price"
val actual = p.parseThis(mainParser,uri).get
val expectedAst =
ODataQuery(
URL("http://services.odata.org/OData.svc"),
ResourcePath("Products",EmptyExp(),EmptyExp()),
QueryOperations(List(Select(List(Property("Name"), Property("Price"))))))
def factorial(n: Int): Int = {
if (n<=0)
1
else
n * factorial(n-1)
}
@adilakhter
adilakhter / factorial-tail-rec.sc
Created May 18, 2014 09:50
factorial-tail-rec
def factorial(n: Int): Int = {
@tailrec def factorialAcc(acc: Int, n:Int): Int = {
if (n<=1) acc else factorialAcc(acc*n, n-1)
}
factorialAcc(1,n)
}
def odd1(n: Int): Boolean= {
if (n==0) false
else even1(n-1)
}
def even1(n: Int): Boolean = {
if (n==0) true
else odd1(n-1)
}
// output from scala repl
def even2(n: Int): Bounce[Boolean] = {
if (n == 0) Done(true)
else Call(() => odd2(n - 1))
}
def odd2(n: Int): Bounce[Boolean] = {
if (n == 0) Done(false)
else Call(() => even2(n - 1))
}
sealed trait Bounce[A]
@adilakhter
adilakhter / Minsum.scala
Last active August 29, 2015 14:05
Calculation of Minimum sum using recursion.
object Minsum extends App{
def sum(ls: List[Int]) = ls.foldLeft(0)(_ + _)
def getMin (numbers: List[Int]): (Boolean, Int, List[Int]) = {
val total = sum(numbers)
def partition(ls: List[Int], nls: List[Int], sum: Int): (Boolean, Int, List[Int]) = {
ls match {
case Nil => if (sum >= 0) (true, sum, nls) else (false, sum, nls) // End of the List => if sum >= 0, return true
case x :: xs if x > sum => partition(xs, nls, sum) // not ìncluding x in the partitioning set nls
case x :: xs => {
@adilakhter
adilakhter / MinsumDP.scala
Created August 17, 2014 19:25
Calculation of minimum sum using dynamic programming.
import scala.collection.mutable.ListBuffer
object MinsumDP extends App{
def sum(ls: List[Int]) = ls.foldLeft(0)(_ + _)
def buildOPT(OPT: Array[Array[Boolean]], A: Array[Int]): Unit ={
for (
i <- 1 until OPT.length;
j <- 1 until OPT(0).length){