-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/master'
- Loading branch information
Showing
20 changed files
with
292 additions
and
285 deletions.
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,7 @@ | ||
# Домашнее задание №6 (курс Scala, Naumen) | ||
|
||
Необходимо выполнить задания, описанные в | ||
<br><a href='https://github.com/naumen-student/naumen.scala.course.2023.autumn/tree/master/homeworks/homework_6/src/main/scala'>src/main/scala/Exercises.scala</a> | ||
|
||
Решение должно успешно проходить тесты в <a href='https://github.com/naumen-student/naumen.scala.course.2023.autumn/tree/master/homeworks/homework_6/src/test/scala'>(src/test/scala/Tests.scala)</a> | ||
|
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions
6
...ractice2/src/main/scala/online/Book.scala → ...ractice2/src/main/scala/online/Book.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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
package online | ||
|
||
case class Book(name: String, author: String) | ||
package online | ||
|
||
case class Book(name: String, author: String) |
6 changes: 3 additions & 3 deletions
6
...ctice2/src/main/scala/online/Client.scala → ...ctice2/src/main/scala/online/Client.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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
package online | ||
|
||
case class Client(name: String) | ||
package online | ||
|
||
case class Client(name: String) |
132 changes: 66 additions & 66 deletions
132
...src/main/scala/online/CommandParser.scala → ...src/main/scala/online/CommandParser.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 |
---|---|---|
@@ -1,66 +1,66 @@ | ||
package online | ||
|
||
object CommandParser { | ||
def parse(commandLine: String): LibraryOperation[String] = commandLine.split(' ').toList match { | ||
case command :: arguments if arguments.nonEmpty => | ||
command match { | ||
|
||
case "addClient" => LibraryOperation(db => | ||
db.copy(clients = db.clients :+ Client(arguments.mkString(" "))) -> Right("ok") | ||
) | ||
|
||
case "addBook" => { | ||
val totalArgument = arguments.mkString(" ").split('|') | ||
val name = totalArgument.headOption | ||
val author = totalArgument.drop(1).headOption | ||
(name, author) match { | ||
case (Some(name), Some(author)) => LibraryOperation { db => db.copy(books = db.books :+ Book(name, author)) -> Right("ok") } | ||
case _ => LibraryOperation.failure(new RuntimeException("wrong syntax")) | ||
} | ||
} | ||
|
||
case _ => LibraryOperation.failure(new Exception("unknown command")) | ||
} | ||
case _ => LibraryOperation.failure(new Exception("wrong syntax")) | ||
} | ||
} | ||
|
||
trait Command { | ||
def couldParse(commandLine: String): Boolean | ||
|
||
def parse(commandLine: String): LibraryOperation[String] | ||
} | ||
|
||
class UniversalCommandParser(additionalCommands: List[Command]) { | ||
|
||
val defaultCommands: List[Command] = List( | ||
new Command { | ||
override def couldParse(commandLine: String): Boolean = commandLine.split(' ').toList match { | ||
case command :: arguments if arguments.nonEmpty => | ||
command match { | ||
case "addClient" => true | ||
case _ => false | ||
} | ||
case _ => false | ||
} | ||
|
||
override def parse(commandLine: String): LibraryOperation[String] = commandLine.split(' ').toList match { | ||
case command :: arguments if arguments.nonEmpty => | ||
command match { | ||
case "addClient" => LibraryOperation(db => | ||
db.copy(clients = db.clients :+ Client(arguments.mkString(" "))) -> Right("ok") | ||
) | ||
case _ => LibraryOperation.failure(new Exception("unknownCommand")) | ||
} | ||
case _ => LibraryOperation.failure(new Exception("wrong syntax")) | ||
} | ||
} | ||
) | ||
|
||
def parse(commandLine: String): LibraryOperation[String] = (defaultCommands ++ additionalCommands) | ||
.dropWhile(!_.couldParse(commandLine)) | ||
.headOption | ||
.map(_.parse(commandLine)) | ||
.getOrElse(LibraryOperation.failure(new Exception("unknown command"))) | ||
} | ||
|
||
package online | ||
|
||
object CommandParser { | ||
def parse(commandLine: String): LibraryOperation[String] = commandLine.split(' ').toList match { | ||
case command :: arguments if arguments.nonEmpty => | ||
command match { | ||
|
||
case "addClient" => LibraryOperation(db => | ||
db.copy(clients = db.clients :+ Client(arguments.mkString(" "))) -> Right("ok") | ||
) | ||
|
||
case "addBook" => { | ||
val totalArgument = arguments.mkString(" ").split('|') | ||
val name = totalArgument.headOption | ||
val author = totalArgument.drop(1).headOption | ||
(name, author) match { | ||
case (Some(name), Some(author)) => LibraryOperation { db => db.copy(books = db.books :+ Book(name, author)) -> Right("ok") } | ||
case _ => LibraryOperation.failure(new RuntimeException("wrong syntax")) | ||
} | ||
} | ||
|
||
case _ => LibraryOperation.failure(new Exception("unknown command")) | ||
} | ||
case _ => LibraryOperation.failure(new Exception("wrong syntax")) | ||
} | ||
} | ||
|
||
trait Command { | ||
def couldParse(commandLine: String): Boolean | ||
|
||
def parse(commandLine: String): LibraryOperation[String] | ||
} | ||
|
||
class UniversalCommandParser(additionalCommands: List[Command]) { | ||
|
||
val defaultCommands: List[Command] = List( | ||
new Command { | ||
override def couldParse(commandLine: String): Boolean = commandLine.split(' ').toList match { | ||
case command :: arguments if arguments.nonEmpty => | ||
command match { | ||
case "addClient" => true | ||
case _ => false | ||
} | ||
case _ => false | ||
} | ||
|
||
override def parse(commandLine: String): LibraryOperation[String] = commandLine.split(' ').toList match { | ||
case command :: arguments if arguments.nonEmpty => | ||
command match { | ||
case "addClient" => LibraryOperation(db => | ||
db.copy(clients = db.clients :+ Client(arguments.mkString(" "))) -> Right("ok") | ||
) | ||
case _ => LibraryOperation.failure(new Exception("unknownCommand")) | ||
} | ||
case _ => LibraryOperation.failure(new Exception("wrong syntax")) | ||
} | ||
} | ||
) | ||
|
||
def parse(commandLine: String): LibraryOperation[String] = (defaultCommands ++ additionalCommands) | ||
.dropWhile(!_.couldParse(commandLine)) | ||
.headOption | ||
.map(_.parse(commandLine)) | ||
.getOrElse(LibraryOperation.failure(new Exception("unknown command"))) | ||
} | ||
|
6 changes: 3 additions & 3 deletions
6
...c/main/scala/online/LibraryDatabase.scala → ...c/main/scala/online/LibraryDatabase.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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
package online | ||
|
||
case class LibraryDatabase(clients: List[Client], books: List[Book], issuedBoos: Map[Book, Client]) | ||
package online | ||
|
||
case class LibraryDatabase(clients: List[Client], books: List[Book], issuedBoos: Map[Book, Client]) |
78 changes: 39 additions & 39 deletions
78
.../main/scala/online/LibraryOperation.scala → .../main/scala/online/LibraryOperation.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 |
---|---|---|
@@ -1,39 +1,39 @@ | ||
package online | ||
|
||
case class LibraryOperation[+R](run: LibraryDatabase => (LibraryDatabase, Either[Throwable, R])) { | ||
|
||
def map[R2](f: R => R2): LibraryOperation[R2] = LibraryOperation { db => | ||
val (newDb, res) = run(db) | ||
|
||
res match { | ||
case Left(error) => newDb -> Left(error) | ||
case Right(value) => newDb -> Right(f(value)) | ||
} | ||
|
||
} | ||
|
||
def flatMap[R2](f: R => LibraryOperation[R2]): LibraryOperation[R2] = LibraryOperation { db => | ||
val (newDb, res: Either[Throwable, R]) = run(db) | ||
|
||
res match { | ||
case Left(error) => newDb -> Left(error) | ||
case Right(value) => | ||
|
||
val r1: LibraryOperation[R2] = f(value) | ||
|
||
val r2: (LibraryDatabase, Either[Throwable, R2]) = r1.run(newDb) | ||
val r3: Either[Throwable, R2] = r2._2 | ||
|
||
|
||
r2._1 -> r2._2 | ||
} | ||
} | ||
} | ||
|
||
object LibraryOperation{ | ||
|
||
def failure(e: Throwable) = LibraryOperation(db => db -> Left(e)) | ||
|
||
def pure[R](value: R): LibraryOperation[R] = LibraryOperation(db => db -> Right(value)) | ||
|
||
} | ||
package online | ||
|
||
case class LibraryOperation[+R](run: LibraryDatabase => (LibraryDatabase, Either[Throwable, R])) { | ||
|
||
def map[R2](f: R => R2): LibraryOperation[R2] = LibraryOperation { db => | ||
val (newDb, res) = run(db) | ||
|
||
res match { | ||
case Left(error) => newDb -> Left(error) | ||
case Right(value) => newDb -> Right(f(value)) | ||
} | ||
|
||
} | ||
|
||
def flatMap[R2](f: R => LibraryOperation[R2]): LibraryOperation[R2] = LibraryOperation { db => | ||
val (newDb, res: Either[Throwable, R]) = run(db) | ||
|
||
res match { | ||
case Left(error) => newDb -> Left(error) | ||
case Right(value) => | ||
|
||
val r1: LibraryOperation[R2] = f(value) | ||
|
||
val r2: (LibraryDatabase, Either[Throwable, R2]) = r1.run(newDb) | ||
val r3: Either[Throwable, R2] = r2._2 | ||
|
||
|
||
r2._1 -> r2._2 | ||
} | ||
} | ||
} | ||
|
||
object LibraryOperation{ | ||
|
||
def failure(e: Throwable) = LibraryOperation(db => db -> Left(e)) | ||
|
||
def pure[R](value: R): LibraryOperation[R] = LibraryOperation(db => db -> Right(value)) | ||
|
||
} |
Oops, something went wrong.