Skip to content

Commit

Permalink
deploy: d1b0830
Browse files Browse the repository at this point in the history
  • Loading branch information
danicheg committed Sep 26, 2024
1 parent 7e30cd4 commit 390aa07
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 39 deletions.
8 changes: 4 additions & 4 deletions datatypes/chain.html
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ <h1 id="chain" class="title">Chain</h1>
<p>API Documentation: <a class="api" href="https://www.javadoc.io/doc/org.typelevel/cats-docs_2.13/2.12.0/cats/data/Chain.html">Chain</a></p>
<p><code>Chain</code> is an immutable sequence data structure that allows constant time prepending, appending and concatenation.
This makes it especially efficient when used as a <a href="../typeclasses/monoid.html#monoid">Monoid</a>, e.g. with <a href="validated.html#validated">Validated</a> or <a href="writer.html#writer">Writer</a>.
As such it aims to be used where <a class="api" href="https://www.scala-lang.org/api/2.13.14/scala/collection/immutable/List.html">List</a> and <a class="api" href="https://www.scala-lang.org/api/2.13.14/scala/collection/immutable/Vector.html">Vector</a> incur a performance penalty.
As such it aims to be used where <a class="api" href="https://www.scala-lang.org/api/2.13.15/scala/collection/immutable/List.html">List</a> and <a class="api" href="https://www.scala-lang.org/api/2.13.15/scala/collection/immutable/Vector.html">Vector</a> incur a performance penalty.
Cats also includes type class implementations to support using <code>Chain</code> as a general-purpose collection type, including <a href="../typeclasses/traverse.html#traverse">Traverse</a>, <a href="../typeclasses/monad.html#monad">Monad</a>, and <a href="../typeclasses/alternative.html#alternative">Alternative</a>.</p>

<h2 id="motivation" class="section"><a class="anchor-link left" href="#motivation"><i class="icofont-laika link">&#xef71;</i></a>Motivation</h2>
Expand All @@ -238,7 +238,7 @@ <h2 id="motivation" class="section"><a class="anchor-link left" href="#motivatio
That is because both of these traversals make use of the <code>List</code> monoid (or the <a href="nel.html#nonemptylist">NonEmptyList</a> semigroup), which by the nature of <code>List</code> is very inefficient.
If you use <a href="../typeclasses/traverse.html#traverse">traverse</a> with a data structure with <code>n</code> elements and <a href="writer.html#writer">Writer</a> or <a href="validated.html#validated">Validated</a> as the <a href="../typeclasses/applicative.html#applicative">Applicative</a> type, you will end up with a runtime of <code>O(n^2)</code>.
This is because, with <code>List</code>, appending a single element requires iterating over the entire data structure and therefore takes linear time.</p>
<p>So <a class="api" href="https://www.scala-lang.org/api/2.13.14/scala/collection/immutable/List.html">List</a> isn&#39;t all that great for this use case, so let&#39;s use <a class="api" href="https://www.scala-lang.org/api/2.13.14/scala/collection/immutable/Vector.html">Vector</a> or <a class="api" href="https://www.javadoc.io/doc/org.typelevel/cats-docs_2.13/2.12.0/cats/data/NonEmptyVector.html">NonEmptyVector</a>` instead, right?</p>
<p>So <a class="api" href="https://www.scala-lang.org/api/2.13.15/scala/collection/immutable/List.html">List</a> isn&#39;t all that great for this use case, so let&#39;s use <a class="api" href="https://www.scala-lang.org/api/2.13.15/scala/collection/immutable/Vector.html">Vector</a> or <a class="api" href="https://www.javadoc.io/doc/org.typelevel/cats-docs_2.13/2.12.0/cats/data/NonEmptyVector.html">NonEmptyVector</a>` instead, right?</p>
<p>Well, <code>Vector</code> has its own problems and in this case it&#39;s unfortunately not that much faster than <code>List</code> at all. You can check <a href="http://www.lihaoyi.com/post/BenchmarkingScalaCollections.html#vectors-are-ok">this blog post</a> by Li Haoyi for some deeper insight into <code>Vector</code>&#39;s issues.</p>
<p><code>Chain</code> evolved from what used to be <code>fs2.Catenable</code> and Erik Osheim&#39;s <a href="https://github.com/non/chain">Chain</a> library.
Similar to <code>List</code>, it is also a very simple data structure, but unlike <code>List</code> it supports constant O(1) time <code>append</code>, <code>prepend</code> and <code>concat</code>.
Expand All @@ -265,7 +265,7 @@ <h2 id="nonemptychain" class="section"><a class="anchor-link left" href="#nonemp
</span><span>
</span><span class="type-name">NonEmptyChain</span><span>.</span><span class="identifier">one</span><span>(</span><span class="number-literal">1</span><span>)
</span><span class="comment">// res3: NonEmptyChain[Int] = Singleton(a = 1)</span></code></pre>
<p>You can also create an <a class="api" href="https://www.scala-lang.org/api/2.13.14/scala/Option.html">Option</a> of <code>NonEmptyChain</code> from a <code>Chain</code> or any other collection type:</p>
<p>You can also create an <a class="api" href="https://www.scala-lang.org/api/2.13.15/scala/Option.html">Option</a> of <code>NonEmptyChain</code> from a <code>Chain</code> or any other collection type:</p>
<pre class="keep-together pdf epub"><code class="nohighlight"><span class="keyword">import</span><span> </span><span class="identifier">cats</span><span>.</span><span class="identifier">data</span><span>.</span><span class="identifier">_</span><span>

