<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xml:base="http://dikini.net" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
 <title>dikini.net - shorts</title>
 <link>http://dikini.net/taxonomy/term/105/0</link>
 <description></description>
 <language>en</language>
<item>
 <title>Drupal, computer science, random bits</title>
 <link>http://dikini.net/13.06.2007/drupal_computer_science_random_bits</link>
 <description>&lt;p&gt;There are quite a few interesting computer science artefacts in Drupal. I&#039;m going to try to highlight a few of them. Sorry, this is unfinished, and might not be finished ever, but I&#039;m just trying to capture a snapshot of confused meandering thoughts.&lt;/p&gt;
&lt;h3&gt;Very late, dynamic binding&lt;/h3&gt;
&lt;p&gt;Late binding came to popular life with the advent of object oriented languages. Essentially it means binding of values, for example functions, to names at object creation, as opposed to compile or link time. In drupal, this can happen at any time, more even, it is algorithmic - that is you can change at runtime what is to be executed at a specific control point. The hook system is one of the ways to do it.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/13.06.2007/drupal_computer_science_random_bits&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/13.06.2007/drupal_computer_science_random_bits#comment</comments>
 <category domain="http://dikini.net/tags/drupal">drupal</category>
 <category domain="http://dikini.net/tags/programming">programming</category>
 <category domain="http://dikini.net/taxonomy/term/105">shorts</category>
 <pubDate>Wed, 13 Jun 2007 04:50:48 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">257 at http://dikini.net</guid>
</item>
<item>
 <title>Multi-page forms using form api</title>
 <link>http://dikini.net/22.03.2006/multi_page_forms_using_form_api</link>
 <description>&lt;blockquote&gt;&lt;p&gt;After months of work between hunmonk, myself, and chx, we&#039;ve finalized some substantial progress on multipart forms. A multipart form is a form that is spread across multiple pages (like a wizard), but doesn&#039;t truly &quot;submit&quot; until the final page.&lt;br /&gt;
Morbus Iff on the drupal development mailing list
&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;That is a cool functionality. While it shows the capability of the forms api to handle multi-page forms via the #pre_render function, which is good, the approach is only the beginning of multi-page form handling.&lt;/p&gt;
&lt;p&gt;Some time ago I was writing about a proposed &lt;a href=&quot;http://dikini.net/14.02.2006/wizardy_druidy_wiz&quot;&gt;method for resolving application state across multi-page forms&lt;/a&gt;. The sceme applies directly.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/22.03.2006/multi_page_forms_using_form_api&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/22.03.2006/multi_page_forms_using_form_api#comment</comments>
 <category domain="http://dikini.net/tags/drupal">drupal</category>
 <category domain="http://dikini.net/tags/programming">programming</category>
 <category domain="http://dikini.net/taxonomy/term/105">shorts</category>
 <pubDate>Wed, 22 Mar 2006 02:46:52 -0500</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">134 at http://dikini.net</guid>
</item>
<item>
 <title>Wizardy, druidy, wiz</title>
 <link>http://dikini.net/14.02.2006/wizardy_druidy_wiz</link>
 <description>&lt;p&gt;For a while I&#039;ve been looking at some interesting techniques for improved state tracking for web applications. &lt;a href=&quot;http://seaside.st&quot; title=&quot;smalltalk web application framework&quot; rel=&quot;nofollow&quot;&gt;Seaside&lt;/a&gt; and &lt;a href=&quot;/www.plt-scheme.org&quot; title=&quot;PLT Scheme&quot; rel=&quot;nofollow&quot;&gt;plt scheme webserver&lt;/a&gt; are using continuations to improve session handling, and have a nice clean code while at it. What does this mean? Continuations are a functional goto. They allow you to capture and save the state of an application, and possibly to return to it at any time. You could think of it as a snapshot of the current state of the application. Well, not exactly, but for the current discussion this will suffice.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/14.02.2006/wizardy_druidy_wiz&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/14.02.2006/wizardy_druidy_wiz#comment</comments>
 <category domain="http://dikini.net/tags/continuations">continuations</category>
 <category domain="http://dikini.net/tags/drupal">drupal</category>
 <category domain="http://dikini.net/tags/ideas">ideas</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <category domain="http://dikini.net/tags/programming">programming</category>
 <category domain="http://dikini.net/taxonomy/term/105">shorts</category>
 <pubDate>Tue, 14 Feb 2006 10:59:44 -0500</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">130 at http://dikini.net</guid>
</item>
<item>
 <title>Some ways to use saved state with closures in php</title>
 <link>http://dikini.net/25.01.2006/some_ways_to_use_saved_state_with_closures_in_php</link>
 <description>&lt;p&gt;In a &lt;a href=&quot;http://dikini.net/24.01.2006/emulating_closures_in_php&quot;&gt;previous short post&lt;/a&gt; Idescribe a way to emulate closures in php. Using that technique execution environment, otherwise known as a call stack can be saved for future use. This can be put to good use. A couple of patterns or programming techniques could be useful in practice.&lt;/p&gt;
&lt;h3&gt;State pattern&lt;/h3&gt;
&lt;p&gt;A closure represents a state =&amp;gt; implemenation of a state pattern. This is a bit rich. Usually in OO programming the state pattern is implemented by encapsulating different protocols, for denoted states. This is simple to implement by substituting your protocol specification with a different name. Drupal hooks are a good example implementation of this. The following is a simplisting example implementation of a state pattern based on closures. The last argument of the deduced function is the closure itself.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
class function_object {
  var $scope;
  function __get($name) {
    return $this-&amp;gt;$scope-&amp;gt;$name;
  }
  function __call($name,$args) {
    $func =$this-&amp;gt;$state.&quot;_$name&quot;;
    $args[]=$this;
    return call_user_func_args($func,$args);
  }
  function __constructor($scope) {
    $this-&amp;gt;scope=$scope;
  }
  function __clone() {
    $this-&amp;gt;scope = clone($this-&amp;gt;scope);
  }
  function state($state) {
    $this-&amp;gt;state=$state;
  }
  function call() {
    //the function body - do some work
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;To save the state for future use, for example as part of an undo cycle:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
  $saved = clone($closure);
&lt;/code&gt;&lt;/pre&gt;&lt;h3&gt;Call/cc or call with current continuation in php&lt;/h3&gt;
&lt;p&gt;Closure effectively represent the future of computation. This means that we can say what we want to do, by where we are. Since php doesn&#039;t implement any call stack optimisation, the call/cc style of programming makes sense in a limited number of cases. To use this style of programming we need to redefine our functions to use the continuation&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
 f(x,c) : c(f,x);&lt;br /&gt;
(var, continuation) -&amp;gt; continuation&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
and in php&lt;/p&gt;
&lt;p&gt;&lt;code&gt;&lt;br /&gt;
 //the function closure call method&lt;br /&gt;
 ...&lt;br /&gt;
 function call($x, $continuation) {&lt;br /&gt;
   $this-&amp;gt;scope=$continuation; // emulate c(f,x)&lt;br /&gt;
   ...&lt;br /&gt;
   do whatever you want here&lt;br /&gt;
 }&lt;br /&gt;
 ...&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
OK, this is rather crude. In reality this is very limited - it is assumed that the function is defined in a &#039;global&#039; scope, so we are putting it in some exhisting stack. It will be cleaner if the continuation implements the stack assignment, rather than rely on the function.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/25.01.2006/some_ways_to_use_saved_state_with_closures_in_php&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/25.01.2006/some_ways_to_use_saved_state_with_closures_in_php#comment</comments>
 <category domain="http://dikini.net/tags/articles">articles</category>
 <category domain="http://dikini.net/tags/code">code</category>
 <category domain="http://dikini.net/tags/design_patterns">design patterns</category>
 <category domain="http://dikini.net/tags/lisp">lisp</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <category domain="http://dikini.net/tags/programming">programming</category>
 <category domain="http://dikini.net/tags/scheme">scheme</category>
 <category domain="http://dikini.net/tags/scribbles">scribbles</category>
 <category domain="http://dikini.net/taxonomy/term/105">shorts</category>
 <pubDate>Wed, 25 Jan 2006 05:23:50 -0500</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">120 at http://dikini.net</guid>
</item>
<item>
 <title>Emulating closures in PHP</title>
 <link>http://dikini.net/24.01.2006/emulating_closures_in_php</link>
 <description>&lt;blockquote&gt;In programming languages, a closure is an abstraction that combines a function and a special lexical environment bound to that function (scope). The variables in the lexical environment are designed to retain state information between function calls. Unlike garden-variety functions which retain no memory of what happened in previous calls, closures are &lt;strong&gt;capable of storing information across function calls&lt;/strong&gt;.&lt;a href=&quot;http://en.wikipedia.org/wiki/Closure_(computer_science)&quot;&gt;closulres in computer science (wikipedia)&lt;/a&gt;&lt;/blockquote&gt;
&lt;p&gt;PHP doesn&#039;t have closures as an element of the language. What can we do to emulate it?&lt;/p&gt;
&lt;p&gt;We need to be able to encapsulate in one entity scope, state and execution. The most obvious candidate will be a PHP object.&lt;/p&gt;

&lt;p&gt;With the introduction of the __get(), __set() and __call() magic functions for PHP classes, something the manual calls overloading, PHP opens the gate to transparently enhance a class to add variables and methods to an object&#039;s body. We can abuse the __get() to implemet nested scopes. The state is preserved as part of the object body. The default function (call()) is a protocol to add a uniformity of execution between different function objects.&lt;/p&gt;
&lt;code&gt;
class function_object {
  var $scope;
  function __get($name) {
    return $this-&gt;$scope-&gt;$name;
  }
  function __constructor($scope) {
    $this-&gt;scope=$scope;
  }
  function call() {
    //the function body - do some work
  }
}
&lt;/code&gt;

If all $scope objects are function objects we get a closure emulation in PHP. This opens the doors to such fancy ideas as namespaces, modules et.al. This technique opens the possibilities for functional style programming techniques and idioms as delayed evaluation, continuation, ...
&lt;p&gt;&lt;strong&gt;Update:&lt;/strong&gt; I&#039;ve added &lt;a href=&quot;http://dikini.net/closure&quot;&gt;closures&lt;/a&gt; to my &lt;a href=&quot;http://dikini.net/php_design_patterns&quot;&gt;design patterns collection&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/24.01.2006/emulating_closures_in_php&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/24.01.2006/emulating_closures_in_php#comment</comments>
 <category domain="http://dikini.net/tags/articles">articles</category>
 <category domain="http://dikini.net/tags/code">code</category>
 <category domain="http://dikini.net/tags/design_patterns">design patterns</category>
 <category domain="http://dikini.net/tags/lisp">lisp</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <category domain="http://dikini.net/tags/programming">programming</category>
 <category domain="http://dikini.net/tags/scheme">scheme</category>
 <category domain="http://dikini.net/tags/scribbles">scribbles</category>
 <category domain="http://dikini.net/taxonomy/term/105">shorts</category>
 <pubDate>Tue, 24 Jan 2006 04:03:26 -0500</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">119 at http://dikini.net</guid>
</item>
</channel>
</rss>
