Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
109 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package lascala.turtle | ||
|
||
import lascala.turtle.common._ | ||
import scala.util.{Failure, Try} | ||
|
||
object APIwithFunctionalCore extends App { | ||
import TurtleApi._ | ||
|
||
class TurtleApi { | ||
|
||
var state = initialTurtleState | ||
|
||
def updateState(newState:TurtleState) = { | ||
state = newState | ||
} | ||
|
||
|
||
def exec(commandStr: String) = { | ||
val tokens = commandStr.split(' ').toList.map(_.trim) | ||
|
||
val stateR = returnR(state) | ||
|
||
val newStateR = tokens match { | ||
case List("Move", distanceStr) => | ||
val distanceR = validateDistance(distanceStr) | ||
//lift2R(move)(distanceR)(stateR) | ||
} | ||
} | ||
} | ||
|
||
object TurtleApi { | ||
case class TurtleState(position: Position, angle: Angle, color: PenColor, penState: PenState) | ||
|
||
lazy val initialTurtleState = TurtleState(initialPosition, 0.0.degree, initialColor, initialPenState) | ||
|
||
def move(log: Log)(distance: Distance)(state: TurtleState) = { | ||
log(f"Move $distance%.1f") | ||
val newPosition = state.position.move(distance, state.angle) | ||
if (state.penState == PenState.Down) dummyDrawLine(log, state.position, newPosition, state.color) | ||
state.copy(position = newPosition) | ||
} | ||
|
||
def validateDistance(distanceStr: String): Try[Double] = { | ||
Try(distanceStr.toDouble) recoverWith { | ||
case e:NumberFormatException => Failure(InvalidDistance(distanceStr)) | ||
} | ||
} | ||
|
||
|
||
sealed trait ErrorMessage extends Throwable | ||
case class InvalidDistance(msg: String) extends ErrorMessage | ||
case class InvalidAngle(msg: String) extends ErrorMessage | ||
case class InvalidColor(msg: String) extends ErrorMessage | ||
case class InvalidCommand(msg: String) extends ErrorMessage | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/test/scala/lascala/turtle/APIwithFunctionalCoreTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package lascala.turtle | ||
|
||
import scala.util.{Failure, Success, Try} | ||
|
||
import lascala.turtle.common._ | ||
import APIwithFunctionalCore.TurtleApi._ | ||
import utest._ | ||
|
||
object APIwithFunctionalCoreTest extends TestSuite { | ||
val tests = this { | ||
'validateDistance { | ||
"success case" - { | ||
val result: Try[Double] = validateDistance("1.0") | ||
assertMatch(result){ case Success(d: Double) if (d - 1.0).abs < 0.00001 => } | ||
} | ||
"failure case" - { | ||
val result: Try[Double] = validateDistance("abc") | ||
assertMatch(result){ case Failure(InvalidDistance(_)) => } | ||
} | ||
} | ||
|
||
'exec { | ||
//val api = new TurtleApi() | ||
//api.exec("Move 100") | ||
//assertMatch(api.state){ case TurtleState(Position(x, y), _, _, _) if (x-100).abs < 0.00001 => } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters