last day (14 days later) » 

11:14 AM
room topic changed to Erlang record inside an ordict: [erlang]
room mode changed to Public: anyone may enter and talk
 
Hi
 
hii
thank you for the answer..
 
np. :-)
 
I had some doubt regarding the code in that link..but i dont want to pollute the comment area ..so i invited you for a chat :)
 
11:18 AM
Ah.
So what is the part that you've got a question about?
 
The matching on TimeOut?
 
come to this part .. {Pid, MsgRef, {add, Name, Description, TimeOut}} ->
 
ok
 
ok..in that case ...
we have
NewEvents = orddict:store(Name,
#event{name=Name,
description=Description,
pid=EventPid,
timeout=TimeOut},
S#state.events),
we are storing records ..in orddict?
 
11:21 AM
Yes. NewEvents being a new orddict with the new #event{} added under tghe key Name.
A record is a tuple.
So in the list it looks like [{Name, {event, Name, Description, Pid, Timeout}}]
 
so the problem is...where is it receiving its information from? and why agin call loop in the end?
S#state.events ...this part?
 
So Key is an Erlang term (apparently a monitor reference) labeled as the variable "Name", and Value is a tagged tuple which corresponds to a record defined as #event{} in {Key, Value}
loop/1 is a recursive function.
You can't modify a list, right. You can only produce a new copy of it.
So you call loop again last with the new state you want in the process.
Yes, S is bound to the #state{} record which represents the current state of the process while it processes the loop().
So you call loop again last with the new state you want in the process.
I think I see what you are confused about.
The final argument to orddict:store is not S. It is S#state.events, which is an orddict.
 
ohhh...we are pushing it
ohh yes..
the entire event record is pused in State record?
ohhh..
is it?
 
Its referencing an element of the record S, not treating S (which is a record, meaning it is a tagged tuple of the form {state, Events, Clients}, where Events and Clients are oddicts).
Yes, S is a #state{} record, like I wrote above.
So one element of the tuple is its tag, the next is the orddict of Events, and the last is the orddict of Clients.
The syntax for referencing record elements is sort of weird.
-record(person, {name, age}).
P = #person{name = "Bob", age = "40"}.
P#person.age. % This evaluates to an expression which returns 40
So in loop()
 
11:32 AM
S#state.events % evaluates to the orddict containing the events
Its just like pattern matching like this:
{_, _, Events} = S,
orddict:store(Key, Value, Events).
 
They are equivalent, but the syntax of records prevents having to write a HUGE string of {_, _, _, _, _, _, Foo, _, _, _, _, _} every time you want element N of a really wide tuple.
Because Thingy#somerec.foo is a lot shorter.
But you keep the efficiency of tuple access in the background, because records are transformed to tuples during compilation.
Records are really just syntactic sugar over tuples.
Make sense now, or did I just confuse you more?;-)
 
great..understood some part of it..
but then..the recursion at the end ..what good is that for?
 
Because if we don't recurse the process ENDS
We want the process to continue, so it acts as, you know, a loop!
There are no looping constructs in functional languages, so the way you loop is to recurse.
 
untill the time is zero..i see
 
11:37 AM
Usually what would be the main while() {}; loop in another language is a loop() {blah, ..., loop()}; loop in a functional one.
No, time zero exists for the other processes.
Each timer event is a process here.
They are all separate.
When the event process' timers die those processes themselves die, and since the event_server process doing the loop() is monitoring them, when they die they send it a 'DOWN' message like {'DOWN', Ref, process, Pid, Reason}
That is the thing being matched later on in loop() here.
In Erlang you will see folks treat processes the same way they treat objects in Python or C++. Having millions of objects doesn't give a C++ a second thought, and having millions of processes in Erlang doesn't give an Erlang programmer a second thought, either.
This is a pretty strange way of thinking the first time you encounter it.
So anyway, the event server itself needs to continue to live. When an event timer process has its timer expire it commits suicide, and its last act is to send its DOWN message to the event server. The event server then removes it from the orddict that contains the list of events, and sends a message out to all the client processes in the orddict that contains the client list.
So the event processes are really little timebombs, not timers in the traditional sense you'd see in another language.
Still confusing?
 
this seems pretty difficult to grasp at once..I am trying to understand that page since yesterday morning..but everytime i think i understand some part of it..there is even more confusion..
 
Check out the same piece of code we've been looking at, the receive in the loop here.
The first thing that happens in the case statement when "true" is EventPid = event:start_link(..)
That is spawning a new process that is the event timer
 
So we've already got the independent process spawned which is not running inside the process doing the loop() we're looking at.
 
11:45 AM
Which is why we need this loop() to call itself again, to wait for another message to add a new event, or to process a message that one of the events has timed out, or whatever else.
 
yes..
got the point..i got the brids eye view of what we are doing..but programmatically it creates a lot of confusion
 
Think of it like people, then.
I'm going to tell you to track an event, and in turn you're going to give someone else a number of seconds after which they are going to shout "I'm down!". Once they do that you are going to call me to let me know they went down.
And, in Erlang this code is one way of specifying that procedure.
You'll get used to it after a bit.
But expect to remain confused for a while longer. Remember the basic law of technical exploration: if you aren't confused then you aren't learning anything.
 
hahahaha..
so in this case..you are the Event Server
 
I am the client, you are the event server. The event server is spawning the timer processes.
Er, event processes.
The events talk to the server, the clients talk to the server, but the events and the clients never speak directly.
 
okay..now its pretty much clear..
 
