I'm trying to learn some Erlang and having trouble figuring out the best approach for a particular problem. Here's what I'm doing:
I have a big 2-D array of data (list of lists) whose values are calculated by independent processes. The processes send a message back to an aggregator with the result when they're done calculating. My problem is how to get the aggregated data into a new big array. I could do something like this:
aggregator(Array) ->
receive
{Value1_1, {1, 1}} ->
Value1_1 = Value1_1
end,
receive
{Value1_2, {1, 2}} ->
Value1_2 = Value1_2
end,
% ...
{{Value1_1, Value2_1},
{Value2_1, Value2_2}}
but that's terribly lame and scales poorly. What I think I want to do is more like this:
aggregator(Array) when is_full(Array) -> %% I think I know how to write is_full.
Array;
aggregator(Array) ->
receive
{Value, {X, Y}} ->
aggregator(replace_2d(Array, X, Y)) %% Is this the way to go?
end.
but I don't see any built-in facilities for replacing values based on an array index in the built-ins. This leads me to suspect that I'm missing some idiomatic way of achieving my goals that would be more obvious to someone experienced with functional programming. Am I? Is my approach all wrong? Should I just write myself a replace_2d function and call it good?