Commits

pchiusano  committed f1012c2

added link to presentation slides

  • Participants
  • Parent commits 5c2c1c9

Comments (0)

Files changed (3)

 <li>F0 (<a href="https://bitbucket.org/pchiusano/f0">source</a>, <a href="docs/f0/api/index.html">api</a>)</li>
 </ul>
 <p>I also contribute to the <a href="https://github.com/scalaz/scalaz">Scalaz project</a>.</p>
+<h3 id="presentations">Presentations</h3>
+<ul>
+<li>How to write a functional program with IO, mutation, and other effects (<a href="presentations/nescala-2012.html">slides</a>), at NEScala, 2012</li>
+</ul>

File index.markdown

 * F0 ([source](https://bitbucket.org/pchiusano/f0), [api](docs/f0/api/index.html))
 
 I also contribute to the [Scalaz project](https://github.com/scalaz/scalaz). 
+
+### Presentations ###
+
+* How to write a functional program with IO, mutation, and other effects ([slides](presentations/nescala-2012.html)), at NEScala, 2012 

File presentations/nescala-2012.html

+<?xml version="1.0" encoding="utf-8"?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
+<head>
+  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+  <meta http-equiv="Content-Style-Type" content="text/css" />
+  <meta name="generator" content="pandoc" />
+  <meta name="author" content="Paul Chiusano, Capital IQ (capitaliq.com)" />
+  <title>If a tree falls in a forest and no one is around to hear it, does it make a sound? Or, how to write a functional program with IO, mutation, and other effects</title>
+  <style type="text/css">
+table.sourceCode, tr.sourceCode, td.lineNumbers, td.sourceCode {
+  margin: 0; padding: 0; vertical-align: baseline; border: none; }
+table.sourceCode { width: 100%; }
+td.lineNumbers { text-align: right; padding-right: 4px; padding-left: 4px; color: #aaaaaa; border-right: 1px solid #aaaaaa; }
+td.sourceCode { padding-left: 5px; }
+code > span.kw { color: #007020; font-weight: bold; }
+code > span.dt { color: #902000; }
+code > span.dv { color: #40a070; }
+code > span.bn { color: #40a070; }
+code > span.fl { color: #40a070; }
+code > span.ch { color: #4070a0; }
+code > span.st { color: #4070a0; }
+code > span.co { color: #60a0b0; font-style: italic; }
+code > span.ot { color: #007020; }
+code > span.al { color: #ff0000; font-weight: bold; }
+code > span.fu { color: #06287e; }
+code > span.er { color: #ff0000; font-weight: bold; }
+  </style>
+  <link rel="stylesheet" type="text/css" media="screen, projection, print"
+    href="http://www.w3.org/Talks/Tools/Slidy2/styles/slidy.css" />
+  <script src="http://www.w3.org/Talks/Tools/Slidy2/scripts/slidy.js.gz"
+    charset="utf-8" type="text/javascript"></script>
+</head>
+<body>
+<div class="slide titlepage">
+  <h1 class="title">If a tree falls in a forest and no one is around to hear it, does it make a sound? Or, how to write a functional program with IO, mutation, and other effects</h1>
+  <p class="author">
+Paul Chiusano, Capital IQ (capitaliq.com)
+  </p>
+  <p class="date">@pchiusano, pchiusano.blogspot.com</p>
+</div>
+<div class="section slide level1" id="claim">
+<h1 id="claim">Claim</h1>
+<ul>
+<li>Functional programming has the most powerful imperative programming tools of all</li>
+</ul>
+</div>
+<div class="section slide level1" id="a-simple-example">
+<h1 id="a-simple-example">A simple example</h1>
+<pre class="sourceCode scala"><code class="sourceCode scala"><span class="kw">case</span> <span class="kw">class</span> <span class="fu">Player</span>(name: String, score: Int)
+
+<span class="kw">def</span> <span class="fu">declareWinner</span>(p: Player): Unit = 
+  <span class="fu">println</span>(p.<span class="fu">name</span> + <span class="st">&quot; is the winner!&quot;</span>)
+
+<span class="kw">def</span> <span class="fu">winner</span>(p1: Player, p2: Player): Unit = 
+  <span class="kw">if</span> (p1.<span class="fu">score</span> &gt; p2.<span class="fu">score</span>) <span class="fu">declareWinner</span>(p1)
+  <span class="kw">else</span> <span class="fu">declareWinner</span>(p2)</code></pre>
+</div>
+<div class="section slide level1" id="extracting-a-pure-function-from-an-impure-function">
+<h1 id="extracting-a-pure-function-from-an-impure-function">Extracting a pure function from an impure function</h1>
+<pre class="sourceCode scala"><code class="sourceCode scala"><span class="kw">def</span> <span class="fu">declareWinner</span>(p: Player): Unit = 
+  <span class="fu">println</span>(p.<span class="fu">name</span> + <span class="st">&quot; is the winner!&quot;</span>)
+
+<span class="kw">def</span> <span class="fu">winner</span>(p1: Player, p2: Player): Player = 
+  <span class="kw">if</span> (p1.<span class="fu">score</span> &gt; p2.<span class="fu">score</span>) p1 
+  <span class="kw">else</span> p2
+
+<span class="kw">def</span> <span class="fu">computeAndDeclareWinner</span>(p1: Player, p2: Player) = { 
+  <span class="kw">val</span> p = <span class="fu">winner</span>(p1,p2)
+  <span class="fu">declareWinner</span>(p)
+}</code></pre>
+</div>
+<div class="section slide level1" id="inside-every-impure-function...">
+<h1 id="inside-every-impure-function...">Inside every impure function...</h1>
+<pre class="sourceCode scala"><code class="sourceCode scala"><span class="kw">def</span> <span class="fu">declareWinner</span>(p: Player): Unit = 
+  <span class="fu">println</span>(p.<span class="fu">name</span> + <span class="st">&quot; is the winner!&quot;</span>)
+
+<span class="kw">def</span> <span class="fu">winner</span>(p1: Player, p2: Player): Unit = 
+  <span class="kw">if</span> (p1.<span class="fu">score</span> &gt; p2.<span class="fu">score</span>) <span class="fu">declareWinner</span>(p1)
+  <span class="kw">else</span> <span class="fu">declareWinner</span>(p2)</code></pre>
+<ul>
+<li>... is a pure one waiting to get out</li>
+<li>Given impure <code>A =&gt; B</code>
+<ul>
+<li><code>A =&gt; D</code> (pure)</li>
+<li><code>D =&gt; B</code> (impure)</li>
+</ul></li>
+</ul>
+</div>
+<div class="section slide level1" id="and-inside-the-remaining-impure-function...">
+<h1 id="and-inside-the-remaining-impure-function...">And inside the remaining impure function...</h1>
+<pre class="sourceCode scala"><code class="sourceCode scala"><span class="kw">def</span> <span class="fu">declareWinner</span>(p: Player): Unit = 
+  <span class="fu">println</span>(<span class="fu">winnerMsg</span>(p))
+
+<span class="kw">def</span> <span class="fu">winnerMsg</span>(p: Player): String = 
+  p.<span class="fu">name</span> + <span class="st">&quot; is the winner!&quot;</span></code></pre>
+</div>
+<div class="section slide level1" id="turtles-all-the-way-down">
+<h1 id="turtles-all-the-way-down">Turtles all the way down</h1>
+<pre class="sourceCode scala"><code class="sourceCode scala"><span class="kw">trait</span> Action { 
+  <span class="kw">def</span> run: Unit
+}
+<span class="kw">def</span> <span class="fu">Println</span>(msg: String): Action = 
+  <span class="kw">new</span> Action { <span class="kw">def</span> run = <span class="fu">println</span>(msg) }
+
+<span class="kw">def</span> <span class="fu">declareWinner</span>(p: Player): Action = 
+  <span class="fu">Println</span>(<span class="fu">winnerMsg</span>(p))</code></pre>
+</div>
+<div class="section slide level1" id="what-makes-a-good-description">
+<h1 id="what-makes-a-good-description">What makes a good description?</h1>
+<pre class="sourceCode scala"><code class="sourceCode scala"><span class="kw">trait</span> Action { 
+  <span class="kw">def</span> run: Unit
+}
+<span class="kw">def</span> <span class="fu">Println</span>(msg: String): Action = 
+  <span class="kw">new</span> Action { <span class="kw">def</span> run = <span class="fu">println</span>(msg) }
+
+<span class="kw">def</span> <span class="fu">declareWinner</span>(p: Player): Action = 
+  <span class="fu">Println</span>(<span class="fu">winnerMsg</span>(p))</code></pre>
+</div>
+<div class="section slide level1" id="the-api-of-action">
+<h1 id="the-api-of-action">The API of <code>Action</code></h1>
+<pre class="sourceCode scala"><code class="sourceCode scala"><span class="kw">trait</span> Action { self =&gt; 
+  <span class="kw">def</span> run: Unit
+  <span class="kw">def</span> ++(a: Action) = <span class="kw">new</span> Action {
+    <span class="kw">def</span> run = { self.<span class="fu">run</span>; a.<span class="fu">run</span> }
+  }
+}</code></pre>
+</div>
+<div class="section slide level1" id="action-cannot-handle-input-effects">
+<h1 id="action-cannot-handle-input-effects"><code>Action</code> cannot handle input effects!</h1>
+<pre class="sourceCode scala"><code class="sourceCode scala"><span class="kw">def</span> <span class="fu">farenheitToCelsius</span>(f: Double): Double = 
+  (f - <span class="dv">32</span>) * <span class="fl">5.0</span>/<span class="fl">9.0</span>
+
+<span class="kw">def</span> convertToCelsius: Action = { 
+  <span class="fu">PrintLn</span>(<span class="st">&quot;Enter the temperature in degrees farenheit:&quot;</span>)
+  <span class="kw">val</span> f = <span class="fu">readLine</span>().<span class="fu">toDouble</span>  <span class="co">// !!!</span>
+  <span class="fu">PrintLn</span>(<span class="st">&quot;in celsius: &quot;</span> + <span class="fu">farenheitToCelsius</span>(f))
+}</code></pre>
+</div>
+<div class="section slide level1" id="action-version-2">
+<h1 id="action-version-2"><code>Action</code> (version 2)</h1>
+<pre class="sourceCode scala"><code class="sourceCode scala"><span class="kw">trait</span> Action[+A] { 
+  <span class="kw">def</span> run: A
+  <span class="kw">def</span> map[B](f: A =&gt; B): Action[B]
+  <span class="kw">def</span> flatMap[B](f: A =&gt; Action[B]): Action[B]
+}</code></pre>
+</div>
+<div class="section slide level1" id="action-version-2-1">
+<h1 id="action-version-2-1"><code>Action</code> (version 2)</h1>
+<pre class="sourceCode scala"><code class="sourceCode scala"><span class="kw">def</span> <span class="fu">PrintLn</span>(msg: String): Action[Unit]
+<span class="kw">def</span> ReadLn: Action[String]
+
+<span class="kw">def</span> convertToCelsius: Action[Unit] = <span class="kw">for</span> { 
+  _ &lt;- <span class="fu">Println</span>(<span class="st">&quot;Enter the temperature in degrees farenheit:&quot;</span>)
+  f &lt;- ReadLn.<span class="fu">map</span>(_.<span class="fu">toDouble</span>)
+  _ &lt;- <span class="fu">Println</span>(<span class="st">&quot;in celsius: &quot;</span> + <span class="fu">farenheitToCelsius</span>(f))
+} <span class="kw">yield</span> ()</code></pre>
+</div>
+<div class="section slide level1" id="action-version-3">
+<h1 id="action-version-3"><code>Action</code> (version 3)</h1>
+<pre class="sourceCode scala"><code class="sourceCode scala"><span class="kw">def</span> <span class="fu">PrintLn</span>(msg: String): Action[Unit]
+<span class="kw">def</span> ReadLn: Action[String]
+<span class="kw">def</span> <span class="fu">ReadLines</span>(file: String): Action[List[String]]
+<span class="kw">def</span> <span class="fu">WriteLines</span>(file: String, lines: List[String]): Action[Unit]
+
+<span class="kw">def</span> convertToCelsius: Action[Unit] = <span class="kw">for</span> { 
+  lines &lt;- <span class="fu">ReadLines</span>(<span class="st">&quot;farenheit.txt&quot;</span>)
+  _ &lt;- <span class="fu">WriteLines</span>(<span class="st">&quot;celsius.txt&quot;</span>, lines.<span class="fu">map</span>(farenheitToCelsius))
+} <span class="kw">yield</span> ()</code></pre>
+</div>
+<div class="section slide level1" id="action-version-3a">
+<h1 id="action-version-3a"><code>Action</code> (version 3a)</h1>
+<pre class="sourceCode scala"><code class="sourceCode scala"><span class="kw">trait</span> Source[+A] { 
+  <span class="kw">def</span> map[B](f: A =&gt; B): Source[B] = <span class="fu">MapSource</span>(<span class="kw">this</span>, f) 
+}
+<span class="kw">case</span> <span class="kw">class</span> <span class="fu">FileLines</span>(file: String) <span class="kw">extends</span> Source[String]
+<span class="kw">case</span> <span class="kw">class</span> Literal[+A](elements: List[A]) <span class="kw">extends</span> Source[A]
+<span class="kw">case</span> <span class="kw">class</span> MapSource[A,B](s: Source[A], f: A =&gt; B) <span class="kw">extends</span> Source[A]
+
+<span class="kw">def</span> <span class="fu">ReadLines</span>(file: String): Action[Source[String]]
+<span class="kw">def</span> <span class="fu">PrintLn</span>(msg: String): Action[Unit]
+<span class="kw">def</span> <span class="fu">WriteLines</span>(file: String, lines: Source[String]): Action[Unit]
+
+<span class="kw">def</span> convertToCelsius: Action[Unit] = <span class="kw">for</span> { 
+  lines &lt;- <span class="fu">ReadLines</span>(<span class="st">&quot;farenheit.txt&quot;</span>)
+  _ &lt;- <span class="fu">WriteLines</span>(<span class="st">&quot;celsius.txt&quot;</span>, lines.<span class="fu">map</span>(farenheitToCelsius))
+} <span class="kw">yield</span> ()</code></pre>
+</div>
+<div class="section slide level1" id="other-examples">
+<h1 id="other-examples">Other examples</h1>
+<ul>
+<li>Serializing objects to normalized database schemas</li>
+<li>Separating layout description from rendering</li>
+<li>Separating the query language from the side-effect of retrieving the requested data</li>
+</ul>
+</div>
+<div class="section slide level1" id="takeaways">
+<h1 id="takeaways">Takeaways</h1>
+<ul>
+<li>Factor out effects to &quot;outer layers&quot;</li>
+<li>Discover high-level, composable descriptions</li>
+</ul>
+</div>
+<div class="section slide level1" id="want-to-learn-more">
+<h1 id="want-to-learn-more">Want to learn more?</h1>
+<ul>
+<li>&quot;Functional programming in Scala&quot;, being published by Manning, coming out later this year</li>
+<li>Part 4 of book discusses these issues in much more detail</li>
+<li>Manning Early Access Program signup: http://manning.com/bjarnason/</li>
+</ul>
+</div>
+<div class="section slide level1" id="appendix-about-the-title">
+<h1 id="appendix-about-the-title">Appendix: about the title</h1>
+<pre class="sourceCode scala"><code class="sourceCode scala"><span class="kw">trait</span> Action { 
+  <span class="kw">def</span> run: Unit = ... 
+}
+<span class="kw">case</span> <span class="kw">class</span> <span class="fu">Println</span>(msg: String) <span class="kw">extends</span> Action
+
+<span class="kw">def</span> <span class="fu">main</span>(args: Array[String]): Unit = {
+  <span class="kw">val</span> a = <span class="fu">Println</span>(<span class="st">&quot;Hello world!&quot;</span>) 
+  a.<span class="fu">run</span>
+}</code></pre>
+<ul>
+<li>It's okay to perform a side-effect at the end of the universe!</li>
+</ul>
+</div>
+</body>
+</html>