Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are three different meanings, not much related!</p> <ul> <li><p>In Ocaml it is a parametrized module. See <a href="http://caml.inria.fr/pub/docs/manual-ocaml/manual004.html" rel="noreferrer">manual</a>. I think the best way to grok them is by example: (written quickly, might be buggy)</p> <pre><code>module type Order = sig type t val compare: t -&gt; t -&gt; bool end;; module Integers = struct type t = int let compare x y = x &gt; y end;; module ReverseOrder = functor (X: Order) -&gt; struct type t = X.t let compare x y = X.compare y x end;; (* We can order reversely *) module K = ReverseOrder (Integers);; Integers.compare 3 4;; (* this is false *) K.compare 3 4;; (* this is true *) module LexicographicOrder = functor (X: Order) -&gt; functor (Y: Order) -&gt; struct type t = X.t * Y.t let compare (a,b) (c,d) = if X.compare a c then true else if X.compare c a then false else Y.compare b d end;; (* compare lexicographically *) module X = LexicographicOrder (Integers) (Integers);; X.compare (2,3) (4,5);; module LinearSearch = functor (X: Order) -&gt; struct type t = X.t array let find x k = 0 (* some boring code *) end;; module BinarySearch = functor (X: Order) -&gt; struct type t = X.t array let find x k = 0 (* some boring code *) end;; (* linear search over arrays of integers *) module LS = LinearSearch (Integers);; LS.find [|1;2;3] 2;; (* binary search over arrays of pairs of integers, sorted lexicographically *) module BS = BinarySearch (LexicographicOrder (Integers) (Integers));; BS.find [|(2,3);(4,5)|] (2,3);; </code></pre></li> </ul> <p>You can now add quickly many possible orders, ways to form new orders, do a binary or linear search easily over them. Generic programming FTW.</p> <ul> <li><p>In functional programming languages like Haskell, it means some type constructors (parametrized types like lists, sets) that can be "mapped". To be precise, a functor <code>f</code> is equipped with <code>(a -&gt; b) -&gt; (f a -&gt; f b)</code>. This has origins in category theory. The Wikipedia article you linked to is this usage.</p> <pre><code>class Functor f where fmap :: (a -&gt; b) -&gt; (f a -&gt; f b) instance Functor [] where -- lists are a functor fmap = map instance Functor Maybe where -- Maybe is option in Haskell fmap f (Just x) = Just (f x) fmap f Nothing = Nothing fmap (+1) [2,3,4] -- this is [3,4,5] fmap (+1) (Just 5) -- this is Just 6 fmap (+1) Nothing -- this is Nothing </code></pre></li> </ul> <p>So, this is a special kind of a type constructors, and has little to do with functors in Ocaml!</p> <ul> <li>In imperative languages, it is a pointer to function.</li> </ul>
 

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