Skip to main content

functional vs oo

well it's been a while and I have come to trivial but usefull conclusions lately.
Once I thought functional programming was fundamentaly diffrent then
oo style programming, but I actually realized how well many aspects
of fp match to elements of oop;

Functions are simply Objects,
Closures are anonymous class,
certain design patterns ressemble
monads(decorator, chain of reponsibility,...)

oo style programming can be seen as a restricted variation of functional programming.

That matters because the key aspects of oop is encapsulation and
information hiding. This can easyly be achieved in fp through the
use of closures and the fact that functions can be treated like
any other data.

In oop very explicit notations usually exists, wich couple
certain functions to certain data through the notion of objects. Both, to increase readability of complex programms, and to lighten the restrictions that come with encapsulation, an explicit notion of inheritance is used.

While all this seems pretty obvious to most, frankly, I did not
appreciate oop after doing a a bit of fp, until I found myself reading through some javascript code that I wanted to optimize.
The code was using a many closures like this, which I removed
by placing the variables that the closures contained in
properties of objects, that also contained the functions
corresponding to the closures' code.

When I was finished I had not only increased speed by 40-60%
but was also surprised by the fact that my code looked a lot
like good oo-style code.

So in my opinion every programmer should learn to think functional
in order to get a hold of what the importance of encapsulation and code reuse.

That said, most javascript interpreters(javascript by itself not a purely functional language like haskell anyway) could do this manual optimization of closures automatically.

It is rumored that modern lisp systems and erlang(using HiPE) interpreters/compilers are very close to compiled "C" code performance:
See i.e. http://www.sics.se/~joe/apachevsyaws.html
or http://bob.pythonmac.org/archives/2006/09/21/erlang-binary-performance/

But to emphasis my point of oop vs fp regard this page:
http://openmap.bbn.com/~kanderso/performance/java/index.html

That's it for today.

Comments

Popular posts from this blog

Lazy Evaluation(there be dragons and basement cats)

Lazy Evaluation and "undefined" I am on the road to being a haskell programmer, and it still is a long way to go. Yesterday I had some nice guys from #haskell explain to me lazy evaluation. Take a look at this code: Prelude> let x = undefined in "hello world" "hello world" Prelude> Because of Haskells lazyness, x will not be evaluated because it is not used, hence undefined will not be evaluated and no exception will occur. The evaluation of "undefined" will result in a runtime exception: Prelude> undefined *** Exception: Prelude.undefined Prelude> Strictness Strictness means that the result of a function is undefined, if one of the arguments, the function is applied to, is undefined. Classical programming languages are strict. The following example in Java will demonstrate this. When the programm is run, it will throw a RuntimeException, although the variable "evilX" is never actually used, strictness requires that all argu

Learning Haskell, functional music

As you might have realized, I started to learn Haskell. One of the most fun things to do in any programming language is creating some kind of audible side effects with a program. Already back in the days when I started programming, I always played around with audio when toying around with a new language. I have found a wonderful set of lecture slides about haskell and multimedia programming, called school of expression. Inspired by the slides about functional music I implemented a little song. Ahh ... and yes it is intended to sound slightly strange . I used the synthesis toolkit to transform the music to real noise, simply by piping skini message to std-out. I used this command line to achieve the results audible in the table: sven@hhi1214a:~/Mukke$ ghc -o test1 test1.hs && ./test1 | stk-demo Plucked -n 16 -or -ip Sound samples: Plucked play Clarinet play Whistle(attention very crazy!) play As always the source... stueck = anfang :+: mitte :+: ende anfang = groovy :+: (Trans

Erlang mock - erlymock

NOTE THIS POST IS OUTDATED! The project has evolved and can be found here: ErlyMock Some features Easy to use Design based on easymock Works together with otp: can be used even if the clut is called from another process, by invoking mock:verify_after_last_call(Mock,optional: timeout) custom return functions predefined return functions for returning values, receiving message, throwing exceptions, etc.. erlymock automatically purges all modules that were mocked, after verify() Custom argument matchers: %% Orderchecking types: in_order, out_of_order, stub; %% Answering: {return, ...}|{error, ...}|{throw, ...}|{exit, ...}|{rec_msg, Pid}|{function, Fun(Args) -> RetVal} expect(Mock, Type, Module, Function, Arguments, Answer = {AT, _}) when AT==return;AT==error;AT==throw;AT==exit;AT==rec_msg;AT==function -> call(Mock, {expect, Type, Module, Function, length(Arguments), {Arguments, Answer}}). %% this version of expect is suited for useing custom argument matchers expect(Mock, Type,