Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

homework_7 - Полозков Марк #287

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions homeworks/homework_1/task_1/console-output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
PS C:\Users\Y\IdeaProjects\naumen_scala_urfu_2023\naumen.scala.course.2023.spring> scala
Welcome to Scala 2.12.10 (Java HotSpot(TM) 64-Bit Server VM, Java 19.0.1).
Type in expressions for evaluation. Or try :help.

scala> println("Ni hao")
Ni hao

scala> println("Hello Scala! This is Mark")
Hello Scala! This is Mark

scala> val name = Mark Polozkov
<console>:11: error: not found: value Mark
val name = Mark Polozkov
^
<console>:11: warning: postfix operator Poloz should be enabled
by making the implicit value scala.language.postfixOps visible.
This can be achieved by adding the import clause 'import scala.language.postfixOps'
or by setting the compiler option -language:postfixOps.
See the Scaladoc for value scala.language.postfixOps for a discussion
why the feature should be explicitly enabled.
val name = Mark Polozkov
^

scala> val name = "Mark Polozkov"
name: String = Mark Polozkov

scala> val small_turple = ("Hello","Hola","Guten morgen")
small_turple: (String, String, String) = (Hello,Hola,Guten morgen)

scala> val phrase = " Scala! This is "
phrase: String = " Scala! This is "

scala> println(small_turple(1) + phrase + name)
<console>:15: error: (String, String, String) does not take parameters
println(small_turple(1) + phrase + name)
^

scala> println(small_turple)
(Hello,Hola,Guten morgen)

scala> small_turple.getClass
res4: Class[_ <: (String, String, String)] = class scala.Tuple3

scala> small_turple(0)
<console>:13: error: (String, String, String) does not take parameters
small_turple(0)
^

scala> small_turple.0
<console>:1: error: ';' expected but double literal found.
small_turple.0
^

scala> small_turple[0]
<console>:1: error: identifier expected but integer literal found.
small_turple[0]
^

scala> small_turple._1
res6: String = Hello


scala> val new_seq = Seq("Hello","Hola","Guten morgen")
new_seq: Seq[String] = List(Hello, Hola, Guten morgen)

scala> for (elem <- new_seq) println(elem + phrase + name)
Hello Scala! This is Mark Polozkov
Hola Scala! This is Mark Polozkov
Guten morgen Scala! This is Mark Polozkov


scala>
1 change: 1 addition & 0 deletions homeworks/homework_1/task_2/scastie-url.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://scastie.scala-lang.org/aBFFrmeIT1e24FKS4rRy7Q
9 changes: 5 additions & 4 deletions homeworks/homework_7/src/main/scala/MonadFunctor.scala
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@

trait Monad[F[_]] {

def pure[A](a: A): F[A]

def flatMap[A, B](fa: F[A])(f: A => F[B]): F[B]

def map2[A, B, C](fa: F[A], fb: F[B])(f: (A, B) => C): F[C] = ???
def map2[A, B, C](fa: F[A], fb: F[B])(f: (A, B) => C): F[C] = flatMap(fa)(x => flatMap(fb)(y => pure(f(x, y))))

def sequence[A](fas: List[F[A]]): F[List[A]] = ???
def sequence[A](fas: List[F[A]]): F[List[A]] = fas.foldRight(pure(List[A]()))((a, b) => map2(a, b)((c, d) => c :: d))

def compose[A, B, C](f: A => F[B])(g: B => F[C]): A => F[C] = ???
def compose[A, B, C](f: A => F[B])(g: B => F[C]): A => F[C] = a => flatMap(f(a))(g)
}

trait Functor[F[_]] {
def map[A, B](a: F[A])(f: A => B): F[B]
}
@@ -18,6 +18,6 @@ trait Functor[F[_]] {

object Functor {
def functorFromMonad[F[_]](M: Monad[F]): Functor[F] = new Functor[F] {
def map[A, B](a: F[A])(f: A => B): F[B] = ???
def map[A, B](a: F[A])(f: A => B): F[B] = M.flatMap(a)(b => M.pure(f(b)))
}
}