I have in mind a design problem that does not look like complex, but I can’t find by myself a way to solve it. I would like to follow as much as possible the DRY principle.
I have got two functions. One is the basic version of the algorithm, the other one is an improvement with parallel programming. They are two, and my aim is to write only one of them with an additional parameter, e.g., “parallel”. Here the pseudo codes.
fun1 ()
loop for par1 times
do_work()
fun2 ()
loop for par1 times
run new thread
do_work()
Currently I have coded
if parallel == 0
fun1 ()
else
fun2 ()
What I want is something like
fun ()
loop for par1 times
run new thread if parallel > 0 #Ruby's syntax-like
do_work()
I was wondering if Ruby may help me solving this issue. I am a newbie of Ruby so I am not sure its functional programming could make the difference.
Just like this:
parallel=falsesets the default argument value if no argument is passed.Or you could use an options hash to pass the parameters more explicitly
Another approach is to always run
do_work()in a thread and to wait for the thread to finish depending on theparallelparameter.