{"id":306368,"date":"2020-07-03T15:00:24","date_gmt":"2020-07-03T15:00:24","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=306368"},"modified":"-0001-11-30T00:00:00","modified_gmt":"2020-07-03T15:00:24","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=306368","title":{"rendered":"\u041f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435 ZIO ZLayer"},"content":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/company\/otus\/blog\/509438\/\"><b><i>\u0412 \u0438\u044e\u043b\u0435 OTUS \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u043d\u043e\u0432\u044b\u0439 \u043a\u0443\u0440\u0441 <a href=\"https:\/\/otus.pw\/t5r0\/\">\u00abScala-\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u00bb<\/a>, \u0432 \u0441\u0432\u044f\u0437\u0438 \u0441 \u0447\u0435\u043c \u043c\u044b \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u0438\u043b\u0438 \u0434\u043b\u044f \u0432\u0430\u0441 \u043f\u0435\u0440\u0435\u0432\u043e\u0434 \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0433\u043e \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u0430.<\/i><\/b><\/p>\n<hr>\n<p>  \u041d\u043e\u0432\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f ZLayer \u0432 ZIO 1.0.0-RC18+ \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u043c \u0443\u043b\u0443\u0447\u0448\u0435\u043d\u0438\u0435\u043c \u0441\u0442\u0430\u0440\u043e\u0433\u043e \u043f\u0430\u0442\u0442\u0435\u0440\u043d\u0430 \u043c\u043e\u0434\u0443\u043b\u0435\u0439, \u0447\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043d\u043e\u0432\u044b\u0445 \u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432 \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u0431\u044b\u0441\u0442\u0440\u0435\u0435 \u0438 \u043b\u0435\u0433\u0447\u0435. \u041e\u0434\u043d\u0430\u043a\u043e \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435 \u044f \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0438\u043b, \u0447\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u0442\u0440\u0435\u0431\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043a\u0430\u043a\u043e\u0435-\u0442\u043e \u0432\u0440\u0435\u043c\u044f, \u0447\u0442\u043e\u0431\u044b \u043e\u0441\u0432\u043e\u0438\u0442\u044c \u044d\u0442\u0443 \u0438\u0434\u0438\u043e\u043c\u0443.<\/p>\n<p>  \u041d\u0438\u0436\u0435 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u0430\u043d\u043d\u043e\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0444\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0439 \u0432\u0435\u0440\u0441\u0438\u0438 \u043c\u043e\u0435\u0433\u043e \u0442\u0435\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u043a\u043e\u0434\u0430, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u044f \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u044e \u0440\u044f\u0434 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f. \u0411\u043e\u043b\u044c\u0448\u043e\u0435 \u0441\u043f\u0430\u0441\u0438\u0431\u043e \u0410\u0434\u0430\u043c\u0443 \u0424\u0440\u0435\u0439\u0437\u0435\u0440\u0443 \u0437\u0430 \u043f\u043e\u043c\u043e\u0449\u044c \u0432 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u0438 \u043e\u0431\u043b\u0430\u0433\u043e\u0440\u0430\u0436\u0438\u0432\u0430\u043d\u0438\u0438 \u043c\u043e\u0435\u0439 \u0440\u0430\u0431\u043e\u0442\u044b. \u0421\u0435\u0440\u0432\u0438\u0441\u044b \u043f\u0440\u0435\u0434\u043d\u0430\u043c\u0435\u0440\u0435\u043d\u043d\u043e \u0443\u043f\u0440\u043e\u0449\u0435\u043d\u044b, \u0442\u0430\u043a \u0447\u0442\u043e, \u043d\u0430\u0434\u0435\u044e\u0441\u044c, \u043e\u043d\u0438 \u0431\u0443\u0434\u0443\u0442 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u043e\u043d\u044f\u0442\u043d\u044b \u0434\u043b\u044f \u0431\u044b\u0441\u0442\u0440\u043e\u0433\u043e \u0447\u0442\u0435\u043d\u0438\u044f.<a name=\"habracut\"><\/a><\/p>\n<p>  \u042f \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u044e, \u0447\u0442\u043e \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u0431\u0430\u0437\u043e\u0432\u043e\u0435 \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u0435 ZIO \u0442\u0435\u0441\u0442\u043e\u0432, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0432\u044b \u043e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u043b\u0438\u0441\u044c \u0441 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0435\u0439 \u043a\u0430\u0441\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043c\u043e\u0434\u0443\u043b\u0435\u0439.<\/p>\n<p>  \u0412\u0435\u0441\u044c \u043a\u043e\u0434 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u0432 zio \u0442\u0435\u0441\u0442\u0430\u0445 \u0438 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u043e\u0431\u043e\u0439 \u043e\u0434\u0438\u043d \u0444\u0430\u0439\u043b.<br \/>  \u0412\u043e\u0442 \u0435\u0433\u043e \u0432\u0435\u0440\u0445\u0443\u0448\u043a\u0430:<\/p>\n<pre><code class=\"scala\">import zio._ import zio.test._ import zio.random.Random import Assertion._  object LayerTests extends DefaultRunnableSpec {    type Names = Has[Names.Service]   type Teams = Has[Teams.Service]   type History = Has[History.Service]    val firstNames = Vector( &quot;Ed&quot;, &quot;Jane&quot;, &quot;Joe&quot;, &quot;Linda&quot;, &quot;Sue&quot;, &quot;Tim&quot;, &quot;Tom&quot;) <\/code><\/pre>\n<h3>Names<\/h3>\n<p>  \u0418\u0442\u0430\u043a, \u043c\u044b \u0434\u043e\u0431\u0440\u0430\u043b\u0438\u0441\u044c \u0434\u043e \u043d\u0430\u0448\u0435\u0433\u043e \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0441\u0435\u0440\u0432\u0438\u0441\u0430 \u2014 Names (\u0418\u043c\u0435\u043d\u0430)<\/p>\n<pre><code class=\"scala\"> type Names = Has[Names.Service]    object Names {     trait Service {       def randomName: UIO[String]     }      case class NamesImpl(random: Random.Service) extends Names.Service {       println(s&quot;created namesImpl&quot;)       def randomName =          random.nextInt(firstNames.size).map(firstNames(_))     }      val live: ZLayer[Random, Nothing, Names] =       ZLayer.fromService(NamesImpl)   }    package object names {     def randomName = ZIO.accessM[Names](_.get.randomName)   } <\/code><\/pre>\n<p>  \u0422\u0443\u0442 \u0432\u0441\u0435 \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u0442\u0438\u043f\u0438\u0447\u043d\u043e\u0433\u043e \u043c\u043e\u0434\u0443\u043b\u044c\u043d\u043e\u0433\u043e \u043f\u0430\u0442\u0442\u0435\u0440\u043d\u0430.<\/p>\n<ul>\n<li>\u041e\u0431\u044a\u044f\u0432\u0438\u0442\u0435 Names \u043a\u0430\u043a \u043f\u0441\u0435\u0432\u0434\u043e\u043d\u0438\u043c \u0442\u0438\u043f\u0430 \u0434\u043b\u044f Has<\/li>\n<li>\u0412 \u043e\u0431\u044a\u0435\u043a\u0442\u0435, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u0435 Service \u043a\u0430\u043a \u0442\u0440\u0435\u0439\u0442<\/li>\n<li>\u0421\u043e\u0437\u0434\u0430\u0439\u0442\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e (\u043a\u043e\u043d\u0435\u0447\u043d\u043e, \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e),<\/li>\n<li>\u0421\u043e\u0437\u0434\u0430\u0439\u0442\u0435 ZLayer \u0432\u043d\u0443\u0442\u0440\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0434\u043b\u044f \u0434\u0430\u043d\u043d\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438. \u041a\u043e\u043d\u0432\u0435\u043d\u0446\u0438\u044f ZIO \u0438\u043c\u0435\u0435\u0442 \u0442\u0435\u043d\u0434\u0435\u043d\u0446\u0438\u044e \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0438\u0445 \u0432 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u043c \u0432\u0440\u0435\u043c\u0435\u043d\u0438.<\/li>\n<li>\u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u0431\u044a\u0435\u043a\u0442 \u043f\u0430\u043a\u0435\u0442\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u0435\u0442 \u0443\u0434\u043e\u0431\u043d\u043e\u0435 \u0434\u043b\u044f \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u0441\u043e\u043a\u0440\u0430\u0449\u0435\u043d\u0438\u0435.<\/li>\n<\/ul>\n<p>  \u0412 <b>live<\/b> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f <code>ZLayer.fromService<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u043a\u0430\u043a:<\/p>\n<pre><code class=\"scala\">def fromService[A: Tagged, B: Tagged](f: A =&gt; B): ZLayer[Has[A], Nothing, Has[B]<\/code><\/pre>\n<p>  \u0418\u0433\u043d\u043e\u0440\u0438\u0440\u0443\u044f Tagged (\u044d\u0442\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0432\u0441\u0435\u0433\u043e Has\/Layers), \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0432\u0438\u0434\u0435\u0442\u044c, \u0447\u0442\u043e \u0437\u0434\u0435\u0441\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f <i>f: A =&gt; B<\/i> \u2014 \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u0441\u0442\u043e \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u043e\u043c \u043a\u0435\u0439\u0441 \u043a\u043b\u0430\u0441\u0441\u0430 \u0434\u043b\u044f <code>NamesImpl<\/code>.<\/p>\n<p>  \u041a\u0430\u043a \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0432\u0438\u0434\u0435\u0442\u044c, \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b Names \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f Random \u0438\u0437 \u0441\u0440\u0435\u0434\u044b zio.<\/p>\n<p>  \u0412\u043e\u0442 \u0442\u0435\u0441\u0442:<\/p>\n<pre><code class=\"scala\">def namesTest = testM(&quot;names test&quot;) {     for {       name &lt;- names.randomName     }  yield {       assert(firstNames.contains(name))(equalTo(true))     }   }<\/code><\/pre>\n<p>  \u041e\u043d \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 <code>ZIO.accessM<\/code> \u0434\u043b\u044f \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f <i>Names<\/i> \u0438\u0437 \u0441\u0440\u0435\u0434\u044b. <code>_.get <\/code>\u0438\u0437\u0432\u043b\u0435\u043a\u0430\u0435\u0442 \u0441\u0435\u0440\u0432\u0438\u0441.<\/p>\n<p>  \u041c\u044b \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c <i>Names<\/i> \u0434\u043b\u044f \u0442\u0435\u0441\u0442\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code class=\"scala\"> suite(&quot;needs Names&quot;)(        namesTest     ).provideCustomLayer(Names.live), <\/code><\/pre>\n<p>  <code>provideCustomLayer<\/code> \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u043b\u043e\u0439 <i>Names<\/i> \u0432 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0443\u044e \u0441\u0440\u0435\u0434\u0443.<\/p>\n<h3>Teams<\/h3>\n<p>  \u0421\u0443\u0442\u044c <i>Teams<\/i> (\u041a\u043e\u043c\u0430\u043d\u0434\u044b) \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439 \u043c\u0435\u0436\u0434\u0443 \u043c\u043e\u0434\u0443\u043b\u044f\u043c\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u043b\u0438.<\/p>\n<pre><code class=\"scala\"> object Teams {     trait Service {       def pickTeam(size: Int): UIO[Set[String]]     }      case class TeamsImpl(names: Names.Service) extends Service {       def pickTeam(size: Int) =          ZIO.collectAll(0.until(size).map { _ =&gt; names.randomName}).map(_.toSet ) \/\/ \u0434\u0430, \u044f \u0437\u043d\u0430\u044e, \u0447\u0442\u043e \u043a\u043e\u043c\u0430\u043d\u0434\u0430 \u043c\u043e\u0436\u0435\u0442 \u0438\u043c\u0435\u0442\u044c &lt; \u0440\u0430\u0437\u043c\u0435\u0440!        }      val live: ZLayer[Names, Nothing, Teams] =       ZLayer.fromService(TeamsImpl)    }<\/code><\/pre>\n<p>  Teams \u0432\u044b\u0431\u0435\u0440\u0443\u0442 \u043a\u043e\u043c\u0430\u043d\u0434\u0443 \u0438\u0437 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b\u0445 names, \u0441\u0434\u0435\u043b\u0430\u0432 \u0432\u044b\u0431\u043e\u0440 \u043f\u043e <i>\u0440\u0430\u0437\u043c\u0435\u0440\u0443<\/i>.<br \/>  \u0421\u043b\u0435\u0434\u0443\u044f \u043f\u0430\u0442\u0442\u0435\u0440\u043d\u0430\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043c\u043e\u0434\u0443\u043b\u0435\u0439, \u0445\u043e\u0442\u044f \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b <b>pickTeam<\/b> \u043d\u0443\u0436\u043d\u044b <i>Names<\/i>, \u043c\u044b \u043d\u0435 \u043f\u043e\u043c\u0435\u0449\u0430\u0435\u043c \u0435\u0433\u043e \u0432 <i>ZIO[Names, Nothing, Set[String]]<\/i> \u2014 \u0432\u043c\u0435\u0441\u0442\u043e \u044d\u0442\u043e\u0433\u043e \u043c\u044b \u0434\u0435\u0440\u0436\u0438\u043c \u043d\u0430 \u043d\u0435\u0433\u043e \u0441\u0441\u044b\u043b\u043a\u0443 \u0432 <code>TeamsImpl<\/code>.<\/p>\n<p>  \u041d\u0430\u0448 \u043f\u0435\u0440\u0432\u044b\u0439 \u0442\u0435\u0441\u0442 \u043f\u0440\u043e\u0441\u0442.<\/p>\n<pre><code class=\"scala\"> def justTeamsTest = testM(&quot;small team test&quot;) {     for {       team &lt;- teams.pickTeam(1)     }  yield {       assert(team.size)(equalTo(1))     }   }<\/code><\/pre>\n<p>  \u0427\u0442\u043e\u0431\u044b \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0435\u0433\u043e, \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0435\u043c\u0443 \u0441\u043b\u043e\u0439 Teams:<\/p>\n<pre><code class=\"scala\"> suite(&quot;needs just Team&quot;)(       justTeamsTest     ).provideCustomLayer(Names.live &gt;&gt;&gt; Teams.live),<\/code><\/pre>\n<p>  <i>\u0427\u0442\u043e \u0442\u0430\u043a\u043e\u0435 \u00ab&gt;&gt;&gt;\u00bb?<\/i> <\/p>\n<p>  \u042d\u0442\u043e \u0432\u0435\u0440\u0442\u0438\u043a\u0430\u043b\u044c\u043d\u0430\u044f \u043a\u043e\u043c\u043f\u043e\u0437\u0438\u0446\u0438\u044f. \u041e\u043d\u0430 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442, \u0447\u0442\u043e \u043d\u0430\u043c \u043d\u0443\u0436\u0435\u043d \u0441\u043b\u043e\u0439 <i>Names<\/i>, \u043a\u043e\u0442\u043e\u0440\u043e\u043c\u0443 \u043d\u0443\u0436\u0435\u043d \u0441\u043b\u043e\u0439 <i>Teams<\/i>.<\/p>\n<p>  \u0422\u0435\u043c \u043d\u0435 \u043c\u0435\u043d\u0435\u0435, \u043f\u0440\u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0435 \u044d\u0442\u043e\u0433\u043e, \u0435\u0441\u0442\u044c \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0430\u044f \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430.<\/p>\n<pre><code class=\"smalltalk\">created namesImpl created namesImpl [32m+[0m individually   [32m+[0m needs just Team     [32m+[0m small team test [36mRan 1 test in 225 ms: 1 succeeded, 0 ignored, 0 failed[0m<\/code><\/pre>\n<p>  \u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044f\u0441\u044c \u043a \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044e <code>NamesImpl<\/code><\/p>\n<pre><code class=\"scala\">case class NamesImpl(random: Random.Service) extends Names.Service {       println(s&quot;created namesImpl&quot;)       def randomName =          random.nextInt(firstNames.size).map(firstNames(_))     }<\/code><\/pre>\n<p>  \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u043d\u0430\u0448 <code>NamesImpl<\/code> \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u0434\u0432\u0430\u0436\u0434\u044b. \u0427\u0435\u043c \u044d\u0442\u043e \u0447\u0440\u0435\u0432\u0430\u0442\u043e, \u0435\u0441\u043b\u0438 \u043d\u0430\u0448 \u0441\u0435\u0440\u0432\u0438\u0441 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043a\u0430\u043a\u043e\u0439-\u043d\u0438\u0431\u0443\u0434\u044c \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u044b\u0439 \u0440\u0435\u0441\u0443\u0440\u0441 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f? \u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435, \u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0432\u043e\u0432\u0441\u0435 \u043d\u0435 \u0432 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u0435 Layers \u2014 \u0441\u043b\u043e\u0438 \u0437\u0430\u043f\u043e\u043c\u0438\u043d\u0430\u044e\u0442\u0441\u044f \u0438 \u043d\u0435 \u0441\u043e\u0437\u0434\u0430\u044e\u0442\u0441\u044f \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0440\u0430\u0437 \u0432 \u0433\u0440\u0430\u0444\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439. \u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u044d\u0442\u043e \u0430\u0440\u0442\u0435\u0444\u0430\u043a\u0442 \u0442\u0435\u0441\u0442\u043e\u0432\u043e\u0439 \u0441\u0440\u0435\u0434\u044b.<\/p>\n<p>  \u0418\u0437\u043c\u0435\u043d\u0438\u043c \u043d\u0430\u0448 \u043d\u0430\u0431\u043e\u0440 \u0442\u0435\u0441\u0442\u043e\u0432 \u043d\u0430:<\/p>\n<pre><code class=\"scala\">suite(&quot;needs just Team&quot;)(       justTeamsTest     ).provideCustomLayerShared(Names.live &gt;&gt;&gt; Teams.live),<\/code><\/pre>\n<p>  \u042d\u0442\u043e \u0438\u0441\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443, \u0447\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u0441\u043b\u043e\u0439 \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u0432 \u0442\u0435\u0441\u0442\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0440\u0430\u0437<\/p>\n<p>  <b>JustTeamsTest<\/b> \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e <i>teams<\/i>. \u041d\u043e \u0447\u0442\u043e, \u0435\u0441\u043b\u0438 \u044f \u0445\u043e\u0442\u0435\u043b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043a <i>Teams<\/i> \u0438 <i>Names<\/i>?<\/p>\n<pre><code class=\"scala\"> def inMyTeam = testM(&quot;combines names and teams&quot;) {     for {       name &lt;- names.randomName       team &lt;- teams.pickTeam(5)       _ = if (team.contains(name)) println(&quot;one of mine&quot;)         else println(&quot;not mine&quot;)     } yield assertCompletes   }<\/code><\/pre>\n<p>  \u0427\u0442\u043e\u0431\u044b \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u043b\u043e, \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0438 \u0442\u043e, \u0438 \u0434\u0440\u0443\u0433\u043e\u0435:<\/p>\n<pre><code class=\"scala\"> suite(&quot;needs Names and Teams&quot;)(        inMyTeam     ).provideCustomLayer(Names.live ++ (Names.live &gt;&gt;&gt; Teams.live)),<\/code><\/pre>\n<p>  \u0417\u0434\u0435\u0441\u044c \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0442\u043e\u0440 <b>++<\/b> \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0441\u043b\u043e\u044f <i>Names<\/i> \u0441 <i>Teams<\/i>. \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430 \u0438 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0441\u043a\u043e\u0431\u043a\u0438<\/p>\n<pre><code class=\"scala\">(Names.live &gt;&gt;&gt; Teams.live)<\/code><\/pre>\n<p>  \u0412\u043d\u0430\u0447\u0430\u043b\u0435, \u044f \u0441\u0430\u043c \u043d\u0430 \u044d\u0442\u043e \u043f\u043e\u043f\u0430\u043b\u0441\u044f \u2014 \u0432 \u043f\u0440\u043e\u0442\u0438\u0432\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0431\u0443\u0434\u0435\u0442 \u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0435 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e.<\/p>\n<h3>History<\/h3>\n<p>  History (\u0418\u0441\u0442\u043e\u0440\u0438\u044f) \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u0436\u043d\u0435\u0435.<\/p>\n<pre><code class=\"scala\">object History {          trait Service {       def wonLastYear(team: Set[String]): Boolean     }      case class HistoryImpl(lastYearsWinners: Set[String]) extends Service {       def wonLastYear(team: Set[String]) = lastYearsWinners == team     }          val live: ZLayer[Teams, Nothing, History] = ZLayer.fromServiceM { teams =&gt;        teams.pickTeam(5).map(nt =&gt; HistoryImpl(nt))     }        }<\/code><\/pre>\n<p>  \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 <code>HistoryImpl<\/code> \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e <i>Names<\/i>. \u041d\u043e \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0442\u0430\u043a\u043e\u0435 \u2014 \u0438\u0437\u0432\u043b\u0435\u0447\u044c \u0435\u0433\u043e \u0438\u0437 <i>Teams<\/i>. \u0418 \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f ZIO \u2014 \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c <code>ZLayer.fromServiceM<\/code>, \u0447\u0442\u043e\u0431\u044b \u043e\u043d \u0434\u0430\u043b \u043d\u0430\u043c \u0442\u043e, \u0447\u0442\u043e \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e.<br \/>  \u0422\u0435\u0441\u0442 \u043f\u0440\u043e\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u043f\u043e \u0442\u043e\u0439 \u0436\u0435 \u0441\u0445\u0435\u043c\u0435, \u0447\u0442\u043e \u0438 \u0440\u0430\u043d\u044c\u0448\u0435:<\/p>\n<pre><code class=\"scala\"> def wonLastYear = testM(&quot;won last year&quot;) {     for {       team &lt;- teams.pickTeams(5)       ly &lt;- history.wonLastYear(team)     } yield assertCompletes   }      suite(&quot;needs History and Teams&quot;)(       wonLastYear     ).provideCustomLayerShared((Names.live &gt;&gt;&gt; Teams.live) ++ (Names.live &gt;&gt;&gt; Teams.live &gt;&gt;&gt; History.live))<\/code><\/pre>\n<p>  \u0418 \u0432\u0441\u0435.<\/p>\n<h3>Throwable \u043e\u0448\u0438\u0431\u043a\u0438<\/h3>\n<p>  \u0412 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u043e\u043c \u0432\u044b\u0448\u0435 \u043a\u043e\u0434\u0435 \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u0432\u044b \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0435 ZLayer[R, Nothing, T] \u2014 \u0434\u0440\u0443\u0433\u0438\u043c\u0438 \u0441\u043b\u043e\u0432\u0430\u043c\u0438, \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044f \u0441\u043b\u0443\u0436\u0431\u044b \u0441\u0440\u0435\u0434\u044b \u0438\u043c\u0435\u0435\u0442 \u0442\u0438\u043f Nothing. \u041d\u043e \u0435\u0441\u043b\u0438 \u043e\u043d \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u0447\u0442\u043e-\u0442\u043e \u0432\u0440\u043e\u0434\u0435 \u0447\u0442\u0435\u043d\u0438\u044f \u0438\u0437 \u0444\u0430\u0439\u043b\u0430 \u0438\u043b\u0438 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445, \u0442\u043e, \u0441\u043a\u043e\u0440\u0435\u0435 \u0432\u0441\u0435\u0433\u043e, \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 ZLayer[R, Throwable, T] \u2014 \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0442\u0430\u043a\u043e\u0433\u043e \u0440\u043e\u0434\u0430 \u0432\u0435\u0449\u0438 \u0447\u0430\u0441\u0442\u043e \u0432\u043a\u043b\u044e\u0447\u0430\u044e\u0442 \u0438\u043c\u0435\u043d\u043d\u043e \u0442\u043e\u0442 \u0432\u043d\u0435\u0448\u043d\u0438\u0439 \u0444\u0430\u043a\u0442\u043e\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435. \u0422\u0430\u043a \u0447\u0442\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u044c\u0442\u0435 \u0441\u0435\u0431\u0435, \u0447\u0442\u043e \u0432 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0438 Names \u043f\u0440\u043e\u0438\u0437\u043e\u0448\u043b\u0430 \u043e\u0448\u0438\u0431\u043a\u0430. \u0414\u043b\u044f \u0432\u0430\u0448\u0438\u0445 \u0442\u0435\u0441\u0442\u043e\u0432 \u0435\u0441\u0442\u044c \u0441\u043f\u043e\u0441\u043e\u0431 \u043e\u0431\u043e\u0439\u0442\u0438 \u044d\u0442\u043e:<\/p>\n<pre><code class=\"scala\">val live: ZLayer[Random, Throwable, Names] = ???<\/code><\/pre>\n<p>  \u0437\u0430\u0442\u0435\u043c \u0432 \u043a\u043e\u043d\u0446\u0435 \u0442\u0435\u0441\u0442\u0430<\/p>\n<pre><code class=\"scala\">.provideCustomLayer(Names.live).mapError(TestFailure.test)<\/code><\/pre>\n<p>  <code>mapError<\/code> \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 <code>throwable<\/code> \u0432 \u0441\u0431\u043e\u0439 \u0442\u0435\u0441\u0442\u0430 \u2014 \u044d\u0442\u043e \u0442\u043e, \u0447\u0442\u043e \u0432\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u2014 \u043e\u043d \u043c\u043e\u0436\u0435\u0442 \u0441\u043a\u0430\u0437\u0430\u0442\u044c, \u0447\u0442\u043e \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0439 \u0444\u0430\u0439\u043b \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0438\u043b\u0438 \u0447\u0442\u043e-\u0442\u043e \u0432\u0440\u043e\u0434\u0435 \u044d\u0442\u043e\u0433\u043e.<\/p>\n<h3>\u0411\u043e\u043b\u044c\u0448\u0435 ZEnv \u043a\u0435\u0439\u0441\u043e\u0432<\/h3>\n<p>  \u0412 \u00ab\u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0435\u00bb \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u0440\u0435\u0434\u044b \u0432\u0445\u043e\u0434\u044f\u0442 Clock (\u0447\u0430\u0441\u044b) \u0438 Random. \u0412 \u043d\u0430\u0448\u0438\u0445 Names \u043c\u044b \u0443\u0436\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438 Random. \u041d\u043e \u0447\u0442\u043e, \u0435\u0441\u043b\u0438 \u043c\u044b \u0442\u0430\u043a\u0436\u0435 \u0445\u043e\u0442\u0438\u043c, \u0447\u0442\u043e\u0431\u044b \u043e\u0434\u0438\u043d \u0438\u0437 \u044d\u0442\u0438\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0435\u0449\u0435 \u0431\u043e\u043b\u044c\u0448\u0435 \u00ab\u043f\u043e\u043d\u0438\u0437\u0438\u043b\u00bb \u043d\u0430\u0448\u0438 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438? \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u044f \u0441\u043e\u0437\u0434\u0430\u043b \u0432\u0442\u043e\u0440\u0443\u044e \u0432\u0435\u0440\u0441\u0438\u044e History \u2014 History2 \u2014 \u0438 \u0437\u0434\u0435\u0441\u044c \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430 \u043d\u0443\u0436\u0435\u043d Clock.<\/p>\n<pre><code class=\"scala\"> object History2 {          trait Service {       def wonLastYear(team: Set[String]): Boolean     }      case class History2Impl(lastYearsWinners: Set[String], lastYear: Long) extends Service {       def wonLastYear(team: Set[String]) = lastYearsWinners == team     }          val live: ZLayer[Clock with Teams, Nothing, History2] = ZLayer.fromEffect {        for {         someTime &lt;- ZIO.accessM[Clock](_.get.nanoTime)                 team &lt;- teams.pickTeam(5)       } yield History2Impl(team, someTime)     }        } <\/code><\/pre>\n<p>  \u042d\u0442\u043e \u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440, \u043d\u043e \u0432\u0430\u0436\u043d\u043e\u0439 \u0447\u0430\u0441\u0442\u044c\u044e \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0442\u043e, \u0447\u0442\u043e \u0441\u0442\u0440\u043e\u043a\u0430<\/p>\n<pre><code class=\"scala\"> someTime &lt;- ZIO.accessM[Clock](_.get.nanoTime)<\/code><\/pre>\n<p>  \u0437\u0430\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043d\u0430\u0441 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c \u0447\u0430\u0441\u044b \u0432 \u043d\u0443\u0436\u043d\u043e\u043c \u043c\u0435\u0441\u0442\u0435.<\/p>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c <code>.provideCustomLayer<\/code> \u043c\u043e\u0436\u0435\u0442 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043d\u0430\u0448 \u0441\u043b\u043e\u0439 \u0432 \u0441\u0442\u0435\u043a \u0441\u043b\u043e\u0435\u0432, \u0438 \u043e\u043d \u0432\u043e\u043b\u0448\u0435\u0431\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0432\u044b\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u0435\u0442 Random \u0432 Names. \u041d\u043e \u044d\u0442\u043e\u0433\u043e \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442\u044c \u0434\u043b\u044f \u0447\u0430\u0441\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0442\u0440\u0435\u0431\u0443\u044e\u0442\u0441\u044f \u043d\u0438\u0436\u0435, \u0432 History2. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043a\u043e\u0434 \u041d\u0415 \u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u0443\u0435\u0442\u0441\u044f:<\/p>\n<pre><code class=\"scala\">def wonLastYear2 = testM(&quot;won last year&quot;) {     for {       team &lt;- teams.pickTeam(5)       _ &lt;- history2.wonLastYear(team)     } yield assertCompletes   }  \/\/ ...     suite(&quot;needs History2 and Teams&quot;)(       wonLastYear2     ).provideCustomLayerShared((Names.live &gt;&gt;&gt; Teams.live) ++ (Names.live &gt;&gt;&gt; Teams.live &gt;&gt;&gt; History2.live)),<\/code><\/pre>\n<p>  \u0412\u043c\u0435\u0441\u0442\u043e \u044d\u0442\u043e\u0433\u043e \u0432\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c <code>History2.live<\/code> \u0447\u0430\u0441\u044b \u0432 \u044f\u0432\u043d\u043e\u043c \u0432\u0438\u0434\u0435, \u0447\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442\u0441\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code class=\"scala\"> suite(&quot;needs History2 and Teams&quot;)(       wonLastYear2     ).provideCustomLayerShared((Names.live &gt;&gt;&gt; Teams.live) ++ (((Names.live &gt;&gt;&gt; Teams.live) ++ Clock.any) &gt;&gt;&gt; History2.live))<\/code><\/pre>\n<p>  <code>Clock.any<\/code> \u2014 \u044d\u0442\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u043b\u044e\u0431\u044b\u0435 \u0447\u0430\u0441\u044b, \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b\u0435 \u0441\u0432\u0435\u0440\u0445\u0443. \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u044d\u0442\u043e \u0431\u0443\u0434\u0443\u0442 \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0435 \u0447\u0430\u0441\u044b, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043c\u044b \u043d\u0435 \u043f\u044b\u0442\u0430\u043b\u0438\u0441\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>Clock.live<\/code>.<\/p>\n<h3>\u0418\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434<br \/>  <\/h3>\n<p>\u041f\u043e\u043b\u043d\u044b\u0439 \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434 (\u0437\u0430 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435\u043c throwable) \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u043d\u0438\u0436\u0435:<\/p>\n<pre><code class=\"scala\">import zio._ import zio.test._ import zio.random.Random import Assertion._  import zio._ import zio.test._ import zio.random.Random import zio.clock.Clock import Assertion._  object LayerTests extends DefaultRunnableSpec {    type Names = Has[Names.Service]   type Teams = Has[Teams.Service]   type History = Has[History.Service]   type History2 = Has[History2.Service]    val firstNames = Vector( &quot;Ed&quot;, &quot;Jane&quot;, &quot;Joe&quot;, &quot;Linda&quot;, &quot;Sue&quot;, &quot;Tim&quot;, &quot;Tom&quot;)    object Names {     trait Service {       def randomName: UIO[String]     }      case class NamesImpl(random: Random.Service) extends Names.Service {       println(s&quot;created namesImpl&quot;)       def randomName =          random.nextInt(firstNames.size).map(firstNames(_))     }      val live: ZLayer[Random, Nothing, Names] =       ZLayer.fromService(NamesImpl)   }      object Teams {     trait Service {       def pickTeam(size: Int): UIO[Set[String]]     }      case class TeamsImpl(names: Names.Service) extends Service {       def pickTeam(size: Int) =          ZIO.collectAll(0.until(size).map { _ =&gt; names.randomName}).map(_.toSet )  \/\/ \u0434\u0430, \u044f \u0437\u043d\u0430\u044e, \u0447\u0442\u043e \u043a\u043e\u043c\u0430\u043d\u0434\u0430 \u043c\u043e\u0436\u0435\u0442 \u0438\u043c\u0435\u0442\u044c &lt; \u0440\u0430\u0437\u043c\u0435\u0440!        }      val live: ZLayer[Names, Nothing, Teams] =       ZLayer.fromService(TeamsImpl)    }     object History {          trait Service {       def wonLastYear(team: Set[String]): Boolean     }      case class HistoryImpl(lastYearsWinners: Set[String]) extends Service {       def wonLastYear(team: Set[String]) = lastYearsWinners == team     }          val live: ZLayer[Teams, Nothing, History] = ZLayer.fromServiceM { teams =&gt;        teams.pickTeam(5).map(nt =&gt; HistoryImpl(nt))     }        }      object History2 {          trait Service {       def wonLastYear(team: Set[String]): Boolean     }      case class History2Impl(lastYearsWinners: Set[String], lastYear: Long) extends Service {       def wonLastYear(team: Set[String]) = lastYearsWinners == team     }          val live: ZLayer[Clock with Teams, Nothing, History2] = ZLayer.fromEffect {        for {         someTime &lt;- ZIO.accessM[Clock](_.get.nanoTime)                 team &lt;- teams.pickTeam(5)       } yield History2Impl(team, someTime)     }        }       def namesTest = testM(&quot;names test&quot;) {     for {       name &lt;- names.randomName     }  yield {       assert(firstNames.contains(name))(equalTo(true))     }   }    def justTeamsTest = testM(&quot;small team test&quot;) {     for {       team &lt;- teams.pickTeam(1)     }  yield {       assert(team.size)(equalTo(1))     }   }      def inMyTeam = testM(&quot;combines names and teams&quot;) {     for {       name &lt;- names.randomName       team &lt;- teams.pickTeam(5)       _ = if (team.contains(name)) println(&quot;one of mine&quot;)         else println(&quot;not mine&quot;)     } yield assertCompletes   }         def wonLastYear = testM(&quot;won last year&quot;) {     for {       team &lt;- teams.pickTeam(5)       _ &lt;- history.wonLastYear(team)     } yield assertCompletes   }      def wonLastYear2 = testM(&quot;won last year&quot;) {     for {       team &lt;- teams.pickTeam(5)       _ &lt;- history2.wonLastYear(team)     } yield assertCompletes   }     val individually = suite(&quot;individually&quot;)(     suite(&quot;needs Names&quot;)(        namesTest     ).provideCustomLayer(Names.live),     suite(&quot;needs just Team&quot;)(       justTeamsTest     ).provideCustomLayer(Names.live &gt;&gt;&gt; Teams.live),      suite(&quot;needs Names and Teams&quot;)(        inMyTeam     ).provideCustomLayer(Names.live ++ (Names.live &gt;&gt;&gt; Teams.live)),     suite(&quot;needs History and Teams&quot;)(       wonLastYear     ).provideCustomLayerShared((Names.live &gt;&gt;&gt; Teams.live) ++ (Names.live &gt;&gt;&gt; Teams.live &gt;&gt;&gt; History.live)),     suite(&quot;needs History2 and Teams&quot;)(       wonLastYear2     ).provideCustomLayerShared((Names.live &gt;&gt;&gt; Teams.live) ++ (((Names.live &gt;&gt;&gt; Teams.live) ++ Clock.any) &gt;&gt;&gt; History2.live))   )      val altogether = suite(&quot;all together&quot;)(       suite(&quot;needs Names&quot;)(        namesTest     ),     suite(&quot;needs just Team&quot;)(       justTeamsTest     ),      suite(&quot;needs Names and Teams&quot;)(        inMyTeam     ),     suite(&quot;needs History and Teams&quot;)(       wonLastYear     ),   ).provideCustomLayerShared(Names.live ++ (Names.live &gt;&gt;&gt; Teams.live) ++ (Names.live &gt;&gt;&gt; Teams.live &gt;&gt;&gt; History.live))    override def spec = (     individually   ) }  import LayerTests._  package object names {   def randomName = ZIO.accessM[Names](_.get.randomName) }  package object teams {   def pickTeam(nPicks: Int) = ZIO.accessM[Teams](_.get.pickTeam(nPicks)) }    package object history {   def wonLastYear(team: Set[String]) = ZIO.access[History](_.get.wonLastYear(team)) }  package object history2 {   def wonLastYear(team: Set[String]) = ZIO.access[History2](_.get.wonLastYear(team)) } <\/code><\/pre>\n<p>  \u0415\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u0431\u043e\u043b\u0435\u0435 \u0441\u043b\u043e\u0436\u043d\u044b\u0435 \u0432\u043e\u043f\u0440\u043e\u0441\u044b, \u043e\u0431\u0440\u0430\u0449\u0430\u0439\u0442\u0435\u0441\u044c \u0432 Discord #zio-users \u0438\u043b\u0438 \u043f\u043e\u0441\u0435\u0442\u0438\u0442\u0435 \u0441\u0430\u0439\u0442 \u0438 <a href=\"https:\/\/zio.dev\/\">\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044e<\/a> zio.<\/p>\n<hr>\n<p>  <a href=\"https:\/\/otus.pw\/t5r0\/\">\u0423\u0437\u043d\u0430\u0442\u044c \u043e \u043a\u0443\u0440\u0441\u0435 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435.<\/a><\/p>\n<hr>\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\/509438\/\"> https:\/\/habr.com\/ru\/company\/otus\/blog\/509438\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/company\/otus\/blog\/509438\/\"><b><i>\u0412 \u0438\u044e\u043b\u0435 OTUS \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u043d\u043e\u0432\u044b\u0439 \u043a\u0443\u0440\u0441 <a href=\"https:\/\/otus.pw\/t5r0\/\">\u00abScala-\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u00bb<\/a>, \u0432 \u0441\u0432\u044f\u0437\u0438 \u0441 \u0447\u0435\u043c \u043c\u044b \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u0438\u043b\u0438 \u0434\u043b\u044f \u0432\u0430\u0441 \u043f\u0435\u0440\u0435\u0432\u043e\u0434 \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0433\u043e \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u0430.<\/i><\/b><\/p>\n<hr>\n<p>  \u041d\u043e\u0432\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f ZLayer \u0432 ZIO 1.0.0-RC18+ \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u043c \u0443\u043b\u0443\u0447\u0448\u0435\u043d\u0438\u0435\u043c \u0441\u0442\u0430\u0440\u043e\u0433\u043e \u043f\u0430\u0442\u0442\u0435\u0440\u043d\u0430 \u043c\u043e\u0434\u0443\u043b\u0435\u0439, \u0447\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043d\u043e\u0432\u044b\u0445 \u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432 \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u0431\u044b\u0441\u0442\u0440\u0435\u0435 \u0438 \u043b\u0435\u0433\u0447\u0435. \u041e\u0434\u043d\u0430\u043a\u043e \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435 \u044f \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0438\u043b, \u0447\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u0442\u0440\u0435\u0431\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043a\u0430\u043a\u043e\u0435-\u0442\u043e \u0432\u0440\u0435\u043c\u044f, \u0447\u0442\u043e\u0431\u044b \u043e\u0441\u0432\u043e\u0438\u0442\u044c \u044d\u0442\u0443 \u0438\u0434\u0438\u043e\u043c\u0443.<\/p>\n<p>  \u041d\u0438\u0436\u0435 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u0430\u043d\u043d\u043e\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0444\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0439 \u0432\u0435\u0440\u0441\u0438\u0438 \u043c\u043e\u0435\u0433\u043e \u0442\u0435\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u043a\u043e\u0434\u0430, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u044f \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u044e \u0440\u044f\u0434 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f. \u0411\u043e\u043b\u044c\u0448\u043e\u0435 \u0441\u043f\u0430\u0441\u0438\u0431\u043e \u0410\u0434\u0430\u043c\u0443 \u0424\u0440\u0435\u0439\u0437\u0435\u0440\u0443 \u0437\u0430 \u043f\u043e\u043c\u043e\u0449\u044c \u0432 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u0438 \u043e\u0431\u043b\u0430\u0433\u043e\u0440\u0430\u0436\u0438\u0432\u0430\u043d\u0438\u0438 \u043c\u043e\u0435\u0439 \u0440\u0430\u0431\u043e\u0442\u044b. \u0421\u0435\u0440\u0432\u0438\u0441\u044b \u043f\u0440\u0435\u0434\u043d\u0430\u043c\u0435\u0440\u0435\u043d\u043d\u043e \u0443\u043f\u0440\u043e\u0449\u0435\u043d\u044b, \u0442\u0430\u043a \u0447\u0442\u043e, \u043d\u0430\u0434\u0435\u044e\u0441\u044c, \u043e\u043d\u0438 \u0431\u0443\u0434\u0443\u0442 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u043e\u043d\u044f\u0442\u043d\u044b \u0434\u043b\u044f \u0431\u044b\u0441\u0442\u0440\u043e\u0433\u043e \u0447\u0442\u0435\u043d\u0438\u044f.<\/p>\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-306368","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/306368","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=306368"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/306368\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=306368"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=306368"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=306368"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}