last day (3269 days later) » 

user2355058
7:00 AM
hey
 
Hey there
 
user2355058
wow your logic in that last comment would half my code...
 
user2355058
i think
 
Just glanced at your updated code. Might be some places to simplify things for your mind.
 
user2355058
spawn a process for each item in the list....
 
user2355058
7:02 AM
sounds much easier than what I did
 
An easy way to keep track of that is with a list comprehension. Like:
[spawn_monitor(fun() -> child(SeqItem) end) || SeqItem <- Sequence]
So then you would a process per item doing whatever it does, and a list that corresponds to all of those processes of the form [{Pid, Reference}] -- so you could search through it like a proplist when they return their result or crash.
When either happens, you remove that item from the list and loop again -- once all the processes are dead (assuming you respawn any that crash) you move on to perform your final computation.
 
user2355058
im a beginner unfortunately.
 
user2355058
the <-
 
Have you used list comprehensions before?
 
user2355058
I do not understand that
 
user2355058
7:06 AM
no i have not.
 
Have you used a map?
 
user2355058
i have taken a scheme course in college but i have no dared yet in erlang
 
Like lists:map(fun(Z) -> Z + 1 end, [1, 2, 3]) should add 1 to each item and return the updated list.
 
user2355058
no advanced list stuff yet
 
Do you know Python or Ruby or Perl or something like that?
 
user2355058
7:07 AM
C# java and racket
 
So in scheme, imagine we have a list like '(1 2 3)
 
user2355058
k
 
user2355058
i relly appreciate your help
 
And a function defined like this (define (plus-one z) (+ 1 z))
 
user2355058
ok
 
7:09 AM
So now (plus-one 10) will return 11.
 
user2355058
sure
 
So think back to our list '(1 2 3) If we (car '(1 2 3)) we get 1 by itself
And the cdr is '(2 3)
 
user2355058
oh wow i see how that could be used here
 