11:51 AM
The difficult part of this sometimes is when you have two different processes spawned from the same module talking to each other. The specification is in the same source module so it can be hard to model that in your head until you get used to it.
In other words, unmediated peer communication.
That can take a bit of getting used to, in particular you have to settle on a verbiage for the contents of messages that represent commands and acting on commands. So like a command message might be {hit, Enemy}, and where you send that message you are thinking about it as a verb "to hit".
But you receive it in the same module by matching receive {hit, Enemy}, but this time "Enemy" is the current process (which received the hit), and "hit" is an incoming thing, not a command.
So your semantics can get turned around pretty quick if you don't keep a firm grasp that you are NOT SENDING RPC, you are sending messages. So you should phrase things in a way that makes it clear that you are "sending a message about a hit" instead of "sending a command [to hit/receive a hit]".
This whole "messages are not RPC" thing is, in my view, the hardest part of getting folks used to Algol-style languages to understand a real message passing language like Erlang.
 
your name?
 
Craig Everett.
Yours?
 
Jayesh Jain
Thx.craig
 
Nice to meet you.
No problem. Keep working at understanding this and it'll eventually make sense.
I have never been able to really understand stuff from just reading. Build yourself a variation of the event server and this will make more sense.
 
the pleasure is all mine..you made a lot of things clear and simpler too.I will take the next few days learning basic erlang.
 
12:00 PM
Have fun with it. You'll find that you can model quite a few really hard things in Erlang very easily compared to other languages. Of course, a few easy things in some other languages can be hard in Erlang, too, so there is always a tradeoff.
For the most part Erlang has been a positive tradeoff for me, though.
 
the only documentation source that i found easy to learn was learnyousomeerlang.com ..should i prefer some book or just keep practicising..
 
I actually bought the paper version of that book -- reading entire books online makes me cranky.
But that is a good one. Another good one is Programming Erlang, 2nd ed.
 
alright..
 
OTP in Action is really good, too. But it will make your head explode if you haven't read one of these two first.
Just so much OTP stuff floating around at once.
Read a little, then try a hardish problem, like writing a chat system on your own.
Then port the chat system from a raw Erlang thing to an OTP thing.
For example.
You'll learn a ton.
 
yes thats what i am trying to build.. a chat system
 
12:02 PM
Then, re-read LYSE, and stuff that wasn't clear before will stick out as really good lessons.
 
do you mind if i mail you in future?
 
Feel free.
zxq9@zxq9.com
 
alright..thx a ton craig
you stay in japan?
 
yes.
Born in Texas, but this is where I live now.
You?
 
Mumbai,India
 
12:05 PM
Quite a few of your countrymen have been popping up on the list lately. Trying to escape the pit of of Java?
I say that because a company I was contracting for last year had contracted out a lot of Java maintenance work to a subcontractor in Mumbai.
 
And the guys were really nice, but they seemed to be sort of depressed sometimes -- like happy to have decent jobs, but not really enjoying the work.
Working in an unhappy coding shop must really suck.
 
yaa..thats the case with many programmers in india
 
Well, things might change in the future anyway. This is sort of the first serious years of independent software development in India -- no telling how much more interesting things might get in the future, right? ;-)
A lot of younger talent coming of age and all that.
 
yes..People have started taking coding seriously..
Web Development specially..
 
12:08 PM
I wish I could find more Japanese coders who knew Erlang, actually! There is way too much Java and Visual Basic out here!
Unfortunately I don't think the real future is in the Web. I think its a stopgap formed from the rubble of bad ideas. But that's my personal sense of things.
 
it has got a lot with the learning curve..learning PHP,Ruby,Asp is far more easier than python,c,c++,Erlang
besides Java programmer are everywhere..PHP,JAVA are trending hot in mumbai
 
Yes. Learning a "currently hot" framework is a lot easier than learning something conceptually new. I mean, when "my new language" means Ruby and "my old language" was Python or Perl... well, its really just about the same language, really.
 
lot of jobs
But yes..It all boils down to understanding a problem fundamentally..and solving it.Thats programming
be it any language..
 
If you play around with Erlang a bit, then play with Scheme for a while (Guile2 is a really interesting platform), then mix C and Guile a bit, then write some assembler, then learn Qt's dialect of C++, etc. you'll really start seeing all languages as sort of different (crappy) flavors of the same underlying thing.
And you'll really, really like that underlying thing, but really sort of hate every language and genuinely despise every framework you encounter for one reason or another.
 
man you have tried a lot of things.. Amazing
 
12:15 PM
It gets even more intense if you start playing with hardware architectures. Do some x86 assembly for a bit, think its cool and then check out PPC and you'll fall in love!
And start totally hating x86.
And then try RISC or Arduino boards or some other teeny tiny embedded thing (or make your own 4-register, 4-bit processor out of LEDs and switches or something) and then you'll just let go and start sort of hating all architectures, too.
But like them because they can do cool things once you learn to love the fact that you sort of hate all the tools out there.
 
hahaha..
 
Which will probably land you right back in the C or Python job you thought you were escaping.
But you'll actually enjoy it once again.
Even in Java.
 
seems you havent left any language..
:)
 
Of course, you'll be doing things in Java (or Clojure, if your boss doesn't notice) that you never realized were possible, because you'll suddenly understand that "Java classes" are actually just really obscure syntax over higher-order function definitions! WEE!
Anyway, my point is that you'll strike a point (probably soon, by the looks of it) where you start having "ah ha!" moments more often, and that's a good thing.
 
fingers crossed..
thx for the time craig..
 
12:19 PM
Take care, Jayesh.
See you round.
 
Sure..
Bye.
 

last day (14 days later) »