</span><span class="type-name">NonEmptyChain</span><span>.</span><span class="identifier">fromChain</span><span>(</span><span class="type-name">Chain</span><span>(</span><span class="number-literal">1</span><span>, </span><span class="number-literal">2</span><span>, </span><span class="number-literal">3</span><span>))
Expand Down Expand Up @@ -294,7 +294,7 @@ <h2 id="nonemptychain" class="section"><a class="anchor-link left" href="#nonemp

<h2 id="how-it-works" class="section"><a class="anchor-link left" href="#how-it-works"><i class="icofont-laika link">&#xef71;</i></a>How it works</h2>
<p><code>Chain</code> is implemented as a simple unbalanced binary tree ADT with four cases:
an empty <code>Chain</code> with no elements, a singleton <code>Chain</code> with exactly one element, a concatenation of two chains, or a wrapper for a <a class="api" href="https://www.scala-lang.org/api/2.13.14/scala/collection/immutable/Seq.html">Seq</a>.</p>
an empty <code>Chain</code> with no elements, a singleton <code>Chain</code> with exactly one element, a concatenation of two chains, or a wrapper for a <a class="api" href="https://www.scala-lang.org/api/2.13.15/scala/collection/immutable/Seq.html">Seq</a>.</p>
<p>In code it looks like this:</p>
<pre class="keep-together pdf epub"><code class="nohighlight"><span class="keyword">sealed</span><span> </span><span class="keyword">abstract</span><span> </span><span class="keyword">class</span><span> </span><span class="type-name">Chain</span><span>[+</span><span class="type-name">A</span><span>]

