Functional Programming in Python with Lambda Map Reduce and Filter

Functional programming in Python is possible with the use of lambda map reduce and filter functions. This article briefly describe use of each these functions.

Lambda : Lambda specifies an anonymous function. It is used to declare a function with no name; When you want to use function only once. But why would you declare a function if you don’t want to reuse the code? Read on you’ll see. Syntax: lambda arg1, arg2 : expression

lambda x : x*x

This lambda expression with just one argument x which returns square of x.

Map : It takes two arguments, the first argument is name of a function and second argument is a sequence. map() applies function f to all elements in the sequence and returns a new sequence. Syntax: map (func, sequence)

1
2
3
4
list = [1, 2, 3]
map (lambda x : x*x, list)

#output: [1, 4, 9]

This code also demonstrates use of lambda. Instead writing a square function we substituted it with a lambda expression. map() applies it to all elements in the list and returns a new list with each element square of original element.

Reduce: reduce() continuously applies a function to a sequence and returns one value. In the following example we sum all elements in the original list. Syntax: reduce (func, sequence)

(lambda x,y : x+y, list)
1
#output: 6

Filter: It filters all values in a sequence for which given function returns True. Syntax: filter (booleanFunc, sequence)

1
2
filter (lambda x : x%2, list)
#output: [1, 3]

Above example returns all odd integers in the list. Remember 2%2=0 is treated as boolean value False.

Hello World in 10 Different Programming Languages

Writing Hello World is a custom I dare not to break as I start off with this brand new blog. Here are Hello World programs in ten different programming languages.

1. The C Not only C can be termed as mother of most high level languages but it remains relevant till date. It is undoubtedly the most popular language of all times.

1
2
3
#include void main() {
printf("Hello World!");
}

2. C ++ Bjarne Stroustrup’s language gave programmers a new world view with Object Oriented Programming.

1
2
3
#include void main() {
cout << "Hello World!";
}

3. UNIX Shell Shell is like a Ninja’s sword! You need to see this.

1
2
#!/bin/sh
echo "Hello World!"

4. Java Java is perhaps the only ubiquitous multi platform language we have seen.

1
2
3
4
5
public class HelloWorld {
public static void main(String args \[\]) {
System.out.print("Hello World!");
}
}

5. Hello World in C# C# is a great programming language based on C from Microsoft.

1
2
3
4
5
public class HelloWorld {
static void Main() {
System.Console.Write("Hello World!");
}
}

6. JavaScript JS shaped platforms which employ half of the Indian programmers.

1
alert ("Hello World");

7. PHP PHP makes great front-ends for web applications.

1
echo "Hello World!";

8. Python My favourite! Python is simple, readable and powerful.

1
print ('Hello World!')

9. PL/SQL When you cannot think in SQL, you use PL/SQL!

1
2
3
begin
dbms\_output.put\_line ('Hello World!');
end;

10. Arduino C Arduino propels IoT development with affordable open source prototyping boards for everyone. Arduino-C is indeed C!

1
2
3
4
5
6
7
8
9
10
11
12
13
// setup () function runs only once
void setup() {
// Use on-board LED as output
pinMode(LED\_BUILTIN, OUTPUT);
}

// loop function runs forever
void loop() {
digitalWrite(LED\_BUILTIN, HIGH); // turn the LED on
delay(1000); // wait a sec (or 1000 ms)
digitalWrite(LED\_BUILTIN, LOW); // turn the LED off
delay(1000); // wait a sec (or 1000 ms)
}

So this is how journey at iDevji starts, I hope it will create value, may be little, for everyone specially my students!