nik_jain Posted June 9, 2019 Share Posted June 9, 2019 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 Quote Link to comment https://forums.phpfreaks.com/topic/308829-functional-programming-emulate-the-array_map-function/ Share on other sites More sharing options...
Barand Posted June 9, 2019 Share Posted June 9, 2019 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 ) Quote Link to comment https://forums.phpfreaks.com/topic/308829-functional-programming-emulate-the-array_map-function/#findComment-1567471 Share on other sites More sharing options...
nik_jain Posted June 9, 2019 Author Share Posted June 9, 2019 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 ? Quote Link to comment https://forums.phpfreaks.com/topic/308829-functional-programming-emulate-the-array_map-function/#findComment-1567483 Share on other sites More sharing options...
Barand Posted June 9, 2019 Share Posted June 9, 2019 Depends on what you want to accomplish. Take your pick from these Quote Link to comment https://forums.phpfreaks.com/topic/308829-functional-programming-emulate-the-array_map-function/#findComment-1567484 Share on other sites More sharing options...
gizmola Posted June 10, 2019 Share Posted June 10, 2019 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. Quote Link to comment https://forums.phpfreaks.com/topic/308829-functional-programming-emulate-the-array_map-function/#findComment-1567499 Share on other sites More sharing options...
nik_jain Posted June 10, 2019 Author Share Posted June 10, 2019 (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 June 10, 2019 by nik_jain Quote Link to comment https://forums.phpfreaks.com/topic/308829-functional-programming-emulate-the-array_map-function/#findComment-1567504 Share on other sites More sharing options...
gizmola Posted June 11, 2019 Share Posted June 11, 2019 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. Quote Link to comment https://forums.phpfreaks.com/topic/308829-functional-programming-emulate-the-array_map-function/#findComment-1567528 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.