Skip to content

Instantly share code, notes, and snippets.

@rajeshpv
Created July 29, 2010 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rajeshpv/498423 to your computer and use it in GitHub Desktop.
Save rajeshpv/498423 to your computer and use it in GitHub Desktop.
Task(typeTask:String, id:Int, parentProject:Int price:Float)
notice "p" means project
task for "t"
there can be n number of tasks below project
and parent of project id will be always zero
the list is ordered based on id field
List(
new Task("p", 1, 0, 0.0),
new Task("t", 10, 1, 1.1),
new Task("t", 11, 1, 1.1),
new Task("p", 20, 0, 0.0),
new Task("t", 21, 2, 2.1),
new Task("t", 22, 2, 2.1),
new Task("t", 23, 2, 2.1)
)
I want to add up the price of tasks into Projects' price
so output should be
notice "2.2" value change in first item
and 6.3 change in 4th item
List(
new Task("p", 1, 0, 2.2),
new Task("t", 10, 1, 1.1),
new Task("t", 11, 1, 1.1),
new Task("p", 20, 0, 6.3),
new Task("t", 21, 2, 2.1),
new Task("t", 22, 2, 2.1),
new Task("t", 23, 2, 2.1)
)
@nuttycom
Copy link

sealed trait Task {
def id: Int
def price: Float
}

case class SimpleTask(id: Int, price: Float) extends Task
case class ProjectTask(id: Int, subTasks: List[Task]) extends Task {
def price: Float = subTasks.reduceLeft(_.price + _.price)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment