Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is a page on the excellent <a href="http://www.haskell.org/haskellwiki/Haskell" rel="noreferrer">Haskell wiki</a>, where <a href="http://www.haskell.org/haskellwiki/The_Monad.Reader/Issue3/Functional_Programming_vs_Object_Oriented_Programming#OO_and_FP_Styles_Contrasted" rel="noreferrer">differences in Functional Programming and OOP</a> are contrasted. The Haskell wiki is a wonderful resource <strong>for everything about functional programming in general</strong> in addition to helping with the Haskell language.</p> <h2>Functional programming and OOP Differences</h2> <p>The important difference between pure functional programming and object-oriented programming is:</p> <p>Object-oriented:</p> <blockquote> <p>Data:</p> <ul> <li>OOP asks <strong>What can I do with the data?</strong></li> <li>Producer: Class</li> <li>Consumer: Class method</li> </ul> <p>State:</p> <ul> <li>The methods and objects in OOP have some internal state (method variables and object attributes) and they possibly have side effects affecting the state of computer’s peripherals, the global scope, or the state of an object or method. Variable assignment is one good sign of something having a state.</li> </ul> </blockquote> <p>Functional:</p> <blockquote> <p>Data:</p> <ul> <li>Functional programming asks <strong>How the data is constructed?</strong></li> <li>Producer: Type Constructor</li> <li>Consumer: Function</li> </ul> <p>State:</p> <ul> <li>If a pure functional programming ever assigns to a variable, the variable must be considered and handled as immutable. <strong>There must not be a state in pure functional programming.</strong></li> <li>Code with side effects is often separated from the main purely functional body of code</li> <li>State can be passed around as an argument to a function, this is called a <a href="http://en.wikipedia.org/wiki/Continuation" rel="noreferrer">continuation</a>.</li> </ul> </blockquote> <h2>Functional substitutes for OOP generators</h2> <p>The way to do something similar to OOP style generators (which have an internal state) with pure functional programming is to approach the problem from a different point of view, by using one of these solutions depending on the use case:</p> <p><strong>1. Process some or all values in a sequence:</strong></p> <blockquote> <p>Type of sequence can be list, array, sequence or vector.</p> <ul> <li><p>Lisp has <code>car</code> and Haskell has <code>first</code>, which take first item from a list.</p></li> <li><p>Haskell also has <code>take</code>, which takes the first n items, and which supports lazy evaluation and thus infinite or cyclic sequences – like OOP generators do.</p></li> <li><p>Both have <code>first</code>, and different <code>map</code>, <code>reduce</code> or <code>fold</code> functions for processing sequences with a function.</p></li> <li><p>Matrices usually also have some ways to <code>map</code> or <code>apply</code> a function to each item.</p></li> </ul> </blockquote> <p><strong>2. Some values from a function are needed:</strong></p> <blockquote> <p>The indices might be from a discrete or continuous scale (integers or floats).</p> <ul> <li>Make one pure function to generate the indices (events) and feed those to another pure function (behaviour). This is called <a href="http://en.wikipedia.org/wiki/Functional_reactive_programming" rel="noreferrer">Functional reactive programming</a>. This is a form of <a href="http://en.wikipedia.org/wiki/Dataflow_programming" rel="noreferrer">Dataflow programming</a> along with cell-oriented programming. The <a href="http://en.wikipedia.org/wiki/Actor_model" rel="noreferrer">Actor model</a> is also somewhat similar in operation, and a very interesting alternative to threads with handling concurrency! </li> </ul> </blockquote> <p><strong>3. Use a closure to confine and encapsulate the state from the outside</strong></p> <blockquote> <ul> <li>This is the closest subsitute to OOP way with generators (which I think actually originated to imitate closures), and also farthest from pure functional programming, because a closure has a state.</li> </ul> </blockquote>
    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. 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.
 

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