Prevent MACBook Sleep

Some 3rd parties can prevent MacBook from sleep. Why does Apple always let MacBook/Air go to sleep when lid is closed and ignore user’s settings?

Read More

Fix Docker Exception - Credentials store error on MacOS

When I run docker-compose command on Mac OS, I hit an error as below:

docker.errors.DockerException: Credentials store error: StoreError('Credentials store docker-credential-osxkeychain exited with "User interaction is not allowed.".',)
Failed to execute script docker-compose
Read More

Nginx Proxy Server Configuration

Example 1:

server {
    listen 80;
    index index.html index.htm index.nginx-debian.html index.php;
    server_name aaa.bbb.ccc.ddd;
    location ~/app2(.*)$ {
        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_pass http://aaa.bbb.ccc.ddd:8001$1;
    }
}
Read More

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