-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOrderedJobs.scala
48 lines (39 loc) · 1.83 KB
/
OrderedJobs.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package workshop.codekata
class JobSelfReferenceException extends RuntimeException( "Jobs can't depend on themselves", null )
class JobCircularDependencyException extends RuntimeException( "Jobs can't have circular dependencies", null )
case class Job( id: String, dependency: String )
object OrderedJobs {
def addJob( list: List[ String ], job: Job ): List[ String ] = {
def insertBefore( x: String, y: String ): List[ String ] = {
( list.slice( 0, list.indexOf( x ) ) :+ y ) ::: list.drop( list.indexOf( x ) )
}
job match {
case Job( x, y: Any ) if list.contains( x ) && list.contains( y ) =>
throw new JobCircularDependencyException
case Job( x, null ) if !list.contains( x ) => list :+ x
case Job( x, y: Any ) if !list.contains( x ) && !list.contains( y ) => list :+ y :+ x
case Job( x, y: Any ) if !list.contains( y ) => insertBefore( x, y )
case Job( x, y: Any ) if !list.contains( x ) => list :+ x
case _ => list
}
}
def parse( str: String ): List[ String ] = {
val list: List[ Job ] = str.split( "\n" ).toList.map( x => JobExtractor( x ) )
def extractJobs( jobsRemaining: List[ Job ], jobsList: List[ String ] ): List[ String ] = {
jobsRemaining match {
case x :: _ => extractJobs( jobsRemaining.tail,
addJob( jobsList, x ) )
case Nil => jobsList
}
}
extractJobs( list, List( ) )
}
private def JobExtractor( job: String ): Job = {
job.split( " =>.?" ) match {
case Array( x, y ) if x == y => throw new JobSelfReferenceException
case Array( x, y ) => Job( x, y )
case Array( x ) if x.length > 0 => Job( x, null )
case _ => null
}
}
}