Jump to content
nik_jain

Functional Programming. Emulate the array_map function

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

Share this post


Link to post
Share on other sites
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 )

 

Share this post


Link to post
Share on other sites

I do not know the technical differences between for and foreach, but whichever is 'normal' here , it is not functional ? If so, is there a functional way of iterating an array that does not use loops ?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
Posted (edited)

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

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.