The Functional Approach Part 2

Hello, and welcome to part 2 of approaching some programming problems with functional methodologies. In the last post, I had concentrated on two key areas: Transforming one list into another with the application of an algorithm on each original item to obtain the transformed item, and Filtering the original list based on a predicate to obtain a list with possibly less items than the original.

Today I will discuss the third aspect of functional list processing: Folding.

Folding a list yields exactly one final value. That value depends on the items in the list, and a function that takes the first element of the list, and a start value. The returned result is then used as the “start value” for the next iteration of the fold. In this next iteration the first element of the list is popped off, and the next element becomes the first (we have a shorter list). This process goes on until the entire list is exhausted and the final result is the start value that would have been used had the list had more elements.

Example:

var list = [1, 2, 3]; alert(fold(list, 0, function(start, item) { return start + item; }));

Here, we see a list with values 1, 2, and 3. In the first iteration the start value, 0 in this case, is used as the first parameter to the supplied function and 1, the first element of the list is the second argument. The function then returns their sum (1) to be used as the start argument to the same function in the next iteration. This time however the item parameter will take on the value 2, and the function will sum them up to yield the result 3, which again becomes the start value for the next iteration of the function call. The second parameter is 3 (the last item of our list). The final sum then becomes 6, and since there are no more items, that is the final value of the call to fold. So the alert box pops up with the value of 6.

Final thoughts: By combining the transform, filter and fold algorithms together to use and manipulate lists of items, a section of code can become very condensed and localized, reduced in complexity, and become self documenting. Like with all tools, there is a risk of misuse. My suggestion in this regard would be to first figure out whether a piece of code lends itself to this pattern. A lot of the time list processing code fits the bill quite well. However, this is not a magic bullet. If the occasion calls for an object with state and methods, then by all means write a class or a prototype!

PHP Tricks

One of the best tools a programmer can have is insight into his tools of the trade. PHP, for instance, offers many little features that help in this regard. One such trick is defining a function in your class called “__call”. The idea is that this method is called whenever you try call a method the class does not define, as long as what __call does is acceptable to the caller. Of course a small example is worth a thousand words…

```class Foo {     // below is the definition of the __call method.
It takes 2 parameters.     // \$method - the name of the method
the user has called as a string     // \$args - the arguments
passed by the user to the method when calling it as an array
function __call(\$method, \$args) {         echo "Calling method
\"{\$method}\" with ".count(\$args)." arguments!\n";     } }
\$foo = new Foo; \$foo->bar(1, 2, 3); // call a method not explicitly
defined in class 'Foo'```

The above example will output: Calling method “bar” with 3 arguments!

I have found that this technique works well in particular when generating a tree like data structure such as a DOM. You simply have use a switch based on the method name and return a new object using the \$args parameter to supply arguments to the constructor. This scheme has the advantage of localizing all of the various classes into one single interface, and it keeps the user away from the details of the underlying class hierarchy and implementation.

Developing Mobile Apps for Multiple Screen Sizes and Resolutions

As phones become more advanced, they grow more diverse as well. Some of today’s phones can be considered fully operational personal computers. Many people have begun developing fully fledged apps for these devices including multimedia and interactivity. A common type of applications on today’s phones are mobile web applications.

The earliest phones equipped with web browsers were very simplistic. They used a markup language known as WML (Wireless Markup Language). It is basically a simplified version of HTML with various hints about displaying elements on a very small screen.

Today’s mobile web applications use full on XHTML and other advanced web technologies such as Javascript and Ajax. An age old problem in the web development world is supporting screen sizes. Traditionally we stuck with a standard 800×600 screen and a constant aspect ratio. Today’s phones’ resolutions span many aspect ratios and resolutions, so how do we develop for these mobile platforms and keep visual sanity?

Well there is no for sure method, but there are some basic rules of thumb. Some common screen resolutions on today’s phones are:

• 128 x 160 pixels
• 176 x 220 pixels
• 240 x 320 pixels
• 320 x 480 pixels

They all follow a similar aspect ratio so we are basically going to develop applications that look tall and skinny. A good idea is to left align elements, or center them, so they will display on many different resolutions.

Try to keep the most important elements on the top of the page, and scale down as you move downwards, just like any good XHTML web development methods.

So now we know, we must be very careful when developing mobile applications, that we support many screen resolutions and aspect ratios, in order to maximize usability.

Installing Share Plugins

Hi Everyone.

So now we have our word press up and running, we are interested in installing plugins that will allow users to share our posts on social networking services like facebook, twitter, buzz, etc.