If we think about that, we can do (plus-one (car '(1 2 3)) and get back 2
If we start with an empty list, like (), and cons the result of plus-one... we will get '(1)
 
user2355058
the reason I wanted to use message passing was that my threads can not access the list at the same time. That is why I thought message passing with blocking was good
 
7:13 AM
Normally it would be, if the sequence mattered. But with summation it doesn't.
 
user2355058
well i guess I want the first thread to take 1, the second 2, third 3, etc
 
user2355058
this is how it would work with blocking anyway
 
You can I can take a giant list of receipts, split them down the middle, I sum half and you sum half, and add the result to get the total, right?
Completeness matters, not the order.
 
user2355058
right
 
Right. But we can drop the blocking. We only need to check for completeness.
So if you keep track of the processes you spawn, and make sure each one checks in -- you only block on whether they all checked in or not, not which particular one is finished in what order.
 
user2355058
7:15 AM
well each thread(assuming 4) needs to do 1/4 of the work. but that means 1-25, 25-50,50-75,75-100
 
user2355058
not just 25 25 25 25
 
user2355058
just making sure you understand that.
 
You can split your list up whatever way you want. In Erlang there isn't anything particularly bad about spawning a 100 processes, even.
(I'm working on a game server that spawns around 25,000 with the current map... not a big deal)
 
user2355058
yeah
 
user2355058
wow
 
user2355058
7:16 AM
that is why we are doing erlang in my school
 
user2355058
its cool
 
Anyway, you can divide the list up by its length -- and then accept an arbitrary number, knowing you won't accidentally spawn a billion workers at once (that probably wouldn't work out so well).
 
user2355058
can I possibly use the model taht I have now?
 
Or, just cycle down the list, chopping it up into chunks of a fixed size (never checking how long it is) and stop whenever it is empty.
Sure, you can do that. It needs some changes, though. Like the await function should be rolled into the main loop.
You need to be checking for "I'm done" and "I crashed" messages, and updating your list of current workers instead of waiting for them in a separate function.
 
user2355058
oh really?
 
7:19 AM
Do you have an interpreter open?
 
user2355058
i have the official erlang shell
 
user2355058
ooen
 
user2355058
and my call to make_sequence never returns
 
user2355058
that is my current bug. no way to tell if anything else doesnt work
 
Check this out:
Hey = fun(N) -> io:format("I got ~p.~n", [N]) end.
 
user2355058
7:21 AM
if you see any glaring fixes let me know. I have really been enjoying programming tonight, but it is very late where i live and I must sleep soon. Assuming I am not able to do the list comprehension on this program, how would you fix this code so that it works
 
List = [1, 2, 3, 4, 5].
[spawn(fun() -> Hey(X) end) || X <- List].
Try running those 3 lines and see what you get.
 
user2355058
ok
 
What the line with [stuff(X) || X <- List] is doing is taking 1 item at a time from List, assigning it to X, and building a new list out of it.
 
user2355058
oh wow.
 
In this case we are performing an operation on each item, though, so the return value is the value of that operation.
 
user2355058
7:23 AM
i like the list comprehension
 
spawn/1 returns a Pid, so the list we get back is a Pid that corresponds to each number we sent out.
Now try this:
[{X, spawn(fun() -> Hey(X) end)} || X <- List].
Now we have a list that looks like [{ListElement, Pid}]
 
user2355058
yeah i can see that
 
If we assign that list to a variable, we could use a list function to make the list item a key, and get the Pid back that way:
 
user2355058
but you know what man, you are way over my head right now
 
user2355058
i am still a noob
 
7:26 AM
`3> Workers = [{X, spawn(fun() -> Hey(X) end)} || X <- List].
I got 1.
I got 2.
I got 3.
I got 4.
I got 5.
[{1,<0.37.0>},
{2,<0.38.0>},
{3,<0.39.0>},
{4,<0.40.0>},
{5,<0.41.0>}]
4> proplists:get_value(3, Workers).
<0.39.0>`
The last line I wanted to see what was the worker Pid that was handling the number 3. So -- let's say that this Pid crashed, I would know what value to restart a new worker with.
 
user2355058
my workers are NOT going to crash
 
user2355058
they do not need to account for that
 
user2355058
this is a simple project
 
user2355058
a very simple project and I need a queue or counter with blocking.
 
You don't need blocking per process, you just need to block waiting for a complete result.
Blocking (as in, sequential messaging) defeats the purpose of even using workers at all.
 
user2355058
7:29 AM
if there is any way you can help fix my code that I posted on SO please leave me a message there. I appreciate your tutilage and I am in awe of your generosity (your time) but I must go to sleep now. Like I said if you see what is wrong with my code let me know.
 
user2355058
i understand that but this is a very simple assignment meant to teach me about message passing and using simple concurrent futures
 
btw, these aren't futures. They are just concurrent processes.
 
user2355058
calling the make_sequences does not return.
 
user2355058
make_sequence() ->
spawn(pi, sequence_loop(1), [])
end.
 
user2355058
why does that not return to where it was called?
 
user2355058
7:32 AM
can i save the text you wrote me? i would like to review it when i have time
 
You are calling a function where you should be providing an atom.
So its trying to call sequence_loop/1 with an argument of 1 before anything has even been spun up yet.
 
user2355058
oh
 
And get_next/0 is... wait, you're trying to call a function as if its a process.
 
user2355058
where?
 
user2355058
in accum?
 
7:35 AM
An easy way to spawn a process, giving it the pid to its invoker looks something like this:
 
user2355058
i really have to go. im sorry that my code is so far off from anything working that you can not help me. take care
 
cheers
Looking at your formatting... it looks like you're trying to define methods on objects.
That's not how Erlang works.
Any process can call any function that is exposed to it. Anything in its current module is exposed to it, and anything exported from another module is exposed to it.
There are no methods.
 

  last day (3269 days later) »