Commits

John Kodumal committed 0dabeca

More progress on monad transformers, reader

Comments (0)

Files changed (2)

Added
New image
   	<div style="font-size: 50%">John Kodumal, Atlassian</div>
   	<div style="font-size: 50%">jkodumal@atlassian</div>
 	<div style="font-size: 50%"><br/></div>
+	<div style="font-size: 25%">Use left and right arrows to navigate.</div>
 	<div style="font-size: 25%">Japanese translation by Shingo Omura is <a href="index-ja.html">here<a>.</div>
   </h1>
 </section>
 		<li class="slide">These examples are based on <a href="https://github.com/scalaz/scalaz/">scalaz 7.0.0</a></li>
 		<li class="slide">This is how the hierarchy is defined in Haskell--- it isn't perfect</li>
 	</ul>
-	<p class="slide">We'll cover functors, applicative functors, and monads</p>
+	<p class="slide">We'll cover functors, applicative functors, monads (and monad transformers), foldable, and traversable</p>
 	<p class="slide" style="font-size: 60%">*one exception: <code>for</code> comprehensions and monads</p>
 </section>
 
 </pre>
 	<h6 class="slide">Exercise: Define the applicative instance for <code>Option</code></h6>
 	</div>
+	<div class="slide">
+	<p>With <code>point</code>, we can express the above example as:</p>
+<pre class="scala">
+	(parse("3") |@| 4.point[Option]) (_ + _)
+</pre>
+	</div>
 </section>
 
 <!--
 </section>
 
 <section class="slide">
+	<h2>One more motivating example</h2>
+	<div class="slide">
+	<p>Recall that there is a functor instance for functions:</p>
+	<pre>
+def map[A, B](fa: R => A)(f: A => B) : R => B = (x => f(fa(x)))
+	</pre>
+	</div>
+	<div class="slide">
+		<p>What about a monad instance?</p>	
+		<pre>
+implicit def FunctionMonad[R] = new Monad[({type l[a] = R=>a})#l] {
+	def flatMap[A, B](fa: R => A)(f: A => R => B) : R => B = (x => f(fa(x))(x))
+	...
+}
+		</pre>
+	</div>
+	<div class="slide">
+		<p>This is commonly referred to as the <code>Reader</code> monad. Here's why:</p>
+		<pre>
+val f = for {
+	t1 <- ((_:Int) + 2)
+  t2 <- ((_:Int) - 1)
+} yield t1 * t2
+		</pre>
+	<p>Think of <code>Reader</code> as a computation awaiting a <em>read-only environment</em> (the argument) to be supplied.</p>
+	</div>	
+</section>
+
+<section class="slide">
 	<h2>Monad as Embedded Language</h2>
 	<div class="slide">
-		<p>I'll present one <a href="http://en.wikibooks.org/wiki/Haskell/Understanding_monads">stab</a> at providing an intuition for monads. Consider:</p>
+		<p>Here's a <a href="http://en.wikibooks.org/wiki/Haskell/Understanding_monads">stab</a> at providing an intuition for monads. Consider:</p>
 <pre class="scala">
 for {
 	x <- monadicX
 			</tr>
 			<tr class="slide">
 				<td><code>Reader</code></td>
-				<td>Global environment</td>
+				<td>Read-only environment</td>
 			</tr>
 			<tr class="slide">
 				<td><code>Validation</code></td>
 </section>
 
 <section class="slide">
-	<h2>One more motivating example</h2>
-	<p>Recall that there is a functor instance for functions:</p>
-	<pre>
-	implicit def FunctionFunctor[R] = new Functor[({type l[a] = R=>a})#l] {
-		def map[A, B](fa: R => A)(f: A => B) : R => B = (x => f(fa(x)))
-	}
-	</pre>
+	<h2>Languages with Multiple Effects</h2>
+	<p>In the real world, we have to deal with multiple monads (effects):</p>
+	<ul>
+		<li class="slide">A data store takes a DB configuration (<code>Reader</code>), and the connection may fail (<code>Validation</code>)</li>
+		<li class="slide">A computation may be asynchronous (<code>Future</code>), and may produce multiple results (<code>List</code>)</li>
+		<li class="slide">A remote call may fail if the service is down (<code>Validation</code>), and we want to log responses (<code>Writer</code>)</li>
+	</ul>
 	<div class="slide">
-		<p>What about a monad instance?</p>	
-		<pre>
-		implicit def FunctionMonad[R] = new Monad[({type l[a] = R=>a})#l] {
-			def flatMap[A, B](fa: R => A)(f: A => R => B) : R => B = (x => f(fa(x))(x))
-			...
-		}
-		</pre>
+	<p>Let's see why working with stacked monads gets ugly. We'll add a read-only environment to our calculator:</p>
+<pre class="scala">
+def lookup(s: String)(env: Map[String, Int]) = env.get(s) orElse parse(s)
+</pre>
 	</div>
 	<div class="slide">
-		<p>This is commonly referred to as the Reader monad. Here's why:</p>
-		<pre>
-			
-		</pre>
-	</div>	
+		<p>Now we'll add two numbers, using <code>Reader</code> to avoid repeating the <code>env</code> argument:</p>		
+<pre class="scala">
+for {
+ xOpt <- lookup("3") _
+ yOpt <- lookup("y") _
+} yield (for {
+          x <- xOpt
+          y <- yOpt
+         } yield x + y)
+</pre>
+	<p style="font-size: 60%">(Yuck)</p>
+	</div>
+</section>
+
+<section class="slide">
+	<img src="images/turtle.jpg" style="float: right; width: 100%;">
+	<h3>...all the way down</h3>
 </section>
 
 </body>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.