Posts

Showing posts from December, 2021

Map: Implementing map reduce with lambda functions (python)

Image
  The map function makes a transformation of each element of an array to a correspondent element in a new list that will be produced as output. The transformation of each element is handled by a function that is parametrized in the map function call. In this example below the function to perform the transformation is defined using the lambda syntax. names = [ "Peter Parker" , "Tony Stark" , "Natasha Romanova" ] # Using lambda function namesFormatted = list ( map ( lambda el: el.upper() , names))   The output of the map function is a Map object, then to handle its data as a normal list, it is possible to convert the Map object to an standard list using the list function, as presented in the example.   The complete code is available to be downloaded here: https://github.com/rafaelqg/code/blob/main/map_lambda.py You may see a video class about this video here:

Filter: Implementing map reduce with lambda functions (python)

Image
  The filter function is useful to reduce the amount of elements that there is in a list. It is quite performatic and composes part of map reduce techniques provided by python. The filter function basically receives two parameters. The first one is the function that will verify if a certain element keeps in the produce list or not, basically it has to compare a certain element with a criteria and if it returns true the element is preserved, otherwise not. The second parameter is the list to be filtered. The output of the filter function is another list with filtered elements. Important! The filter does not change the original list, but produces a new one. print ( "### FILTER ###" ) number_list = range (- 5 , 5 ) print ( "original list:" ) print (number_list) # Using normal function def biggerThanZero (x):     return x > 0 bigger_than_zero = list ( filter (biggerThanZero , number_list))   # filter(function, list) print ( "filtered list (bigger tha...

Spread Operator

Image
  The spread operator can be utilized both on arrays and objects. For arrays it will transform the array in a plain list of objects/variables, which can be utilized as input to functions that accept a list of arguments (example: array.push(el1, el2, el3). In This example if we have one array named array01 with options [1,2,3] and we want to add these values to another array, named array02, then we can do array02.push(…array01). Performing that, using the spread operator (…) actually the array02 will have a copy of all elements that are inside array01. Without using the spread operator , for example, array02.push(array01), the array02 will contains one single object that is the array01 itself, and only inside array01 that the values will be found. Another applicability for spread operator is for objects itself. When we want to have a new object with contains a copy of all attributes an existing object already have, in addition to new attributes, we can use the spread operator. L...