PHP try to emulate functional programming by providing the functions like array_reduce. But what they lack is the knowledge of principals of functional programming.
I had a problem where there are multiple arrays and need to be merged. array_merge function works only on 2 arrays. Merging multiple arrays need a for loop OR use array_reduce function [I thought]. But it turns out that array_reduce function is not functional as I imagined. Even though the documentation say that the last parameter to this function is the initial value for the array fold function, actually it is not.
If the initial parameter is not an integer, PHP will default to zero. For the above problem I tried the following
array_reduce($my_arrays, "array_merge", array());
What I expected it is to work as
$initial = array();
foreach ( $my_arrays as $m ) { $initial = array_merge($initial, $m); }
What I got was
PHP Warning: array_merge(): Argument #1 is not an array in Command line code on line 1
PHP Warning: array_merge(): Argument #1 is not an array in Command line code on line 1
These kind of behavior will lead to serious problem in the system and is hard to debug.
Showing posts with label function programming. Show all posts
Showing posts with label function programming. Show all posts
Thursday, October 14, 2010
PHP author(s) doesn't understand functional programming
Labels:
function programming,
php,
programming
Saturday, September 26, 2009
Load balancing in Erlang using pg2 process groups
While working with some project @ my current workplace, we came across a situation where we are using pg2 process group. Apparently pg2 does not load balance across processes registered in the pg2 group. It usually done in round robin way. So it may happen that even though many processes are registered under same group, but all messages will get queues in single process.
My co-worker and I came with an algorithm to solve this. It is describe here.
My co-worker and I came with an algorithm to solve this. It is describe here.
Labels:
erlang,
function programming
Saturday, March 21, 2009
Imperative language constructs in Erlang
"How can I do for loop in Erlang?", "How can I do while loop in Erlang?"....
These are the common questions people ask when they get into the world of functional programming from imperative language(s). It is easy to switch to functional programming in Python, Ruby etc as they are the mixture of functional and imperative language constructs. Programming languages like Erlang (probably Haskell) do not provide explicit for, while constructs. Here are the equivalents way to implement some of the imperative language constructs.
For Loop
for_loop(N, Fun) ->
lists:foreach(Fun, lists:seq(1, N))
for_loop(0, Fun) -> ok;
for_loop(N, Fun) ->
Fun(N),
for_loop(N-1, Fun).
While loop
while_loop(Predicate, Fun, State) ->
while_loop(Predicate(State), Predicate, Fun, State).
while_loop(PredResult, Predicate, Fun, State) when PredResult == false -> ok;
while_loop(_PredResult, Predicate, Fun, State) ->
NewState = Fun(State),
whle_loop(PredResult(State), Predicate, Fun, NewState).
These are the common questions people ask when they get into the world of functional programming from imperative language(s). It is easy to switch to functional programming in Python, Ruby etc as they are the mixture of functional and imperative language constructs. Programming languages like Erlang (probably Haskell) do not provide explicit for, while constructs. Here are the equivalents way to implement some of the imperative language constructs.
For Loop
for_loop(N, Fun) ->
lists:foreach(Fun, lists:seq(1, N))
for_loop(0, Fun) -> ok;
for_loop(N, Fun) ->
Fun(N),
for_loop(N-1, Fun).
While loop
while_loop(Predicate, Fun, State) ->
while_loop(Predicate(State), Predicate, Fun, State).
while_loop(PredResult, Predicate, Fun, State) when PredResult == false -> ok;
while_loop(_PredResult, Predicate, Fun, State) ->
NewState = Fun(State),
whle_loop(PredResult(State), Predicate, Fun, NewState).
Labels:
erlang,
function programming,
loops
Friday, November 14, 2008
Parallel assignment
Today I was working on some problem (off course in Erlang). What I am trying to achieve was to execute a function in parallel and assign result of function to a variable.
In the above code, calling Func() block the execution until receive loop within Func is done receiving the data from the spawned process. SomeOtherValue is assigned only after the execution of Func.
What I want is not that. I want the execution to continue even if the execution of Func is not finished. I want to block only when I am trying to use RetVal variable.
Currently I am acheiving this by waiting for the response from the spawned process only when I need the data from the process, i.e., same as blocking when the variable is accessed.
Even though it is a trivial code change, I would like if the parallel assignment is allowed and block when I try to use it (if necessary).
Func = fun(Arg) ->
Parent = self(),
Pid = spawn( fun() -> timer:sleep(1000), Parent ! {self(), Arg + 2} end ),
receive {Pid, Val} -> Val
end,
RetVal = Func(),
SomeOtherValue = 10,
% rest of the code
io:format( "~p~n", [RetVal] ),
% more code here
In the above code, calling Func() block the execution until receive loop within Func is done receiving the data from the spawned process. SomeOtherValue is assigned only after the execution of Func.
What I want is not that. I want the execution to continue even if the execution of Func is not finished. I want to block only when I am trying to use RetVal variable.
Currently I am acheiving this by waiting for the response from the spawned process only when I need the data from the process, i.e., same as blocking when the variable is accessed.
Func = fun(Arg) ->
Parent = self(),
Pid = spawn( fun() -> timer:sleep(1000), Parent ! {self(), Arg + 2} end ),
end,
Pid = Func(),
SomeOtherValue = 10,
% rest of the code
RetVal = receive {self(), Val} -> Val end,
io:format( "~p~n", [RetVal] ),
% more code here
Even though it is a trivial code change, I would like if the parallel assignment is allowed and block when I try to use it (if necessary).
Labels:
erlang,
function programming
Subscribe to:
Posts (Atom)