Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Just FYI:</p> <p>The proposed <a href="http://download.java.net/jdk8/docs/api/java/util/Optional.html">JDK8 Optional</a> class <strong>does satisfy</strong> the three <a href="http://learnyouahaskell.com/a-fistful-of-monads#monad-laws">Monad laws</a>. Here's a <a href="https://gist.github.com/ms-tg/7420496#file-jdk8_optional_monad_laws-java">gist demonstrating</a> that.</p> <p>All it takes be a Monad is to provide <strong>two</strong> functions which conform to <strong>three</strong> laws.</p> <p>The two functions:</p> <ol> <li><p>Place a <em>value into monadic context</em></p> <ul> <li>Haskell's Maybe: <code>return</code> / <code>Just</code></li> <li>Scala's Option: <code>Some</code></li> <li>Functional Java's Option: <code>Option.some</code></li> <li>JDK8's Optional: <code>Optional.of</code></li> </ul></li> <li><p>Apply a <em>function in monadic context</em></p> <ul> <li>Haskell's Maybe: <code>&gt;&gt;=</code> (aka <code>bind</code>)</li> <li>Scala's Option: <code>flatMap</code></li> <li>Functional Java's Option: <code>flatMap</code></li> <li>JDK8's Optional: <code>flatMap</code></li> </ul></li> </ol> <p>Please see the <a href="https://gist.github.com/ms-tg/7420496#file-jdk8_optional_monad_laws-java">above gist</a> for a java demonstration of the three laws. </p> <p>NOTE: One of the key things to understand is the signature of the <em>function to apply in monadic context</em>: it takes the raw value type, and returns the monadic type. </p> <p>In other words, if you have an instance of <code>Optional&lt;Integer&gt;</code>, the functions you can pass to its <code>flatMap</code> method will have the signature <code>(Integer) -&gt; Optional&lt;U&gt;</code>, where <code>U</code> is a value type which does not have to be <code>Integer</code>, for example <code>String</code>:</p> <pre><code>Optional&lt;Integer&gt; maybeInteger = Optional.of(1); // Function that takes Integer and returns Optional&lt;Integer&gt; Optional&lt;Integer&gt; maybePlusOne = maybeInteger.flatMap(n -&gt; Optional.of(n + 1)); // Function that takes Integer and returns Optional&lt;String&gt; Optional&lt;String&gt; maybeString = maybePlusOne.flatMap(n -&gt; Optional.of(n.toString)); </code></pre> <p>You don't need any sort of Monad Interface to code this way, or to think this way. In Scala, you don't code to a Monad Interface (unless you are using Scalaz library...). It appears that JDK8 will empower Java folks to use this style of <em>chained monadic computations</em> as well.</p> <p>Hope this is helpful!</p> <p><strong>Update:</strong> Blogged about this <a href="https://devblog.timgroup.com/2013/11/11/does-jdk8s-optional-class-satisfy-the-monad-laws-yes-it-does/">here</a>.</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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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