Expand Down
12 changes: 6 additions & 6 deletions datatypes/contt.html
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ <h1 id="contt" class="title">ContT</h1>
</span><span class="type-name">Succeeded</span><span>(</span><span class="identifier">user</span><span>.</span><span class="identifier">id</span><span>)
}
}
</span><span class="comment">// eval: Eval[UserUpdateResult] = cats.Later@7cb3b8c6</span></code></pre>
</span><span class="comment">// eval: Eval[UserUpdateResult] = cats.Later@37f9fc82</span></code></pre>
<p>Finally we can run the resulting <code>Eval</code> to actually execute the computation:</p>
<pre class="keep-together pdf epub"><code class="nohighlight"><span class="identifier">eval</span><span>.</span><span class="identifier">value</span><span>
</span><span class="comment">// Persisting updated user to the DB: User(100,Bob,150)
Expand All @@ -308,7 +308,7 @@ <h2 id="composition" class="section"><a class="anchor-link left" href="#composit
}
</span><span class="comment">// anotherComputation: ContT[Eval, UserUpdateResult, Map[String, String]] = FromFn(
// runAndThen = Single(
// f = cats.data.ContT$$Lambda$11729/0x00007fea8bfe5a60@5c1c43e3,
// f = cats.data.ContT$$Lambda$11795/0x00007f12bbff4168@7de4c77e,
// index = 0
// )
// )
Expand All @@ -319,7 +319,7 @@ <h2 id="composition" class="section"><a class="anchor-link left" href="#composit
</span><span class="type-name">Succeeded</span><span>(</span><span class="identifier">userFields</span><span>(</span><span class="string-literal">&quot;id&quot;</span><span>).</span><span class="identifier">toInt</span><span>)
}
}
</span><span class="comment">// anotherEval: Eval[UserUpdateResult] = cats.Eval$$anon$5@447e5453
</span><span class="comment">// anotherEval: Eval[UserUpdateResult] = cats.Eval$$anon$5@1ab22e3a
</span><span>
</span><span class="identifier">anotherEval</span><span>.</span><span class="identifier">value</span><span>
</span><span class="comment">// Persisting these fields to the DB: Map(id -&gt; 100, name -&gt; Bob, age -&gt; 150)
Expand All @@ -336,7 +336,7 @@ <h2 id="composition" class="section"><a class="anchor-link left" href="#composit
}
</span><span class="comment">// updateUserModel: ContT[Eval, UserUpdateResult, User] = FromFn(
// runAndThen = Single(
// f = cats.data.ContT$$Lambda$11729/0x00007fea8bfe5a60@33d5c760,
// f = cats.data.ContT$$Lambda$11795/0x00007f12bbff4168@141cb51c,
// index = 0
// )
// )
Expand Down Expand Up @@ -370,7 +370,7 @@ <h2 id="composition" class="section"><a class="anchor-link left" href="#composit
</span><span class="identifier">updateUserModel</span><span> </span><span class="identifier">flatMap</span><span> </span><span class="identifier">persistToDb</span><span> </span><span class="identifier">flatMap</span><span> </span><span class="identifier">publishEvent</span><span>
</span><span class="comment">// chainOfContinuations: ContT[Eval, UserUpdateResult, UserUpdateResult] = FromFn(
// runAndThen = Single(
// f = cats.data.ContT$$Lambda$11733/0x00007fea8bfe63e8@2c435e49,
// f = cats.data.ContT$$Lambda$11799/0x00007f12bbff4af0@3c9a479e,
// index = 0
// )
// )
Expand All @@ -381,7 +381,7 @@ <h2 id="composition" class="section"><a class="anchor-link left" href="#composit
</span><span class="identifier">finalResult</span><span>
}
}
</span><span class="comment">// eval: Eval[UserUpdateResult] = cats.Eval$$anon$5@3a9f357e
</span><span class="comment">// eval: Eval[UserUpdateResult] = cats.Eval$$anon$5@14c7a16a
</span><span>
</span><span class="identifier">eval</span><span>.</span><span class="identifier">value</span><span>
</span><span class="comment">// Updated user model
Expand Down
4 changes: 2 additions & 2 deletions datatypes/eval.html
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ <h4 id="eval-later" class="section"><a class="anchor-link left" href="#eval-late
</span><span class="identifier">println</span><span>(</span><span class="string-literal">&quot;Running expensive calculation...&quot;</span><span>)
</span><span class="number-literal">1</span><span> + </span><span class="number-literal">2</span><span> * </span><span class="number-literal">3</span><span>
}
</span><span class="comment">// lazyEval: Eval[Int] = cats.Later@504049c6
</span><span class="comment">// lazyEval: Eval[Int] = cats.Later@45f10304
</span><span>
</span><span class="identifier">lazyEval</span><span>.</span><span class="identifier">value</span><span>
</span><span class="comment">// Running expensive calculation...
Expand All @@ -276,7 +276,7 @@ <h4 id="eval-always" class="section"><a class="anchor-link left" href="#eval-alw
</span><span class="identifier">println</span><span>(</span><span class="string-literal">&quot;Running expensive calculation...&quot;</span><span>)
</span><span class="number-literal">1</span><span> + </span><span class="number-literal">2</span><span> * </span><span class="number-literal">3</span><span>
}
</span><span class="comment">// always: Eval[Int] = cats.Always@241dfd26
</span><span class="comment">// always: Eval[Int] = cats.Always@313991b9
</span><span>
</span><span class="identifier">always</span><span>.</span><span class="identifier">value</span><span>
</span><span class="comment">// Running expensive calculation...
Expand Down
2 changes: 1 addition & 1 deletion datatypes/freeapplicative.html
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ <h2 id="example" class="section"><a class="anchor-link left" href="#example"><i
}
}</span></code></pre>
<pre class="keep-together pdf epub"><code class="nohighlight"><span class="keyword">val</span><span> </span><span class="identifier">validator</span><span> = </span><span class="identifier">prog</span><span>.</span><span class="identifier">foldMap</span><span>[</span><span class="type-name">FromString</span><span>](</span><span class="identifier">compiler</span><span>)
</span><span class="comment">// validator: FromString[Boolean] = cats.instances.Function1Instances$$anon$7$$Lambda$11891/0x00007fea8bd4f958@474dc89
</span><span class="comment">// validator: FromString[Boolean] = cats.instances.Function1Instances$$anon$7$$Lambda$11957/0x00007f12bb7b3648@182071b
</span><span class="identifier">validator</span><span>(</span><span class="string-literal">&quot;1234&quot;</span><span>)
</span><span class="comment">// res0: Boolean = false
</span><span class="identifier">validator</span><span>(</span><span class="string-literal">&quot;12345&quot;</span><span>)
Expand Down
4 changes: 2 additions & 2 deletions datatypes/freemonad.html
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ <h2 id="freet" class="section"><a class="anchor-link left" href="#freet"><i clas
</span><span class="keyword">import</span><span> </span><span class="type-name">TeletypeOps</span><span>.</span><span class="identifier">_</span><span>

</span><span class="keyword">val</span><span> </span><span class="identifier">state</span><span> = </span><span class="identifier">program</span><span>.</span><span class="identifier">foldMap</span><span>(</span><span class="identifier">interpreter</span><span>)
</span><span class="comment">// state: TeletypeState[Unit] = cats.data.IndexedStateT@17bc7a6
</span><span class="comment">// state: TeletypeState[Unit] = cats.data.IndexedStateT@373c878c
</span><span class="keyword">val</span><span> </span><span class="identifier">initialState</span><span> = </span><span class="type-name">Nil</span><span>
</span><span class="comment">// initialState: Nil.type = List()
</span><span class="keyword">val</span><span> (</span><span class="identifier">stored</span><span>, </span><span class="identifier">_</span><span>) = </span><span class="identifier">state</span><span>.</span><span class="identifier">run</span><span>(</span><span class="identifier">initialState</span><span>).</span><span class="identifier">value</span><span>
Expand Down Expand Up @@ -789,7 +789,7 @@ <h2 id="freet" class="section"><a class="anchor-link left" href="#freet"><i clas
// value = Success(value = Some(value = Left(value = Action(value = 7))))
// )
// ),
// f0 = scala.Function1$$Lambda$11730/0x00007fea8bf865a8@172692a4
// f0 = scala.Function1$$Lambda$11796/0x00007f12bbf93060@27d4664
// )
</span><span class="keyword">val</span><span> </span><span class="identifier">evaluated</span><span> = </span><span class="identifier">hoisted</span><span>.</span><span class="identifier">foldMap</span><span>(</span><span class="identifier">tryInterpreter</span><span>)
</span><span class="comment">// evaluated: OptTry[Int] = OptionT(value = Success(value = Some(value = 12)))
Expand Down
4 changes: 2 additions & 2 deletions datatypes/state.html
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ <h2 id="changing-states" class="section"><a class="anchor-link left" href="#chan
</span><span class="identifier">_</span><span> &lt;- </span><span class="identifier">close</span><span>
</span><span class="identifier">_</span><span> &lt;- </span><span class="identifier">open</span><span>
} </span><span class="keyword">yield</span><span> ()
</span><span class="comment">// valid: IndexedStateT[Eval, Closed.type, Open.type, Unit] = cats.data.IndexedStateT@33ebe944</span></code></pre>
</span><span class="comment">// valid: IndexedStateT[Eval, Closed.type, Open.type, Unit] = cats.data.IndexedStateT@717b6e6</span></code></pre>
<p>Note that the inferred type of <code>valid</code> correctly models that this computation can be executed only with an initial <code>Closed</code> state.</p>
<pre class="keep-together pdf epub"><code class="nohighlight"><span class="identifier">valid</span><span>.</span><span class="identifier">run</span><span>(</span><span class="type-name">Open</span><span>)
</span><span class="comment">// error: type mismatch;
Expand All @@ -483,7 +483,7 @@ <h2 id="changing-states" class="section"><a class="anchor-link left" href="#chan
// valid.run(Open)
// ^^^^</span></code></pre>
<pre class="keep-together pdf epub"><code class="nohighlight"><span class="identifier">valid</span><span>.</span><span class="identifier">run</span><span>(</span><span class="type-name">Closed</span><span>)
</span><span class="comment">// res6: Eval[(Open.type, Unit)] = cats.Eval$$anon$1@4fed8034</span></code></pre>
</span><span class="comment">// res6: Eval[(Open.type, Unit)] = cats.Eval$$anon$1@25b7b00a</span></code></pre>


