Advertisement
Guest User

Untitled

a guest
May 16th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.30 KB | None | 0 0
  1. package tables
  2.  
  3. import tables.Table.Row
  4.  
  5. class Table(val name: String, val attrs: List[String], val lines: List[List[Any]]) {
  6.  
  7.   // Secondary constructor for anonym tables
  8.   def this(attrs: List[String], lines: List[List[Any]]) {
  9.     this("", attrs, lines)
  10.   }
  11.  
  12.   def display() = {
  13.     println(name)
  14.     println(attrs)
  15.     for (l <- lines) {
  16.       println(l)
  17.     }
  18.   }
  19.  
  20.   def project(newAttrs: List[String]): Table = {
  21.     var newLines: List[Row] = Nil
  22.     for (l <- lines) {
  23.       var newLine: Row = Nil
  24.       for (a <- newAttrs) {
  25.         newLine = newLine ::: List(Table.get(a, attrs, l))
  26.       }
  27.       newLines = newLines ::: List(newLine)
  28.     }
  29.     new Table(newAttrs, newLines)
  30.   }
  31.  
  32.   // High order version
  33.   def projectHo(newAttrs: List[String]): Table =
  34.     new Table(newAttrs, lines.map(l => newAttrs.map(a => Table.get(a, newAttrs, l))))
  35.  
  36.   def alias(als: Map[String, String]) : Table = {
  37.     var newAttrs: List[String] = Nil
  38.     for (a <- newAttrs) {
  39.       newAttrs =  newAttrs ::: List(if (als.contains(a)) als(a) else a)
  40.     }
  41.     new Table(newAttrs, lines)
  42.   }
  43.  
  44.   // High order version
  45.   def aliasHo(als: Map[String, String]) : Table =
  46.     new Table(attrs.map(a => if (als.contains(a)) als(a) else a), lines)
  47.  
  48.  
  49.   def select(predicate: Row => Boolean): Table = {
  50.     var newLines: List[Row] = Nil
  51.     for(r <- lines ){
  52.       if(predicate.apply(r)) r :: newLines
  53.     }
  54.     new Table(attrs, newLines)
  55.   }
  56.  
  57.   // High order version
  58.   def selectHo(predicate: Row => Boolean): Table = new Table(attrs, lines.filter(predicate))
  59.  
  60.   def product(that: Table): Table = {
  61.     val newAttrs : List[String] = this.attrs
  62.     for(a <- that.attrs){
  63.       a :: newAttrs
  64.     }
  65.     val newLines: List[Row] = Nil
  66.     for(r <- that.lines){
  67.       for(rr <- this.lines){
  68.         (r++rr)::newLines
  69.       }
  70.     }
  71.     new Table(newAttrs, newLines)
  72.   }
  73.  
  74.   // High order version
  75.   def join(that: Table, attributeThis: String, attributeThat: String, predicate: (Any, Any) => Boolean): Table = {
  76.     val p = product(that)
  77.     new Table(p.attrs, p.lines.filter(l => predicate(l(p.attrs.indexOf(attributeThis)),
  78.       l(p.attrs.indexOf(attributeThat)))))
  79.   }
  80. }
  81.  
  82. object Table {
  83.   type Row = List[Any]
  84.  
  85.   def get(attr: String, attrs: List[String] ,row: Row): Any = row(attrs.indexOf(attr))
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement