Note that there are some explanatory texts on larger screens.

plurals
  1. POScalaz: request for use case for Cokleisli composition
    primarykey
    data
    text
    <p>This question isn't meant as flame-bait! As it might be apparent, I've been looking at <a href="http://github.com/scalaz/scalaz" rel="nofollow noreferrer">Scalaz</a> recently. I'm trying to understand <em>why</em> I need some of the functionality that the library provides. Here's something:</p> <pre><code>import scalaz._ import Scalaz._ type NEL[A] = NonEmptyList[A] val NEL = NonEmptyList </code></pre> <p>I put some println statements in my functions to see what was going on (<em>aside: what would I have done if I was trying to avoid side effects like that?</em>). My functions are:</p> <pre><code>val f: NEL[Int] =&gt; String = (l: NEL[Int]) =&gt; {println("f: " + l); l.toString |+| "X" } val g: NEL[String] =&gt; BigInt = (l: NEL[String]) =&gt; {println("g: " + l); BigInt(l.map(_.length).sum) } </code></pre> <p>Then I combine them via a <strong>cokleisli</strong> and pass in a <code>NEL[Int]</code></p> <pre><code>val k = cokleisli(f) =&gt;= cokleisli(g) println("RES: " + k( NEL(1, 2, 3) )) </code></pre> <p>What does this print?</p> <pre><code>f: NonEmptyList(1, 2, 3) f: NonEmptyList(2, 3) f: NonEmptyList(3) g: NonEmptyList(NonEmptyList(1, 2, 3)X, NonEmptyList(2, 3)X, NonEmptyList(3)X) RES: 57 </code></pre> <p>The RES value is the character count of the (String) elements in the final NEL. Two things occur to me:</p> <ol> <li>How could I have known that my NEL was going to be reduced in this manner from the method signatures involved? (I wasn't expecting the result <em>at all</em>)</li> <li>What is the point of this? Can a reasonably simple and easy-to-follow use case be distilled for me?</li> </ol> <p>This question is a thinly-veiled plea for some lovely person like <em><a href="https://stackoverflow.com/questions/2545248/function-syntax-puzzler-in-scalaz/2546400#2546400">retronym</a></em> to explain how this powerful library actually works.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload