Why String is immutable in Java

  1. String values are stored in String constant pool. It can be accessed by multiple client.
  2. It is used by the class loading mechanism. For security reason, it should not be changed to other value.
  3. Immutable string allow to cache its hashcode. It makes hashmap/hashtable very fast since they don’t calculate hashcode everytime.
  4. String instance is thread-safe in Java.
Read More

Lambda Function in Java

Lambda expression replaces anonymous classes and removes all boiler plate, enabling you to write code in functional style, which is some time more readable and expression.

Lambda Expression vs Anonymous class

  1. Keyword this. For anonymous class this keyword resolves to anonymous class, whereas for lambda expression this keyword resolves to enclosing class where lambda is written.
  2. The compiled way. Java compiler compiles lambda expressions and convert them into private method of the class.
Read More

Lambda Function in Python

Sort in non-standard way

In python2

>>> a = ['floor', 'house', 'room', 'roof']
>>> a.sort(lambda x,y: cmp(len(x), len(y)))
>>> a
['room', 'roof', 'floor', 'house']
>>>

In Python3

>>> a = ['floor', 'house', 'room', 'roof']
>>> a.sort(key=lambda x: (len(x)))
>>> a
['room', 'roof', 'floor', 'house']
>>>

Closures - functions evaluated in an environment containing bound variables

In python2

>>> def comp(threshold):
...     return lambda x: x < threshold
...
>>> func_a = comp(10)
>>> func_b = comp(20)
>>> print func_a(5), func_a(9), func_a(15), func_a(25)
True True False False
>>> print func_b(5), func_b(9), func_b(15), func_b(25)
True True True False

In python3

>>> def comp(threshold):
...     return lambda x: x < threshold
...
>>> func_a = comp(10)
>>> func_b = comp(20)
>>> print(func_a(5), func_a(9), func_a(15), func_a(25))
True True False False
>>> print(func_b(5), func_b(9), func_b(15), func_b(25))
True True True False

Mapping - performs a function call on each element of a list.

In python2

>>> a = [1, 2, 3, 4, 5, 6]
>>> print map(lambda x: x*x, a)
[1, 4, 9, 16, 25, 36]

In python3

>>> a = [1, 2, 3, 4, 5, 6]
>>> [item for item in map(lambda x: x*x, a)]
[1, 4, 9, 16, 25, 36]

Filter - returns all elements from a list that evaluate True when passed to a certain function.

In python2

>>> a = [1, 2, 3, 4, 5, 6]
>>> filter(lambda x: x % 2 == 0, a)
[2, 4, 6]

In python3

>>> a = [1, 2, 3, 4, 5, 6]
>>> list(filter(lambda x: x % 2 == 0, a))
[2, 4, 6]

Reduce - The fold/reduce function runs over all elements in a list (usually left-to-right), accumulating a value as it goes.

>>> from functools import reduce
>>> a = [1, 2, 3, 4, 5, 6]
>>> reduce(lambda x, y: x+y, a)
21
>>>

This performs (((((1 + 2) + 3) + 4) + 5) + 6) = 21

Read More

Python OS Functions

The OS module in Python provides functions to using operating system dependent functionality.
To use it, import the OS module “import os

Read More

ZFS Dataset is Busy Solution

Solution on Ubuntu:
Using the following command and find with process is holding the dataset.

grep zroot/2013-10-15T065955229209 /proc/*/mounts

note: replace zroot/2013-10-15T065955229209 as your mount point.

Read More