Lambda functions and Parallel programming

Greek numeral representing the value of 30?The unit representation of wavelength?All these are right but in computing, lambda means more.

Lambda function(or expressions) provides a quick way for declaring small anonymous functions.

Anonymous function is a function that is defined without a name.

Normal functions are defined using the def keyword followed by the name of the function (e.

g.

square — which would square whatever number is supplied as the argument.

Lambda functions are can be found in programming languages such as Ruby, Python, Java and JavaScript but our programming language of focus is Python.

In Python, anonymous functions are defined using the lambda keyword.

The basic syntax for lambda functions is in Pythonlambda arguments: expressionAn example of lambda functionlambda x : x > 0where lambda — is a lambda function, x: — the parameter name within the function, x>0 — what to do with the parameter.

In Python, lambda functions is generally used as an argument to a higher-order function (a function that takes in other functions as arguments).

Lambda functions are used along with built-in functions like filter(), map(), apply() , etc.

The filter() function takes in a function and a sequence(could be sets, lists, tuples,or containers).

Each element in the sequence is tested to be true or not and returns in a new sequence which contains items for which the function evaluates to True.

The map() function takes in a function and a iterable (list, tuples, etc).

The function returns a new list which contains items returned by that function for each item of the given iterable.

The apply() function takes a function and applies it to every single value of the Pandas series.

During the course of the Gitgirl Data Science programme I enrolled in, a curriculum covering lambdas functions was given and it was required to solve some questions .

A record of United States domestic flights from the US Department of Transportation from January 1–15 of 2015 was provided to achieve this.

Example of lambda function using the .

apply()Parallel ProcessingParallel computing is the future of programming and is already paving the way to solving problems that concurrent programming consistently runs into.

Although it has its advantages and disadvantages, it is one of the most consistent programming processes in use today.

Parallel processing is a mode of operation where the task is executed simultaneously in multiple processors in the same computer.

According to Wikipedia, parallel computing is closely related to concurrent computing — they are frequently used together, and often conflated, though the two are distinct: it is possible to have parallelism without concurrency (such as bit-level parallelism), and concurrency without parallelism (such as multitasking by time-sharing on a single-core CPU).

In Python, the multi-processing module is used to run independent parallel processes by using sub-processes (instead of threads).

The maximum number of processes you can run at a time is limited by the number of processors in your computer.

To know this, the cpu_count() function is used.

The advantages of parallel processing(programming) over concurrent programming are:it reduces overall processing time, increases both the efficiency and resources used in order to achieve quick results.

it is more cost efficient because it takes less time to get the same results.

In parallel processing, there are two types of execution:Synchronous — here, the processes are completed in the same order in which it was started.

Asynchronous — here, the order of the requests can get mixed up but usually gets done faster.

There are 2 main objects in multiprocessing to implement parallel execution of a function:Pool Classa.

Synchronous executionPool.

map() and Pool.

starmap()Pool.

apply()b.

Asynchronous executionPool.

map_async() and Pool.

starmap_async()Pool.

apply_async()2.

Process ClassParallelizing any functionThe general way to parallelize any operation is to take a particular function that should be run multiple times and make it run parallelly in different processors.

To do this, you initialize a Pool with n number of processors and pass the function you want to parallelize to one of Pools parallization methods.

To learn more about parallel processing, click these links:Parallel Processing in Python — A Practical Guide with ExamplesHow pararrel processing works.

. More details

Leave a Reply