Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
isolomein-naumen committed Dec 21, 2023
2 parents 2b505b8 + 2eaf492 commit b5634dc
Show file tree
Hide file tree
Showing 20 changed files with 292 additions and 285 deletions.
7 changes: 7 additions & 0 deletions homeworks/homework_6/homework_6.md
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>

2 changes: 1 addition & 1 deletion index.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ layout: default
* Лекция 3. [Scala basic II. От type конструкторов до коллекций](lectures/scala_lecture_3/index.html)
* Лекция 4. [Функциональное программирование. Концепция (PowerPoint)](lectures/scala_lecture_4.pptx)
* Лекция 5. [Функциональное программирование. Type classes и абстракции для ФП](lectures/scala_lecture_5.html)
* Практика [Функциональное программирование. База данных библиотеки](lectures/practice2)
* Лекция 7. [ZIO. Асинхронность и многопоточность. Управление ресурсами](lectures/scala_lecture_7/index.html)
* Лекция 8. [ZIO. Экосистема ZIO и другие библиотеки](lectures/scala_lecture_8.html)

Expand All @@ -22,4 +21,5 @@ layout: default
* [Задание 3](homeworks/homework_3/homework_3.md) [папка в GitHub](https://github.com/naumen-student/naumen.scala.course.2023.autumn/tree/master/homeworks/homework_3)
* [Задание 4](homeworks/homework_4/homework_4.md) [папка в GitHub](https://github.com/naumen-student/naumen.scala.course.2023.autumn/tree/master/homeworks/homework_4)
* [Задание 5](homeworks/homework_5/homework_5.md) [папка в GitHub](https://github.com/naumen-student/naumen.scala.course.2023.autumn/tree/master/homeworks/homework_5)
* [Задание 6](homeworks/homework_6/homework_6.md) [папка в GitHub](https://github.com/naumen-student/naumen.scala.course.2023.autumn/tree/master/homeworks/homework_6)
* [Задание 7](homeworks/homework_5/homework_7.md) [папка в GitHub](https://github.com/naumen-student/naumen.scala.course.2023.autumn/tree/master/homeworks/homework_7)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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)
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)
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")))
}

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])
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))

}
Loading

0 comments on commit b5634dc

Please sign in to comment.