<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0"><channel><atom:link rel="hub" href="http://tumblr.superfeedr.com/" xmlns:atom="http://www.w3.org/2005/Atom"/><description>Programming, etc</description><title>Stateless</title><generator>Tumblr (3.0; @alexleighton)</generator><link>http://alexleighton.tumblr.com/</link><item><title>Functors for the Beginner</title><description>&lt;p&gt;Let&amp;#8217;s talk about Functors in the OCaml programming language. I will assume that you know about modules in OCaml, if not you can refresh by reading the &lt;a title="OCaml Manual - The Module System" target="_blank" href="http://caml.inria.fr/pub/docs/manual-ocaml/manual004.html"&gt;manual&lt;/a&gt;.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Functors are &amp;#8220;functions&amp;#8221; from structures to structures.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The above is probably the best definition I’ve seen of a functor. It touches on what I see as the main motivation behind using a functor. A Functor being a function from structure to structure means that if you keep the signature of the input structure and the signature of the output structure constant, you are free to change the implementation details without affecting any of the modules that use it. Functors allow the software designer an easy way of avoiding duplication, increasing orthogonality, all in a type safe package.&lt;/p&gt;
&lt;p&gt;Let&amp;#8217;s write an algorithm to determine if a string contains balanced parentheses. To start, we&amp;#8217;ll define the type of stack we want to work with:&lt;/p&gt;
&lt;pre class="prettyprint lang-ml"&gt;module type STACK = sig
  type 'a t
  exception Empty
  val create : unit -&amp;gt; 'a t
  val push : 'a -&amp;gt; 'a t -&amp;gt; unit
  val pop : 'a t -&amp;gt; 'a
  val is_empty : 'a t -&amp;gt; bool
end&lt;/pre&gt;
&lt;p&gt;Here we have the basic operations on a very minimalist stack. Now, we&amp;#8217;ll create a functor that will take a stack structure of this type and return a module containing our parenthesis matching algorithm.&lt;/p&gt;
&lt;pre class="prettyprint lang-ml"&gt;module Matcher (S : STACK) = struct
  let is_balanced str =
    let s = S.create () in try
      String.iter
        (fun c -&amp;gt; match c with
            '(' -&amp;gt; S.push c s
          | ')' -&amp;gt; ignore (S.pop s)
          | _ -&amp;gt; ()) str;
        S.is_empty s
    with S.Empty -&amp;gt; false
end&lt;/pre&gt;
&lt;p&gt;While we rely on the Stack with the STACK type to not change the semantics of its functions, we can guarantee that for any stack with this kind of structure and with the right semantics, our algorithm will work. Any use of this functor will create a module with one function: &lt;code class="prettyprint lang-ml"&gt;val is_balanced : string -&amp;gt; bool&lt;/code&gt;. This also means that if we maintain the signature of the functor we just wrote, we can change any of the implementation details and not affect the outside world.&lt;/p&gt;
&lt;p&gt;To use a different stack, all we have to do is change one line of code from &lt;code class="prettyprint lang-ml"&gt;module M = Matcher(Stack)&lt;/code&gt; to &lt;code class="prettyprint lang-ml"&gt;module M = Matcher(CStack)&lt;/code&gt; to change our algorithm to use a stack that keeps a count of the number of things pushed onto it in its lifetime.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Conclusion:&lt;/strong&gt; functors allow you to write code that will work irrespective of the implementation of the given structure (so long as it does what it should), and produces a structure containing functions that we are free to modify, but only have to change in one location, that of the functor.&lt;/p&gt;</description><link>http://alexleighton.tumblr.com/post/1185209057</link><guid>http://alexleighton.tumblr.com/post/1185209057</guid><pubDate>Sat, 25 Sep 2010 08:01:00 -0700</pubDate><category>ocaml</category><category>functor</category><category>stack</category><category>programming</category></item></channel></rss>
