Wednesday, November 12, 2014

Map, Reduce and Filter in Python

Python Supports Functional Programming using Lambda. Python treats functions as first-class objects. It can be used as variables.


>>> def sum(x,y) :
...     return x + y
... 
>>> sum(2,4)
6
>>> mysum = sum
>>> mysum(34,45)
79
>>> sum
<function sum at 0x7f6e5c7135f0>
>>> mysum
<function sum at 0x7f6e5c7135f0>


Lambda allows us to create a short "Anonymous" functions. For example "(lambda x, y: x+y)(3,4)" will return 7.

In this post, we will be looking into 3 function(filter, map, and reduce) from an functional point-of-view:
  • filter(filtering function object, list) 
  • map(mapping function object, list) 
  • reduce(reducing function object, list) 

Filter function:
The filter() call filtering function for each element in the given list and selects the element based on the return value of the filter function.


>>> num = range(1,100)
>>> filter(lambda x: x%5 == 0, num)
[5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95]
>>>


Map Function:
The map() function applies mapping function for each element in the given list and returns the mapped resultant list.


>>> num10 = range(1,11)
>>> num10
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> square10 = map(lambda x: x**2, num10)
>>> square10
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
>>>


Maps are really useful when you want to extract a particular field from an array of Dictionary.

>>> dictList = [ {'FirstName': 'Michael', 'LastName': 'Kirk', 'SSID': '224567'},
... {'FirstName': 'Linda', 'LastName': 'Matthew', 'SSID': '123456'},
... {'FirstName': 'Sandra', 'LastName': 'Parkin', 'SSID': '123456'},
... {'FirstName': 'Bob', 'LastName': 'Henry', 'SSID': '666666'},
... {'FirstName': 'Silvia', 'LastName': 'Perkin', 'SSID': '676767'}]
>>> dictList
[{'LastName': 'Kirk', 'SSID': '224567', 'FirstName': 'Michael'}, 
{'LastName': 'Matthew', 'SSID': '123456', 'FirstName': 'Linda'}, 
{'LastName': 'Parkin', 'SSID': '123456', 'FirstName': 'Sandra'}, 
{'LastName': 'Henry', 'SSID': '666666', 'FirstName': 'Bob'}, 
{'LastName': 'Perkin', 'SSID': '676767', 'FirstName': 'Silvia'}]
>>> map(lambda x: x['LastName'], dictList)
['Kirk', 'Matthew', 'Parkin', 'Henry', 'Perkin']
>>> 


Reduce Function:
The Reduce function applies the reducing function for the first pair of the list and then applies reducing function  again with the resultant with the next element in the list, so on till all the last element of the list and returns the last resultant.


>>> num = [2, 4, 5, 3, 7, 9, 8, 3, 1]
>>> reduce(lambda x, y: x if (x > y) else y, num)
9
>>>
>>> reduce(lambda x, y: x if (x < y) else y, num)
1
>>> reduce(lambda x, y: x + y, num)
42
>>> reduce(lambda x, y: x * y, num)
181440
>>>>>> def factorial(num):
...     return reduce(lambda x, y: x * y, range(1, num+1))
...
>>> factorial(1)
1
>>> factorial(2)
2
>>> factorial(3)
6
>>> factorial(4)
24
>>>
>>> def getCount(num, list):
...     return reduce(lambda x, y: x+y, map(lambda x: 1 if x == num else 0, list))
...
>>> getCount(5, [1,2,3,4,5,6,7])
1
>>> getCount(5, [1,2,5,4,5,6,7])
2
>>>

Thursday, February 27, 2014

Vifm - GTD

Vifm - Vi File Manager is a wonderful utility to navigate files. I have created a wrapper for GTD.

My vifm colorscheme:
COLORSCHEME=Default
DIRECTORY=/
COLOR=MENU=black=-1
COLOR=BORDER=black=white
COLOR=WIN=black=-1
COLOR=STATUS_BAR=blue=-1
COLOR=CURR_LINE=white=blue
COLOR=DIRECTORY=blue=-1
COLOR=LINK=yellow=-1
COLOR=SOCKET=magenta=-1
COLOR=DEVICE=red=-1
COLOR=EXECUTABLE=green=-1
COLOR=SELECTED=magenta=-1
COLOR=CURRENT=blue=-1

My Commands:
COMMAND=ar=mv %f /home/reemuskumar/gtd/Archive
COMMAND=cr=echo 'newfile' > %d/%a
COMMAND=daily=daily.sh %m
COMMAND=ddr=rm -rf "%d/%f" %m
COMMAND=dr=mkdir %d/"%a"
COMMAND=hello=echo 'hello world'

Tuesday, January 21, 2014

Code Snippet in Blogspot

I use the following code CSS to post the code snippets in this blog:

I use the following code CSS to post the code snippets in this blog:
<code style="color: black; word-wrap: normal;">
<! ... Your Code Here .... !>
</code>


Sunday, January 12, 2014

Programming Style

Every programmer will have a style of programming, the way they write the programs. But, when you work as a team, it would be very useful if you and your team follow one 'programming style' for code readability and consistency. 
Here are some of the standard practices:
  • Adhere to the existing style
    • If a source file already has a style, please adhere to the existing style
  • Indent Code Blocks
    • Use 4 space indented code blocks, making it 4 spaces make room of more code within 80 columns.
    •  Use expanded tabs. By this way your code won't look different in different environments
  • Follow only one brace-opening style
    • C++ Style
      if.(...).{
       ... 
      }
    • C Style(K&R)
      if.(...) 

      ... 
      }
  • Use White-spaces between keywords, variables, operators for better readability
    if.(x.>.10).{ 
    ... 
    }
  • Use a Standard Naming Convention
    • Use uppercase for macros
    • Add prefix for macros within a module to avoid conflict with global
    • "UpperCamelCase" for classes, structs, enums, constants and typedefs (use nouns)
    • "lowerCamelCase" for functions, variables, parameters  even for abbreviations (use verb for function names)
    • Use 'get', 'set' - function prefixes. 'is' prefix for functions which returns Boolean(true or flase) value
Reference: Read "The Elements of C++ Programming Style."