Skip to main content

Structures: C -> Java -> Python -> Erlang

With C we had structures and unions.
I like them, because, I don't always need to embed behaviors with data(class).

So you'll do something like

With Java, I'm forced to use classes, because that's how java is structured.
They are purely object oriented. (eg: in C++)



In python, you can take java's approach; but since Python is not Java ;-) and we
tend to write less,
We'll use dictionaries (or hashes in Perl)


I'm a newbie to earlang (and to functional programming),
I think there's no dictionaries, but tuples.

Here's how we use tuples to achieve same (also applicable to python)


much like json, isn't it?
notice, there are no quotes around someone, that's an atom (this is erlang dude).

This in python:
Well, how do we understand what's what?

hmm, ok, we can tag them with atoms, (this is a programming practice in erlang world)
so our earlier example becomes,

That's it.
You can do this with python too, like as I've shown below;
(but I personally prefer the use of dictionaries, as I've shown before)
    
But don't try to write programs in earlnag way in python, why?
Python got dicts, it's just an overkill to use tuples like this.
Also, in earlang, there're some language specific features that we can make use of.

In Earlang, we extarct values using pattern matching operator,

 2nd line will cause error.

it'll fail with an error. see, how it matches atoms at LHS and RHS.
(in python, we don't have this feature, since '=' is just an assignment operator as every
other object oriented/proceedural programmer think it is.)
we can use this in many interesting ways, for example, to distuinguish different
data structures at runtime and extarct them properly.

Just for the sake of it, I'll show another method to extract values called
"unification", which is useful for selective extarction; here it is...
But differntially from object oriented approch, we no more had a template now.
We'll use functions to create and parse these dictionaries and tuples.
(Pythonists like to write more functions than classes, because, python make it
really unncessary to write classes always, and we have 'Zen of python'.)

Comments

Popular posts from this blog

AJAX File Upload with Web2py

It was not that long, since I experienced a problem while trying to upload a file using an ajax  trapped form. I thought, it must be me doing something wrong. I was using web2py  to embed another page into a page via ajax. That is better known to web2py folk as LOADing a component. It's just happened that one of such component contains a file upload form. It was my first time using LOAD function provided by web2py. Basically it make use of jQuery to load the page via ajax into a target div and traps input of any form in that page, so that page doesn't reload. Oh, I forgot to say that web2py is bundled with jQuery. It's always boring and tedious to understand a problem without experiencing it. So, Let's play with an example, (PS: I"m using web2py a full stack python framework, but you can use any language at server side and this problem will be there because, it's a problem with ajax) My mod...

Software Engineering - Continues refinement

Every programmer wishes to write some code, which is both elegant, and readable. A master piece to reference to... And there may not be a single programmer, who said at least once in their life time that, "If I had time, I'd rewrite it" or any similar one. Today, let's discuss, how the idea of sorting a list of list went on,   I had to sort a list of lists, that's the result of a search, comes from Web Service. After that, the result is extracted from the XML document that's returned and formatted for front end. So, now all the data is in the form of strings. And I'm ended up with the task of sorting a list of list of strings, based on different items in the inner lists @ different times. And Obviously we can't compare the data as strings, we have to convert them into their proper data type before comparison. Since, I works on a Python based project, First thing, that came to mind is to use, sorted inbuilt function, and pass it a functi...

Correct way to structure your Django 1.4 projects

PS: This post is written assuming you're familiar with Django and at-least have some basic experience trying to set-up a Django project (for learning or for some cool project). Purpose: To show how to properly set-up your Django1.4 project after seeing other developers getting it wrong (seen it wrongly structured by my mentee, senior developers and junior developers at my firm.). Django 1.3 Project structure: Initial structure followed by two apps added to the project. Refer above picture, where I shown a Django < 1.4 project structure. (I know, at least Django 1.2 & 1.3 follows this structure). First tree view is of the initial structure that you will get by calling $ django-admin startproject Proj Take a note that manage.py, settings.py, urls.py are in the main folder. Following  tree display is after creating two apps named app1 & app2. You'll do it as follows $ ./manage.py startapp app1 $ ./manage.py start...