Incomprehensible condition and incorrect operation of the function

Viewed 94

There is the following code (demo here), the function (with recursion) go the matrix in a spiral collecting values:

function spiralmatrix($arr)
{
    return  $arr 
            ? array_merge(
                    array_shift($arr), 
                    spiralmatrix(array_reverse(transpose($arr)))
            )
            : $arr;
}

function transpose($arr)
{ 
    return $arr
            ? count($arr) == 1
                ? array_chunk($arr[0], 1) 
                : array_map(null, ...$arr)
            : $arr;
}

I understand that using multiple ternary expressions contributes to unreadable code. But there is more predominant sporting interest for code-Golf.

And so, to the question, is it possible to somehow change the condition to cut more characters? Can use any methods.

I would also like to ask, it is strange that the following works fine (demo):

function transpose($arr)
{ 
    return $arr
            ? count($arr) == 1
                ? array_chunk($arr[0], 1) 
                : array_map(null, ...$arr)
            : $arr;
}

And the expression works a little wrong (demo):

function transpose($arr)
{ 
    return count($arr) > 1 ? array_map(null, ...$arr) : $arr; 
}

Although, in separate tests (not in this function spiralmatrix) they show the same results what can is this problem? (demo comparisons). Grateful for any help..

1 Answers

The issue with your:

return count($arr) > 1 ? array_map(null, ...$arr) : $arr;

is that you are overlooking the truthy/falsey initial check:

return $arr
//      v----------------------------truthy----
        ? count($arr) == 1                    |
            ? array_chunk($arr[0], 1)         |
            : array_map(null, ...$arr)        |
        : $arr;                               |
//      ^----------------------------falsey----

Now, I'm not a code golfer, so I don't know if the spaces that you have posted "count" as characters. I don't know if when it's "game time", everyone smashes their code down and you are simply doing us a favor in making the code readable. That said here are a couple of ideas:

function transpose($arr)
{ 
    return $arr
            ? //count($arr) == 1
              //!isset($arr[1])                  <-- if spaces "count", this is shorter
              //!next($arr)                      <-- because no 0 in the arrays
              @!$arr[1]                  //      <-- the dirty stfu operator
                ? array_chunk($arr[0], 1) 
                : array_map(null, ...$arr)
            : $arr;
}

Now, I never use the stfu operator in my career, but if the gloves are off, maybe it's worth a look. Demo

I'll keep staring at the code and chime in if anything strikes me.

Edit: I just realized !next($arr) works because there are no zeros in the sample data.

Actually, why am I playing with negation, flip everything around. Duh!

function transpose($arr)
{ 
    return $arr
            ? //isset($arr[1])                  <-- if spaces "count", this is shorter
              //next($arr)                      <-- because no 0 in the arrays
              @$arr[1]                  //      <-- the dirty stfu operator
                ? array_map(null, ...$arr)
                : array_chunk($arr[0], 1) 
            : $arr;
}
Related