Jump to content

Recommended Posts

This is just for fun/exploring.

The normal way of iterating over a array is using a for() loop. The functional way is to use array_map . I am trying to write/emulate the array_map function in a functional way.

Emulating the array_map using a for loop is easy:

function array_map_me_normal($callback,$arr){
    $returnArr = [];
    for ($i =0 ; $i < count($arr);$i++){
        $returnArr[] = $callback($arr);
    }
    return $returnArr;
}

But emulating array_map using a functional approach seems to be tricky. Not sure if its even possible. I think it should be possible..

function array_map_me_functional($callback,$arr){
    if (empty($arr)){
        return false;
    }
    return [$callback(array_pop($arr)), $callback($arr)];
}

The above is incorrect ofcourse. Returns increasing nested array. But I couldn't do better. Is there a way ? or will an array_map function always be using for loop under its hood ?

Thanks

3 hours ago, nik_jain said:

The normal way of iterating over a array is using a for() loop.

I'd argue that the normal way is to use foreach().

 

try

$arr = [ 2, 3, 4 ];

function square($n)
{
    return $n * $n;
}

function my_array_map($callback, $a )
{
    $new = [];
    foreach ($a as $k => $v) {
        $new[$k] = $callback($v);
    }
    return $new;
}

$new = my_array_map("square", $arr );

print_r($new);                              //  Array ( [0] => 4 [1] => 9 [2] => 16 )

 

Is this an academic exercise, because PHP has an array_map function as well as numerous other higher order functions including the popular array_reduce.  These are all in the list linked by Barand in his response.

yeah I know about array_map. Hence why I mentioned I wanted to emulate it. I guess it is some kind of 'academic exercise' . I guess I should explain more clearly what I am thinking here.

 I was reading about on functional programming , one example focused on iterating an array. Where the imperative/normal  way used a for loop to go through the array, and the functional way used the array_map function. This made me think how the array_map function internally scans the array. Does it use some functional way or does it use a for loop internally .

Edited by nik_jain

array_map is part of the runtime library, which is to say that the source code for the function is part of a c library.  It actually looks at a few different conditions, as array_map can be called without a callback function, and can have 1-n arrays as parameters, but essentially, yes it simply does a for() loop through the array(s) and executes the callback for each iteration of the loop.

We could get even more meta, and ask the question as to what the machine code looks like for a for loop, which is typically a conditional check and jump instruction.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.