4
\$\begingroup\$

This, again, is a very simple code.

This is my 2nd polyglot question and this time I've tried to do something that I miss sometimes in Javascript.

PHP has the array_sum() function, but Javascript has no direct equivalent.

So, I decided to make one that works with both languages!

Here is the code:

function sum_array()
{
    if( $javascript = '\0' == "\0" )
    {
        $vars = new Object();
        $vars['args'] = arguments;
        $vars['l'] = arguments.length;
    }
    else
    {
        $vars = Array();
        $vars['args'] = func_get_args();
        $vars['l'] = func_num_args();
    }

    for( $vars['sum'] = $vars['i'] = 0; $vars['i'] < $vars['l']; $vars['i']++ )
    {
        $vars['m'] = $javascript ? $vars['args'][$vars['i']].length : count($vars['args'][$vars['i']]);

        for( $vars['j'] = 0; $vars['j'] < $vars['m']; $vars['j']++ )
        {
            $vars['sum'] += ( $vars['args'][$vars['i']][$vars['j']] / 1 ? $vars['args'][$vars['i']][$vars['j']] : 0 );
        }
    }

    return $vars['sum'];
}

Usability is really easy:

Simply pass multiple arrays as arguments. In PHP, it is required to be an array with only numeric keys. This is due to the limitations of Javascript.

Example of usage:

sum_array([1,2,3,4,5],[15]); //should return 30 in Javascript and PHP 5.4+

To avoid 'poisoning' the window object, I only created 2 variables and use one of them as an array (The other is just to save if we are running in Javascript or PHP).

Was this a good decision?

What else can I improve on my code?

\$\endgroup\$
3
\$\begingroup\$

Interesting question.

You are trying to avoid poisoning the window object.. Now consider writing several functions like this. You just moved the poisoning from window to $vars. I would not do this because it reduces readability a lot.

$vars['args'][$vars['i']] should be in a helper variable to increase readability. (Regardless of whether you keep $vars)

I would have put this in a function called get_array_length:
$javascript ? $vars['args'][$vars['i']].length : count($vars['args'][$vars['i']]);

Then I would change the top to

if( $javascript = '\0' == "\0" )
{
    $vars = new Object();
    $vars['args'] = arguments;
}
else
{
    $vars = Array();
    $vars['args'] = func_get_args();
}
$vars['l'] = get_array_length($vars['args'])

Still, all in all because you need to maintain compatibility this is okay as an art excercise but this is by no means (because of it's nature) production or high quality code.

\$\endgroup\$
3
  • \$\begingroup\$ I'm really sorry for the delay in commenting. I was interrupted a few times. I like your idea of using a helper function. I aprove that! About the variable, I agree that it isn't the cleanest way. And yes, my intention isn't to use it in production but simply as an exercise to practice a little on this 'art'. This is nowhere close to an overkill multi-language polyglot. I really appreciate your help. But the only thing I don't know is how to make local variables in Javascript without using the keyword var (which causes syntax errors in PHP). But maybe it is impossible. Once again, thank you! \$\endgroup\$ – Ismael Miguel Mar 31 '15 at 19:34
  • \$\begingroup\$ As far as I can tell it is impossible. I wouldnt worry about it. \$\endgroup\$ – konijn Apr 1 '15 at 12:57
  • \$\begingroup\$ As far as I can tell too, it is really impossible. Maybe some eval() voodoo would work? \$\endgroup\$ – Ismael Miguel Apr 1 '15 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.