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)
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...
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'.)
I like them, because, I don't always need to embed behaviors with data(class).
So you'll do something like
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
struct User{ char name[45]; int age; float height; }usr; usr.name = "Someone"; usr.age = 31; ur.height = 6.1;
With Java, I'm forced to use classes, because that's how java is structured.
They are purely object oriented. (eg: in C++)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User{ | |
String name; | |
int age; | |
float height; | |
void set_user(); | |
User get_user(); | |
} | |
void User::set-user(){ | |
/* implementation */ | |
return; | |
} | |
User User::get_user(){ | |
usr = User(); | |
/* implementation */ | |
return usr; | |
} | |
usr = User(); | |
usr.set_user(); | |
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
usr = {'name': "Someone", 'age': 33, 'height': 6.1 }
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
usr = { someone, 33, 6.1 }. |
much like json, isn't it?
notice, there are no quotes around someone, that's an atom (this is erlang dude).
This in python:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
usr = ('someone', 33, 6.1) |
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,
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Usr = { 'User', | |
{name, someone}, | |
{age, 33}, | |
{height, 6.1} | |
}. |
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?This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
usr = ('User', ('name', 'someone'), ('age', 33), ('height', 6.1) )
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,
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{'User, N, A, H} = Usr. | |
{'Any", N, A, H} = Usr. |
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.This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
{_, _, {_, Age}, _} = Usr.
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
Post a Comment