Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 54 lines (46 sloc) 1.455 kB
5a18f01 @debasishg added a dsl implemented using endomorphism
authored
1 package net.debasishg.snippet.endo
2
3 import java.util.{Date, Calendar}
4 import scalaz._
5 import Endo._
6 import WriterT._
7 import scalaz.syntax.all._
8 import scalaz.syntax.std.all._
9
10 object endodsl {
7f21bf7 @debasishg refactoring
authored
11 case class Task(name: String) {
12 def dependsOn(on: Task): Writer[Endo[Project], Task] = {
13 for {
14 _ <- tell(((p: Project) => withDependency(this, on, p)).endo)
15 } yield this
16 }
17 }
18
5a18f01 @debasishg added a dsl implemented using endomorphism
authored
19 case class Project(name: String, startDate: java.util.Date, endDate: Option[java.util.Date] = None,
20 tasks: List[Task] = List(), deps: List[(Task, Task)] = List())
21
22 val withTask = (t: Task, p: Project) => p.copy(tasks = t :: p.tasks)
23 val withDependency = (t: Task, on: Task, p: Project) => p.copy(deps = (t, on) :: p.deps)
24
25 def task(n: String): Writer[Endo[Project], Task] = {
26 val t = Task(n)
27 for {
28 _ <- tell(((p: Project) => withTask(t, p)).endo)
29 } yield t
30 }
31
32 def project(name: String, startDate: Date)(e: Writer[Endo[Project], Task]) = {
33 val p = Project(name, startDate)
34 e.run._1(p)
35 }
36 }
37
38 object Main {
39 import endodsl._
40 val now = Calendar.getInstance.getTime
41 val p =
42 project("xenos", now) {
43 for {
44 a <- task("study requirements")
45 b <- task("do analysis")
7f21bf7 @debasishg refactoring
authored
46 _ <- b dependsOn a
5a18f01 @debasishg added a dsl implemented using endomorphism
authored
47 c <- task("design & code")
7f21bf7 @debasishg refactoring
authored
48 _ <- c dependsOn b
49 d <- c dependsOn a
5a18f01 @debasishg added a dsl implemented using endomorphism
authored
50 } yield d
51 }
52 println(p)
53 }
Something went wrong with that request. Please try again.