.map() and array_map() and list comprehensions

Posted on: August 14th, 2023
By: Tadeo Martinez

Solving the following Codewars challenges today I learned about .map() for both JavaScript and PHP and learned about list comprehensions in Python.

Code examples

Here’s an example of list comprehension used in Python:

import math

def squareOrSquareRoot(numbers):
    return [math.sqrt(x) if math.sqrt(x).is_integer() else x * x for x in numbers]

List comprehensions are often praised for their readability, as the transformation logic is directly visible in the comprehension. In this case, the list comprehension approach clearly shows the conditional transformation for each element.

Here’s the equivalent of the function in Python in JavaScript:

function squareOrSquareRoot(array) {
  return array.map(x => {
    const r = Math.sqrt(x);
    return (r % 1 == 0) ? r : (x*x);
  });  
}

I didn’t use the array_map() as I learned about it after solving it in PHP but here’s the code I wrote using a for loop:

function squareOrSquareRoot($array) {

  $newArr = [];

  for ($i = 0; $i < count($array); $i++) {
    $sqrt = sqrt($array[$i]);
    if(intval($sqrt) == $sqrt) {
        array_push($newArr, intval($sqrt));
    } else {
        array_push($newArr, pow($array[$i], 2));
    }
  }

  return $newArr;
}

Here would be the equivalent using array_map()

function squareOrSquareRoot($array) {
    return array_map(function($value) {
        $sqrt = sqrt($value);
        if (intval($sqrt) == $sqrt) {
            return intval($sqrt);
        } else {
            return pow($value, 2);
        }
    }, $array);
}

Anonymous functions

An anonymous function is a function that doesn’t have a name associated with it. It’s a type of function that is defined without a formal identifier. Instead, it’s typically used inline as an argument to other functions, assigned to variables, or used within a specific context where a function is needed temporarily.

In programming languages like JavaScript, anonymous functions are often used for tasks such as passing functions as arguments to higher-order functions (like array methods .map(), .filter(), etc.), defining callback functions, and creating closures.

Arrow functions

Arrow functions are a JavaScript-specific feature introduced in ES6 (ECMAScript 2015). They provide a more concise syntax for defining functions compared to traditional function expressions. Arrow functions also have a special behavior with respect to the this keyword, as they capture the value of this from their surrounding context.

Inline Functions (JavaScript, PHP, Python)

The term “inline function” is a more general concept that can apply to various programming languages, including JavaScript, PHP, and Python. An inline function typically refers to a function that is defined within an expression or statement, rather than being declared as a separate named entity. While JavaScript uses arrow functions as a way to define functions inline, PHP and Python offer similar functionality using anonymous functions (closures in PHP and lambda functions in Python).

  • JavaScript: Arrow functions can be considered inline functions due to their concise syntax and ability to be used within expressions.
  • PHP: PHP supports inline functions in the form of anonymous functions (closures), which can be assigned to variables or used directly within function calls.
  • Python: Python supports inline functions in the form of lambda functions, which are used for simple, one-line operations and are often used as arguments to functions like map(), filter(), and sorted().

While the syntax and specific characteristics might differ, the concept of inline functions exists in various programming languages, including JavaScript, PHP, and Python.

Codewars challenges

Draw stairs

https://www.codewars.com/kata/5b4e779c578c6a898e0005c5

To square(root) or not to square(root)

https://www.codewars.com/kata/57f6ad55cca6e045d2000627

Have any questions or comments? Write them below!


Leave a Reply

Your email address will not be published. Required fields are marked *