We took a look at how other sites did this, and one stuck out particularly. We liked the way things looked on Mashable.com.

The plugins we will be using:

To install plugins:

Unzip the plugin:

unzip buzrrcom-button-plugin.zip

And were done with the install. Next we must go into wordpress admin panel.

Click on “Plugins” and find your “Buzzr.com Button Plugin”. Underneath, click the “Activate” link and you should be done!

– Written by: Ivgeni Slabkovski

The Functional Approach

Today I would like to talk about one of my favourite topics, programming theory. Do not be alarmed, however, I’m not about to bore you with mundane details. Over the past few years discussion about functional programming has become quite common, though a lot of confusion remains. There are many reasons that contribute to the problem, but mostly its because many main stream languages aren’t known for their functional properties.

So, what are the real benefits in this approach and why should you care? Here are just some of the benefits I have learned of…

The real benefit is that functional programming is declarative. What does that mean to you as a programmer? The answer to that question lies in a small mind shift. Instead of focusing on how something is to be done, you shift into focusing on what that something is.  A small example:

// traditionally, when working on a sequence you would write loops:  var sequence = [1, 2, 3];  for (index in sequence) {     alert(sequence[index]); }  // using a list comprehension we can capture the concept of // using alert on each item of the list without explicitly // setting up a looping construct.  [ alert(item) for each(item in sequence)]

What we’ve done here is put the computation right into the identity of the list. What you see between the [ and ] *is* the list where each item is expressed as a formula that is applied to each item of ’sequence’ (another list). Now, lets take this a step further:

var sequence = [1, 2, 3, 4];  [item * item for each (item in sequence) if item % 2 == 0].map(function(item) { alert(item); });

In one single line, we’ve singled out all the even numbers of ’sequence’, squared them, and applied an arbitrary piece of code to each of those values. Here we’re the code simply pops an alert box with the value on to the screen.

This style, with a bit of practice, can greatly reduce complexity by localizing the code that traditionally had to be written in separate locations, even though the different bits of code themselves do not have much meaning in isolation. Consider a more traditional procedural implementation of the line above, first we define the function that will execute for each element. Notice how it is a separate definition to itself:

// here we pretend that this function will never be used // anywhere except for one specific set of values // contained in ‘sequence’.  function alert_item(item) { alert (item); }

Then we have to define the looping construct which will select every item (even and odd):

for (index in sequence) {     // we can now filter based on even / odd,     // notice that you have to manually extract the value out of the sequence using index     if (sequence[index] % 2 == 0) {         // finally we can apply the code we want to to each of the filtered elements (evens).         var item = sequence[index] * sequence[index];         alert_item(item);     } }

At each step both the programmer writing the code and the programmer reading the code become painfully aware of ‘how’ the computer will be doing something. What that something is is essentially lost in the noise of all the different constructs. In this trivial example it is still possible to quickly grasp what that something is, however, as the code size grows, the number of functions to maintain also grows (usually to a much larger number than anyone would like). We as human beings will always have a limit of what is manageable at any given instance, and the more we can do to limit what we absolutely have to remember in each given instance, the better.

The point of this exercise was to illustrate the disconnect that emerges from doing very simple, everyday things, and to show, that one flexible way of dealing with this problem is to localize sections of code by utilizing functional programming techniques. Today we’ve seen the application of filters by singling out specific elements of one list as items of a new list. Then we transformed each of those items to its square. There is one more trick that is indispensable to functional programming: Reducing a list to a single value. That is a topic that deserves its own post devoted to it.

This is where I will end this post. I hope that you found it useful and entertaining. Functional programming is a very large topic, and I’ve only barely scratched the surface. Though with the few little pieces presented here one can already begin taking advantage of these rather powerful techniques.

– Written by: Ivgeni Slabkovski, Programmer

Installing WordPress

Hi Everyone,

Today I will be going through a simple WordPress set up. What you will need is a web server with SSH terminal access.

First we change into the directory that we are going to install WordPress.

#cd [ROOT WEB DIRECTORY HERE]

#wget http://wordpress.org/latest.tar.gz

Now we untar (unzip or whatever you prefer) the source code into our current directory.

#tar -xzvf latest.tar.gz

This step should create a folder called “wordpress”. That is fine, except we want our blog to be under the folder “mobile-web-marketing”. To rename the folder, we use the “mv” or “move” command.

#mv wordpress mobile-web-marketing

And voila, our WordPress is now ready to be set up. You can check it out here: http://blogs.citydirect.info/mobile-web-marketing/. Check back soon for another post about setting everything up.