This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] = |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def factorial(n: Int): Int = { | |
if (n<=0) | |
1 | |
else | |
n * factorial(n-1) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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){ |