<hr class="footer-rule"/>
Expand Down
4 changes: 2 additions & 2 deletions typeclasses/bifoldable.html
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ <h2 id="tuple-as-bifoldable" class="section"><a class="anchor-link left" href="#
(</span><span class="identifier">s</span><span>, </span><span class="identifier">acc</span><span>) =&gt; </span><span class="identifier">acc</span><span>.</span><span class="identifier">map</span><span>(</span><span class="identifier">_</span><span> |+| </span><span class="identifier">s</span><span>),
(</span><span class="identifier">s</span><span>, </span><span class="identifier">acc</span><span>) =&gt; </span><span class="identifier">acc</span><span>.</span><span class="identifier">map</span><span>(</span><span class="identifier">_</span><span> |+| </span><span class="identifier">s</span><span>)
)
</span><span class="comment">// right: Eval[String] = cats.Eval$$anon$1@51a80682
</span><span class="comment">// right: Eval[String] = cats.Eval$$anon$1@159db2c6
</span><span>
</span><span class="identifier">left</span><span> === </span><span class="identifier">expected</span><span>
</span><span class="comment">// res2: Boolean = true
Expand All @@ -354,7 +354,7 @@ <h2 id="tuple-as-bifoldable" class="section"><a class="anchor-link left" href="#
(</span><span class="identifier">s</span><span>, </span><span class="identifier">acc</span><span>) =&gt; </span><span class="identifier">acc</span><span>.</span><span class="identifier">map</span><span>(</span><span class="identifier">_</span><span> |+| </span><span class="identifier">s</span><span>),
(</span><span class="identifier">s</span><span>, </span><span class="identifier">acc</span><span>) =&gt; </span><span class="identifier">acc</span><span>.</span><span class="identifier">map</span><span>(</span><span class="identifier">_</span><span> |+| </span><span class="identifier">s</span><span>)
)
</span><span class="comment">// reversedRight: Eval[String] = cats.Eval$$anon$1@440c495a
</span><span class="comment">// reversedRight: Eval[String] = cats.Eval$$anon$1@50702bcd
</span><span>
</span><span class="identifier">reversedRight</span><span>.</span><span class="identifier">value</span><span> === </span><span class="identifier">expected</span><span>
</span><span class="comment">// res4: Boolean = false</span></code></pre>
Expand Down
Loading

0 comments on commit 390aa07

Please sign in to comment.