{"id":326584,"date":"2021-07-17T15:00:12","date_gmt":"2021-07-17T15:00:12","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=326584"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=326584","title":{"rendered":"\u0418\u043d\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u044b\u0439 \u0444\u0443\u043d\u043a\u0442\u043e\u0440 \u0432 Scala Cats"},"content":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\">\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/fbf\/213\/465\/fbf213465516d62ed16547b532068728.png\" width=\"780\" height=\"439\"><figcaption><\/figcaption><\/figure>\n<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u043f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u043c \u043e \u0435\u0449\u0435 \u043e\u0434\u043d\u043e\u043c \u0444\u0443\u043d\u043a\u0442\u043e\u0440\u0435 \u2014 \u0438\u043d\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u043e\u043c (Invariant Functor). \u0423\u0436\u0435 \u0431\u044b\u043b\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0441\u0442\u043e\u0432 \u043e \u043a\u043e\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u044b\u0445 \u0444\u0443\u043d\u043a\u0442\u043e\u0440\u0430\u0445 (\u043d\u0430\u0437\u044b\u0432\u0430\u0435\u043c\u044b\u0445 \u043f\u0440\u043e\u0441\u0442\u043e &#171;\u0444\u0443\u043d\u043a\u0442\u043e\u0440\u0430\u043c\u0438&#187;) \u0438 <a href=\"https:\/\/medium.com\/p\/2af1141c5956\"><u>\u043a\u043e\u043d\u0442\u0440\u0430\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u044b\u0445 \u0444\u0443\u043d\u043a\u0442\u043e\u0440\u0430\u0445<\/u><\/a>. \u0415\u0441\u043b\u0438 \u043a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u044f \u043a\u043e\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u044b\u0445 \u0438 \u043a\u043e\u043d\u0442\u0440\u0430\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u044b\u0445 \u0444\u0443\u043d\u043a\u0442\u043e\u0440\u043e\u0432 \u0432\u0430\u043c \u043f\u043e\u043d\u044f\u0442\u043d\u0430, \u0442\u043e \u0441 \u0438\u043d\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u044b\u043c \u0432\u0441\u0435 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u043e\u0441\u0442\u043e \u2014 \u043e\u043d \u0441\u043e\u0447\u0435\u0442\u0430\u0435\u0442 \u0432 \u0441\u0435\u0431\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u043e\u0438\u0445 \u0432\u044b\u0448\u0435\u0443\u043f\u043e\u043c\u044f\u043d\u0443\u0442\u044b\u0445 \u0444\u0443\u043d\u043a\u0442\u043e\u0440\u043e\u0432.<\/p>\n<p>\u041a\u0430\u043a \u0432\u044b \u043f\u043e\u043c\u043d\u0438\u0442\u0435, \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0444\u0443\u043d\u043a\u0442\u043e\u0440\u043e\u0432 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0442\u044c \u043e\u0434\u0438\u043d \u0442\u0438\u043f \u0432 \u0434\u0440\u0443\u0433\u043e\u0439 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>f<\/code>:<\/p>\n<pre><code class=\"scala\">trait Functor[F[_]] {   def map[A, B](fa: F[A])(f: A =&gt; B): F[B] }<\/code><\/pre>\n<p>\u0421 \u043a\u043e\u043d\u0442\u0440\u0430\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u044b\u043c\u0438 \u0444\u0443\u043d\u043a\u0442\u043e\u0440\u0430\u043c\u0438 \u043c\u044b \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u043c \u0442\u0430\u043a\u0436\u0435, \u043d\u043e \u043c\u0435\u043d\u044f\u0435\u043c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 \u0442\u0438\u043f\u044b \u0432 <code>f<\/code>:<\/p>\n<pre><code class=\"scala\">def contramap[A, B](fa: F[A])(f: B =&gt; A): F[B]<\/code><\/pre>\n<p>\u042d\u0442\u043e \u0431\u044b\u0432\u0430\u0435\u0442 \u043f\u043e\u043b\u0435\u0437\u043d\u043e, \u043a\u043e\u0433\u0434\u0430 \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u043d\u043e\u0432\u044b\u0435 \u043d\u0435\u044f\u0432\u043d\u044b\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043a\u0430\u043a\u043e\u0433\u043e-\u0442\u043e \u0442\u0430\u0439\u043f\u043a\u043b\u0430\u0441\u0441\u0430, \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0438\u043c\u0435\u044e\u0449\u0438\u0435\u0441\u044f \u0438\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438.<\/p>\n<p>\u041a\u043e\u0440\u043e\u0447\u0435 \u0433\u043e\u0432\u043e\u0440\u044f, <code>map<\/code> \u0432 \u0444\u0443\u043d\u043a\u0442\u043e\u0440\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f, \u0435\u0441\u043b\u0438 \u043c\u044b \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432 \u043a\u043e\u043d\u0435\u0446 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438, \u0430 <code>contramap<\/code> \u0432 \u043a\u043e\u043d\u0442\u0440\u0430\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u043e\u043c \u0444\u0443\u043d\u043a\u0442\u043e\u0440\u0435, \u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0438\u0445 \u0432 \u043d\u0430\u0447\u0430\u043b\u043e. \u0414\u043b\u044f \u0438\u043d\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u043e\u0433\u043e \u0444\u0443\u043d\u043a\u0442\u043e\u0440\u0430 \u0435\u0441\u0442\u044c \u0435\u0449\u0435 \u0442\u0440\u0435\u0442\u0438\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u2014 <code>imap<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0432 \u043e\u0431\u043e\u0438\u0445 \u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f\u0445.<\/p>\n<p>\u0418\u043d\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0442\u043e\u0440\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0442 \u043c\u0435\u0442\u043e\u0434 <code>imap<\/code>,\u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u044d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442\u0435\u043d \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0446\u0438\u0438 <code>map<\/code> \u0438 <code>contramap<\/code>. \u041c\u0435\u0442\u043e\u0434 <code>imap<\/code> \u2014 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u043d\u043e\u0432\u044b\u0435 \u0442\u0430\u0439\u043f\u043a\u043b\u0430\u0441\u0441\u044b \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043f\u0430\u0440\u044b \u0434\u0432\u0443\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0445 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0439.<\/p>\n<p>\u0421\u0430\u043c\u044b\u0439 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0438 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0438\u043d\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u044b\u0445 \u0444\u0443\u043d\u043a\u0442\u043e\u0440\u043e\u0432 \u2014 \u044d\u0442\u043e \u043a\u043e\u0434\u0435\u043a\u0438 \u0438 \u043f\u0430\u0440\u0441\u0435\u0440\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0432 \u043e\u0431\u043e\u0438\u0445 \u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f\u0445.<\/p>\n<pre><code class=\"scala\">trait CustomParser[A] {   def encode(value: A): String   def decode(value: String): A   def imap[B](dec: A =&gt; B, enc: B =&gt; A): CustomParser[B] = ??? }  def encode[A](value: A)(implicit c: CustomParser[A]): String = c.encode(value) def decode[A](value: String)(implicit c: CustomParser[A]): A = c.decode(value)<\/code><\/pre>\n<p>\u0412 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0435 Cats \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 <code>imap<\/code> \u0432 \u0438\u043d\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u043e\u043c \u0444\u0443\u043d\u043a\u0442\u043e\u0440\u0435 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u043e:<\/p>\n<pre><code class=\"scala\">def imap[A, B](fa: F[A])(f: A =&gt; B)(g: B =&gt; A): F[B]<\/code><\/pre>\n<p>\u041c\u043e\u0436\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043d\u0430\u0448 <code>imap<\/code> \u0432 \u0442\u0435\u0440\u043c\u0438\u043d\u0430\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 <code>encode<\/code> \u0438 <code>decode<\/code>:<\/p>\n<pre><code class=\"scala\">def imap[B](dec: A =&gt; B, enc: B =&gt; A): CustomParser[B] = { val self = this     new CustomParser[B] {       def encode(value: B): String =         self.encode(enc(value))       def decode(value: String): B =         dec(self.decode(value))     }    }<\/code><\/pre>\n<p>\u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0438\u043c\u0435\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 <code>encode<\/code>\/<code>decode<\/code>, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c <code>imap<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u0442 \u043d\u0430\u043c \u043b\u0435\u0433\u043a\u043e \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u0435 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u044b <code>CustomParser<\/code> \u0434\u043b\u044f \u0434\u0440\u0443\u0433\u0438\u0445 \u0442\u0438\u043f\u043e\u0432:<\/p>\n<pre><code class=\"scala\">implicit val longParser: CustomParser[Long] =   new CustomParser[Long] {     def encode(value: Long): String = value.toString     def decode(value: String): Long = value.toLong   }<\/code><\/pre>\n<p>\u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f <code>CustomParser<\/code> \u0434\u043b\u044f <code>Long<\/code> \u0432 \u043d\u0430\u0448\u0435\u0439 \u043d\u0435\u044f\u0432\u043d\u043e\u0439 \u043e\u0431\u043b\u0430\u0441\u0442\u0438 \u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u0438, \u0438 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u0440\u0443\u0433\u043e\u0439 \u043f\u0430\u0440\u0441\u0435\u0440 \u0434\u043b\u044f <code>java.util.Date<\/code>. \u041c\u044b \u0437\u043d\u0430\u0435\u043c, \u043a\u0430\u043a \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c <code>Date<\/code> \u0432 <code>Long<\/code> \u0438 \u043e\u0431\u0440\u0430\u0442\u043d\u043e, \u043f\u043e\u044d\u0442\u043e\u043c\u0443, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0438\u0434\u0435\u044e \u0438\u043d\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u043e\u0433\u043e \u0444\u0443\u043d\u043a\u0442\u043e\u0440\u0430, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043b\u0435\u0433\u043a\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u043f\u0430\u0440\u0441\u0435\u0440:<\/p>\n<pre><code class=\"scala\">implicit val dateParser: CustomParser[Date] =   longParser.imap(new Date(_), _.getTime)<\/code><\/pre>\n<p>\u042d\u0442\u043e \u043f\u043e\u0445\u043e\u0436\u0435 \u043d\u0430 \u0443\u0436\u0435 \u043e\u0431\u0441\u0443\u0436\u0434\u0430\u0432\u0448\u0438\u0435\u0441\u044f \u0440\u0430\u043d\u0435\u0435 \u043a\u043e\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u044b\u0435 \u0438 \u043a\u043e\u043d\u0442\u0440\u0430\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0442\u043e\u0440\u044b. \u041a\u0441\u0442\u0430\u0442\u0438, \u043a\u043e\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u044b\u0439 \u0438 \u043a\u043e\u043d\u0442\u0440\u0430\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u044b\u0439 \u0444\u0443\u043d\u043a\u0442\u043e\u0440\u044b \u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u043f\u043e\u0442\u043e\u043c\u043a\u0430\u043c\u0438 \u0438\u043d\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u043e\u0433\u043e \u0444\u0443\u043d\u043a\u0442\u043e\u0440\u0430, \u0434\u0440\u0443\u0433\u0438\u043c\u0438 \u0441\u043b\u043e\u0432\u0430\u043c\u0438, \u0444\u0443\u043d\u043a\u0446\u0438\u044f <code>imap<\/code> \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u0430 \u0434\u043b\u044f \u043d\u0438\u0445 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0438\u0445 <code>map<\/code> \u0438\u043b\u0438 <code>contramap<\/code> \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e.<\/p>\n<pre><code class=\"scala\">trait Invariant[F[_]] {   def imap[A, B](fa: F[A])(dec: A =&gt; B)(enc: B =&gt; A): F[B] }  trait Conravariant[F[_]] extends Invariant[F] {   def contramap[A, B](fa: F[A])(enc: B =&gt; A): F[B]   def imap[A, B](fa: F[A])(dec: A =&gt; B)(enc: B =&gt; A): F[B] = contramap(fa)(enc) }  trait Covariant[F[_]] extends Invariant[F] {   def map[A, B](fa: F[A])(enc: A =&gt; B): F[B]   def imap[A, B](fa: F[A])(dec: A =&gt; B)(enc: B =&gt; A): F[B] = map(fa)(dec) }<\/code><\/pre>\n<p>\u041f\u0440\u043e Invariant \u0438\u0437 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 Cats:<\/p>\n<blockquote>\n<p><em>\u041a\u0430\u0436\u0434\u044b\u0439 \u043a\u043e\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u044b\u0439 (\u0430 \u0442\u0430\u043a\u0436\u0435 <\/em><a href=\"https:\/\/typelevel.org\/cats\/typeclasses\/contravariant.html\"><em><u>\u043a\u043e\u043d\u0442\u0440\u0430\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u044b\u0439<\/u><\/em><\/a><em>) \u0444\u0443\u043d\u043a\u0442\u043e\u0440 \u043f\u043e\u0440\u043e\u0436\u0434\u0430\u0435\u0442 \u0438\u043d\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u044b\u0439 \u0444\u0443\u043d\u043a\u0442\u043e\u0440, \u0438\u0433\u043d\u043e\u0440\u0438\u0440\u0443\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044e g (\u0438\u043b\u0438, \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u043a\u043e\u043d\u0442\u0440\u0430\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u043e\u0441\u0442\u0438, f).<\/em><\/p>\n<\/blockquote>\n<p>\u0433\u0434\u0435 <code>f<\/code> \u0438 <code>g<\/code> \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u043d\u0430\u0448\u0438\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c <code>dec<\/code> \u0438 <code>enc<\/code>.<\/p>\n<p>\u0412 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 <code>Cats<\/code> \u0443\u043f\u043e\u043c\u0438\u043d\u0430\u0435\u0442\u0441\u044f \u0435\u0449\u0435 \u043e\u0434\u0438\u043d \u0445\u043e\u0440\u043e\u0448\u0438\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043d\u043e\u0432\u044b\u0445 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u043e\u0432 <code>Invariant<\/code> \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0442\u0430\u0439\u043f\u043a\u043b\u0430\u0441\u0441\u0430 <code>Semigroup<\/code>.<\/p>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f <code>Semigroup[Long]<\/code> \u0438\u0437 <code>Cats<\/code>, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043b\u0435\u0433\u043a\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u043d\u043e\u0432\u044b\u0435 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u044b <code>Semigroup<\/code> \u0434\u043b\u044f \u043d\u043e\u0432\u044b\u0445 \u0442\u0438\u043f\u043e\u0432, \u0435\u0441\u043b\u0438 \u0437\u043d\u0430\u0435\u043c, \u043a\u0430\u043a \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0434\u0438\u043d \u0442\u0438\u043f \u0432 \u0434\u0440\u0443\u0433\u043e\u0439 \u0438 \u043e\u0431\u0440\u0430\u0442\u043d\u043e. \u0412 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f <code>Long -&gt; Date<\/code> \u0438 <code>Date -&gt; Long<\/code>, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0440\u0430\u043d\u0435\u0435:<\/p>\n<pre><code class=\"scala\">import cats._ import cats.implicits._  def longToDate: Long =&gt; Date = new Date(_) def dateToLong: Date =&gt; Long = _.getTime  implicit val semigroupDate: Semigroup[Date] =   Semigroup[Long].imap(longToDate)(dateToLong)  val today: Date = longToDate(1449088684104l) val timeLeft: Date = longToDate(1900918893l) today |+| timeLeft \/\/ res1: Date = Thu Dec 24 21:40:02 CET 2015<\/code><\/pre>\n<hr>\n<blockquote>\n<p>\u041c\u0430\u0442\u0435\u0440\u0438\u0430\u043b \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043b\u0435\u043d \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u043a\u0443\u0440\u0441\u0430&nbsp;<a href=\"https:\/\/otus.pw\/A0A7\/\">&#171;Scala-\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a&#187;.<\/a><\/p>\n<p>\u0412\u0441\u0435\u0445 \u0436\u0435\u043b\u0430\u044e\u0449\u0438\u0445 \u043f\u0440\u0438\u0433\u043b\u0430\u0448\u0430\u0435\u043c \u043d\u0430 \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u0443\u0440\u043e\u043a&nbsp;<a href=\"https:\/\/otus.pw\/1yUL\/\">\u00ab\u0420\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e REST API c \u043f\u043e\u043c\u043e\u0449\u044c\u044e HTTP4S \u0438 ZIO\u00bb.<\/a>&nbsp;\u041d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e \u0432\u0435\u0431 \u0441\u0435\u0440\u0432\u0438\u0441\u0430 \u0441 REST API, \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b (\u043f\u0443\u0442\u0438, \u0431\u0438\u0437\u043d\u0435\u0441 \u043b\u043e\u0433\u0438\u043a\u0430, \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0434\u0430\u043d\u043d\u044b\u043c, \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f), \u0430 \u0442\u0430\u043a\u0436\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043a\u0430\u043a \u0434\u0440\u0443\u0436\u0430\u0442 \u0442\u0430\u043a\u0438\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u0435 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438, \u043a\u0430\u043a http4s, cats, zio \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u043e\u0434\u043d\u043e\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f.&nbsp;<a href=\"https:\/\/otus.pw\/1yUL\/\"><strong>\u0420\u0415\u0413\u0418\u0421\u0422\u0420\u0410\u0426\u0418\u042f<\/strong><\/a><\/p>\n<\/blockquote>\n<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/company\/otus\/blog\/568118\/\"> https:\/\/habr.com\/ru\/company\/otus\/blog\/568118\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\">\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u043f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u043c \u043e \u0435\u0449\u0435 \u043e\u0434\u043d\u043e\u043c \u0444\u0443\u043d\u043a\u0442\u043e\u0440\u0435 \u2014 \u0438\u043d\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u043e\u043c (Invariant Functor). \u0423\u0436\u0435 \u0431\u044b\u043b\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0441\u0442\u043e\u0432 \u043e \u043a\u043e\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u044b\u0445 \u0444\u0443\u043d\u043a\u0442\u043e\u0440\u0430\u0445 (\u043d\u0430\u0437\u044b\u0432\u0430\u0435\u043c\u044b\u0445 \u043f\u0440\u043e\u0441\u0442\u043e &#171;\u0444\u0443\u043d\u043a\u0442\u043e\u0440\u0430\u043c\u0438&#187;) \u0438 <a href=\"https:\/\/medium.com\/p\/2af1141c5956\"><u>\u043a\u043e\u043d\u0442\u0440\u0430\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u044b\u0445 \u0444\u0443\u043d\u043a\u0442\u043e\u0440\u0430\u0445<\/u><\/a>. \u0415\u0441\u043b\u0438 \u043a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u044f \u043a\u043e\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u044b\u0445 \u0438 \u043a\u043e\u043d\u0442\u0440\u0430\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u044b\u0445 \u0444\u0443\u043d\u043a\u0442\u043e\u0440\u043e\u0432 \u0432\u0430\u043c \u043f\u043e\u043d\u044f\u0442\u043d\u0430, \u0442\u043e \u0441 \u0438\u043d\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u044b\u043c \u0432\u0441\u0435 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u043e\u0441\u0442\u043e \u2014 \u043e\u043d \u0441\u043e\u0447\u0435\u0442\u0430\u0435\u0442 \u0432 \u0441\u0435\u0431\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u043e\u0438\u0445 \u0432\u044b\u0448\u0435\u0443\u043f\u043e\u043c\u044f\u043d\u0443\u0442\u044b\u0445 \u0444\u0443\u043d\u043a\u0442\u043e\u0440\u043e\u0432.<\/p>\n<p>\u041a\u0430\u043a \u0432\u044b \u043f\u043e\u043c\u043d\u0438\u0442\u0435, \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0444\u0443\u043d\u043a\u0442\u043e\u0440\u043e\u0432 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0442\u044c \u043e\u0434\u0438\u043d \u0442\u0438\u043f \u0432 \u0434\u0440\u0443\u0433\u043e\u0439 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>f<\/code>:<\/p>\n<pre><code class=\"scala\">trait Functor[F[_]] {   def map[A, B](fa: F[A])(f: A =&gt; B): F[B] }<\/code><\/pre>\n<p>\u0421 \u043a\u043e\u043d\u0442\u0440\u0430\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u044b\u043c\u0438 \u0444\u0443\u043d\u043a\u0442\u043e\u0440\u0430\u043c\u0438 \u043c\u044b \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u043c \u0442\u0430\u043a\u0436\u0435, \u043d\u043e \u043c\u0435\u043d\u044f\u0435\u043c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 \u0442\u0438\u043f\u044b \u0432 <code>f<\/code>:<\/p>\n<pre><code class=\"scala\">def contramap[A, B](fa: F[A])(f: B =&gt; A): F[B]<\/code><\/pre>\n<p>\u042d\u0442\u043e \u0431\u044b\u0432\u0430\u0435\u0442 \u043f\u043e\u043b\u0435\u0437\u043d\u043e, \u043a\u043e\u0433\u0434\u0430 \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u043d\u043e\u0432\u044b\u0435 \u043d\u0435\u044f\u0432\u043d\u044b\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043a\u0430\u043a\u043e\u0433\u043e-\u0442\u043e \u0442\u0430\u0439\u043f\u043a\u043b\u0430\u0441\u0441\u0430, \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0438\u043c\u0435\u044e\u0449\u0438\u0435\u0441\u044f \u0438\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438.<\/p>\n<p>\u041a\u043e\u0440\u043e\u0447\u0435 \u0433\u043e\u0432\u043e\u0440\u044f, <code>map<\/code> \u0432 \u0444\u0443\u043d\u043a\u0442\u043e\u0440\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f, \u0435\u0441\u043b\u0438 \u043c\u044b \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432 \u043a\u043e\u043d\u0435\u0446 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438, \u0430 <code>contramap<\/code> \u0432 \u043a\u043e\u043d\u0442\u0440\u0430\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u043e\u043c \u0444\u0443\u043d\u043a\u0442\u043e\u0440\u0435, \u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0438\u0445 \u0432 \u043d\u0430\u0447\u0430\u043b\u043e. \u0414\u043b\u044f \u0438\u043d\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u043e\u0433\u043e \u0444\u0443\u043d\u043a\u0442\u043e\u0440\u0430 \u0435\u0441\u0442\u044c \u0435\u0449\u0435 \u0442\u0440\u0435\u0442\u0438\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u2014 <code>imap<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0432 \u043e\u0431\u043e\u0438\u0445 \u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f\u0445.<\/p>\n<p>\u0418\u043d\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0442\u043e\u0440\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0442 \u043c\u0435\u0442\u043e\u0434 <code>imap<\/code>,\u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u044d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442\u0435\u043d \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0446\u0438\u0438 <code>map<\/code> \u0438 <code>contramap<\/code>. \u041c\u0435\u0442\u043e\u0434 <code>imap<\/code> \u2014 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u043d\u043e\u0432\u044b\u0435 \u0442\u0430\u0439\u043f\u043a\u043b\u0430\u0441\u0441\u044b \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043f\u0430\u0440\u044b \u0434\u0432\u0443\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0445 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0439.<\/p>\n<p>\u0421\u0430\u043c\u044b\u0439 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0438 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0438\u043d\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u044b\u0445 \u0444\u0443\u043d\u043a\u0442\u043e\u0440\u043e\u0432 \u2014 \u044d\u0442\u043e \u043a\u043e\u0434\u0435\u043a\u0438 \u0438 \u043f\u0430\u0440\u0441\u0435\u0440\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0432 \u043e\u0431\u043e\u0438\u0445 \u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f\u0445.<\/p>\n<pre><code class=\"scala\">trait CustomParser[A] {   def encode(value: A): String   def decode(value: String): A   def imap[B](dec: A =&gt; B, enc: B =&gt; A): CustomParser[B] = ??? }  def encode[A](value: A)(implicit c: CustomParser[A]): String = c.encode(value) def decode[A](value: String)(implicit c: CustomParser[A]): A = c.decode(value)<\/code><\/pre>\n<p>\u0412 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0435 Cats \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 <code>imap<\/code> \u0432 \u0438\u043d\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u043e\u043c \u0444\u0443\u043d\u043a\u0442\u043e\u0440\u0435 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u043e:<\/p>\n<pre><code class=\"scala\">def imap[A, B](fa: F[A])(f: A =&gt; B)(g: B =&gt; A): F[B]<\/code><\/pre>\n<p>\u041c\u043e\u0436\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043d\u0430\u0448 <code>imap<\/code> \u0432 \u0442\u0435\u0440\u043c\u0438\u043d\u0430\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 <code>encode<\/code> \u0438 <code>decode<\/code>:<\/p>\n<pre><code class=\"scala\">def imap[B](dec: A =&gt; B, enc: B =&gt; A): CustomParser[B] = { val self = this     new CustomParser[B] {       def encode(value: B): String =         self.encode(enc(value))       def decode(value: String): B =         dec(self.decode(value))     }    }<\/code><\/pre>\n<p>\u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0438\u043c\u0435\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 <code>encode<\/code>\/<code>decode<\/code>, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c <code>imap<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u0442 \u043d\u0430\u043c \u043b\u0435\u0433\u043a\u043e \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u0435 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u044b <code>CustomParser<\/code> \u0434\u043b\u044f \u0434\u0440\u0443\u0433\u0438\u0445 \u0442\u0438\u043f\u043e\u0432:<\/p>\n<pre><code class=\"scala\">implicit val longParser: CustomParser[Long] =   new CustomParser[Long] {     def encode(value: Long): String = value.toString     def decode(value: String): Long = value.toLong   }<\/code><\/pre>\n<p>\u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f <code>CustomParser<\/code> \u0434\u043b\u044f <code>Long<\/code> \u0432 \u043d\u0430\u0448\u0435\u0439 \u043d\u0435\u044f\u0432\u043d\u043e\u0439 \u043e\u0431\u043b\u0430\u0441\u0442\u0438 \u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u0438, \u0438 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u0440\u0443\u0433\u043e\u0439 \u043f\u0430\u0440\u0441\u0435\u0440 \u0434\u043b\u044f <code>java.util.Date<\/code>. \u041c\u044b \u0437\u043d\u0430\u0435\u043c, \u043a\u0430\u043a \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c <code>Date<\/code> \u0432 <code>Long<\/code> \u0438 \u043e\u0431\u0440\u0430\u0442\u043d\u043e, \u043f\u043e\u044d\u0442\u043e\u043c\u0443, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0438\u0434\u0435\u044e \u0438\u043d\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u043e\u0433\u043e \u0444\u0443\u043d\u043a\u0442\u043e\u0440\u0430, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043b\u0435\u0433\u043a\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u043f\u0430\u0440\u0441\u0435\u0440:<\/p>\n<pre><code class=\"scala\">implicit val dateParser: CustomParser[Date] =   longParser.imap(new Date(_), _.getTime)<\/code><\/pre>\n<p>\u042d\u0442\u043e \u043f\u043e\u0445\u043e\u0436\u0435 \u043d\u0430 \u0443\u0436\u0435 \u043e\u0431\u0441\u0443\u0436\u0434\u0430\u0432\u0448\u0438\u0435\u0441\u044f \u0440\u0430\u043d\u0435\u0435 \u043a\u043e\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u044b\u0435 \u0438 \u043a\u043e\u043d\u0442\u0440\u0430\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0442\u043e\u0440\u044b. \u041a\u0441\u0442\u0430\u0442\u0438, \u043a\u043e\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u044b\u0439 \u0438 \u043a\u043e\u043d\u0442\u0440\u0430\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u044b\u0439 \u0444\u0443\u043d\u043a\u0442\u043e\u0440\u044b \u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u043f\u043e\u0442\u043e\u043c\u043a\u0430\u043c\u0438 \u0438\u043d\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u043e\u0433\u043e \u0444\u0443\u043d\u043a\u0442\u043e\u0440\u0430, \u0434\u0440\u0443\u0433\u0438\u043c\u0438 \u0441\u043b\u043e\u0432\u0430\u043c\u0438, \u0444\u0443\u043d\u043a\u0446\u0438\u044f <code>imap<\/code> \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u0430 \u0434\u043b\u044f \u043d\u0438\u0445 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0438\u0445 <code>map<\/code> \u0438\u043b\u0438 <code>contramap<\/code> \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e.<\/p>\n<pre><code class=\"scala\">trait Invariant[F[_]] {   def imap[A, B](fa: F[A])(dec: A =&gt; B)(enc: B =&gt; A): F[B] }  trait Conravariant[F[_]] extends Invariant[F] {   def contramap[A, B](fa: F[A])(enc: B =&gt; A): F[B]   def imap[A, B](fa: F[A])(dec: A =&gt; B)(enc: B =&gt; A): F[B] = contramap(fa)(enc) }  trait Covariant[F[_]] extends Invariant[F] {   def map[A, B](fa: F[A])(enc: A =&gt; B): F[B]   def imap[A, B](fa: F[A])(dec: A =&gt; B)(enc: B =&gt; A): F[B] = map(fa)(dec) }<\/code><\/pre>\n<p>\u041f\u0440\u043e Invariant \u0438\u0437 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 Cats:<\/p>\n<blockquote>\n<p><em>\u041a\u0430\u0436\u0434\u044b\u0439 \u043a\u043e\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u044b\u0439 (\u0430 \u0442\u0430\u043a\u0436\u0435 <\/em><a href=\"https:\/\/typelevel.org\/cats\/typeclasses\/contravariant.html\"><em><u>\u043a\u043e\u043d\u0442\u0440\u0430\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u044b\u0439<\/u><\/em><\/a><em>) \u0444\u0443\u043d\u043a\u0442\u043e\u0440 \u043f\u043e\u0440\u043e\u0436\u0434\u0430\u0435\u0442 \u0438\u043d\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u044b\u0439 \u0444\u0443\u043d\u043a\u0442\u043e\u0440, \u0438\u0433\u043d\u043e\u0440\u0438\u0440\u0443\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044e g (\u0438\u043b\u0438, \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u043a\u043e\u043d\u0442\u0440\u0430\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u043e\u0441\u0442\u0438, f).<\/em><\/p>\n<\/blockquote>\n<p>\u0433\u0434\u0435 <code>f<\/code> \u0438 <code>g<\/code> \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u043d\u0430\u0448\u0438\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c <code>dec<\/code> \u0438 <code>enc<\/code>.<\/p>\n<p>\u0412 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 <code>Cats<\/code> \u0443\u043f\u043e\u043c\u0438\u043d\u0430\u0435\u0442\u0441\u044f \u0435\u0449\u0435 \u043e\u0434\u0438\u043d \u0445\u043e\u0440\u043e\u0448\u0438\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043d\u043e\u0432\u044b\u0445 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u043e\u0432 <code>Invariant<\/code> \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0442\u0430\u0439\u043f\u043a\u043b\u0430\u0441\u0441\u0430 <code>Semigroup<\/code>.<\/p>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f <code>Semigroup[Long]<\/code> \u0438\u0437 <code>Cats<\/code>, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043b\u0435\u0433\u043a\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u043d\u043e\u0432\u044b\u0435 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u044b <code>Semigroup<\/code> \u0434\u043b\u044f \u043d\u043e\u0432\u044b\u0445 \u0442\u0438\u043f\u043e\u0432, \u0435\u0441\u043b\u0438 \u0437\u043d\u0430\u0435\u043c, \u043a\u0430\u043a \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0434\u0438\u043d \u0442\u0438\u043f \u0432 \u0434\u0440\u0443\u0433\u043e\u0439 \u0438 \u043e\u0431\u0440\u0430\u0442\u043d\u043e. \u0412 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f <code>Long -&gt; Date<\/code> \u0438 <code>Date -&gt; Long<\/code>, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0440\u0430\u043d\u0435\u0435:<\/p>\n<pre><code class=\"scala\">import cats._ import cats.implicits._  def longToDate: Long =&gt; Date = new Date(_) def dateToLong: Date =&gt; Long = _.getTime  implicit val semigroupDate: Semigroup[Date] =   Semigroup[Long].imap(longToDate)(dateToLong)  val today: Date = longToDate(1449088684104l) val timeLeft: Date = longToDate(1900918893l) today |+| timeLeft \/\/ res1: Date = Thu Dec 24 21:40:02 CET 2015<\/code><\/pre>\n<hr>\n<blockquote>\n<p>\u041c\u0430\u0442\u0435\u0440\u0438\u0430\u043b \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043b\u0435\u043d \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u043a\u0443\u0440\u0441\u0430&nbsp;<a href=\"https:\/\/otus.pw\/A0A7\/\">&#171;Scala-\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a&#187;.<\/a><\/p>\n<p>\u0412\u0441\u0435\u0445 \u0436\u0435\u043b\u0430\u044e\u0449\u0438\u0445 \u043f\u0440\u0438\u0433\u043b\u0430\u0448\u0430\u0435\u043c \u043d\u0430 \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u0443\u0440\u043e\u043a&nbsp;<a href=\"https:\/\/otus.pw\/1yUL\/\">\u00ab\u0420\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e REST API c \u043f\u043e\u043c\u043e\u0449\u044c\u044e HTTP4S \u0438 ZIO\u00bb.<\/a>&nbsp;\u041d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e \u0432\u0435\u0431 \u0441\u0435\u0440\u0432\u0438\u0441\u0430 \u0441 REST API, \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b (\u043f\u0443\u0442\u0438, \u0431\u0438\u0437\u043d\u0435\u0441 \u043b\u043e\u0433\u0438\u043a\u0430, \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0434\u0430\u043d\u043d\u044b\u043c, \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f), \u0430 \u0442\u0430\u043a\u0436\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043a\u0430\u043a \u0434\u0440\u0443\u0436\u0430\u0442 \u0442\u0430\u043a\u0438\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u0435 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438, \u043a\u0430\u043a http4s, cats, zio \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u043e\u0434\u043d\u043e\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f.&nbsp;<a href=\"https:\/\/otus.pw\/1yUL\/\"><strong>\u0420\u0415\u0413\u0418\u0421\u0422\u0420\u0410\u0426\u0418\u042f<\/strong><\/a><\/p>\n<\/blockquote>\n<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/company\/otus\/blog\/568118\/\"> https:\/\/habr.com\/ru\/company\/otus\/blog\/568118\/<\/a><br \/><\/br><\/br><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-326584","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/326584","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=326584"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/326584\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=326584"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=326584"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=326584"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}