Skip to content

Instantly share code, notes, and snippets.

@jonasabreu
Created July 11, 2012 22:55
Show Gist options
  • Save jonasabreu/3094311 to your computer and use it in GitHub Desktop.
Save jonasabreu/3094311 to your computer and use it in GitHub Desktop.
Código da minha apresentação no TDC 2012
package tdc
case class Gasto(subfuncao : String, destino : String, natureza : String, valor : Double)
case class GastoAgrupado(label : String, total : Double, filhos : Option[List[GastoAgrupado]])
object Gastos {
def join(gastos : List[Gasto], filters : List[Gasto => String]) : Option[List[GastoAgrupado]] = {
filters.headOption.map { filter =>
val itemsParaAgrupar = gastos.map(filter).distinct
itemsParaAgrupar.map { item =>
val gastosFiltrados = gastos.filter(gasto => filter(gasto) == item)
val soma = gastosFiltrados.foldLeft(0.0)(_ + _.valor)
GastoAgrupado(item, soma, join(gastosFiltrados, filters.tail))
}
}
}
def main(args : Array[String]) {
val gastos = List(
Gasto("A1", "B1", "C1", 3.0),
Gasto("A1", "B3", "C2", 4.0),
Gasto("A2", "B1", "C1", 5.0),
Gasto("A2", "B2", "C2", 6.0),
Gasto("A2", "B1", "C1", 7.0))
println(join(gastos, List(_.destino, _.subfuncao, _.natureza)))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment