Category Archives: Uncategorized

Anonymous functions across languages Erlang Python

Anonymous functions originate form the work of Alonzo Church in his invention of the lambda_calculus, in which all functions are anonymous. Anonymous functions are not bound to any identifier.

Erlang is a functional programming language. This means functions can be passed as arguments to functions and functions can return functions.

Functions that manipulate functions are called higher order functions and data type that represents a function in Erlang is called a fun.

fun are anonymous functions in erlang such as lambdas in python.

Example

Erlang
Square=fun(X)->X*X end.
Square(5).
25

Python:
>>> Square=lambda X:X*X
>>> Square(5)
25
Note in python definition of the lambdas, the arguments don’t have parentheses around them. 

Funs can have several different clauses. Kilometer to mile and mile to kilometer function

DistanceConvert = fun({kilometer,Km})->{miles,Km * 0.62137};
                   ({miles, M})->{kilometer,M/0.62137}
                  end.
#Fun<erl_eval.7.126501267>

DistanceConvert({miles,5}).
{kilometer,8.046735439432222}

DistanceConvert({kilometer,8.04673}).
{miles,4.999996620}

Functions That Have Funs As Their Arguments


L=[1,2,3,4,5].               
[1,2,3,4,5]

lists:map(fun(X)->X*X end,L). % Here map gets fun functions as argument
[1,4,9,16,25]



Even = fun(X) -> (X rem 2) =:= 0 end.
#Fun<erl_eval.7.126501267>

lists:filter(Even,L).                                           
[2,4]

Generators across languages

Erlang:

In the code below V<-[1,2,a,3,4,b,5,6] is a generator expression and V > 2 is a filter
[V || V <- [1,2,a,3,4,b,5,6], V > 2].
[a,4,b,5,6]


Simple example:
input to quicksort is a list and splitting the first element as Pivot and rest of list as T

quicksort([Pivot|T])->
  [X || X<-T, X < Pivot]++ % filter elements less than pivot  
   [Pivot]++.               % plus pivot
  [X || X<-T,X >= Pivot];   % filter elements greater or equal to pivot
quicksort([])-> [].        % base case if the list is empty

Python:

Generators are special functions which return lazy iterators which can be looped like a for loop except that generators do not store contents in memory but a normal list stores all the elements in memory.

Typical use of generators is when you have huge streams of data or operations with huge file.

Two ways of using generators:

  1. generator expressions
  2. generator functions

For small lists it is better to use regular list comprehension only for bug list there will be a memory usage.

Simple generator expression:

S=[ i * i for i in range(5)] #normal list comprehension of squares

 print(s)

[0, 1, 4, 9, 16]

sys.getsizeof(s)

120

S=(i*i for i in range(5)) # generator expression

sys.getsizeof(s)

112

[i for i in s]

[0, 1, 4, 9, 16]

Above you can see there is no much difference in memory usage

Simple infinite generator function:

 def gen_infinite_sequence():
                  no=0
                  while True:
                      yield no
                      no+=1
    

Generators can be exhausted if the underlying data is limited:

 def usage_finite_generator(finite_list):
          it=iter(finite_list) # iter 
          while True:
              try:
                  print(next(it))
              except StopIteration as e:
                   break

usage_finite_generator(['s','u','b','b'])
s
u
b
b

Things you cannot control……

I am scribbling after a long time..

There are things in life that you cannot control even if you try to control..

In my case i did not try to control or remained life to pass over me ..In short. i was a dead meat to life’s events ..

Now Story Time:

I have always loved someone in life  during every stage of life.  Back 10 years ago, We were a middle class family and my father and mother had struggles to meet both ends. As a young kid , it had little effect on me. My parents often had fights and it was my brother who understood the reality .

He understood the financial struggles at home . My father used to shout at my mom and brother when he had a bad day at office or he had some tension. It was not intentional but he was helpless. My father and Mother married at thirties and when my brother was studying 12 , Voluntary retirement scheme was a way that corporate would remove employees from work. My father was not always at home. He worked 24 *  7 and he had little time for himself. He had the worry to bring me and my brother up in this monetary world.  My brother and my mom took care of me. They had me enrolled in best school . I had tuition classes extra. It was mainly because my brother studied in a not so good government school that he wanted me to have the best education.

It was during my school days that my mom used to take me to school via a house with lily flowers. Since we stayed at a small house and we were at a considerable distance  my mom used to walk and bring lunch for me.

It was  later we shifted to the same house with lily flowers for RS.1000. It was a huge money for us. My mom once told me that once  while bringing lunch to me earlier that she had wished that if we stayed at the house with lily flowers,the school would be near and she could bring hot lunch to me and it became a reality. She told me if you wish something sincerely, it would come to you ..may be not early but definitely you will get it.

Why all this story… Today i heard the girl i liked very much in college is getting married. its now 5 years after college. Firstly I did not have guts to say that i like her and also i did not want to upset my parents who gave everything they had to bring me up. Even now my brother is looking after my needs and i am grateful for that. I usually fight with my brother but at my heart i have always loved him and there is no single soul as him in this world who puts me before everything even his very own life.

I did not take the effort to convey my liking for her  or work for it. But at the back of my heart, its always there and will be ..

It just looks silly from an external person viewpoint as it is part and parcel of life but the feeling i bear is not conveyable .No matter what Life goes on..

Do not know what i expect out of life..

 

I have wanted to find free time for doing the activities i like to do.. But when i find time i do not do things that i wanted to do . Not quite sure of my devious mind..there are times when we can distinguish ourselves from the ordinary. 

I have myself still searching why i am such an erratic, idiotic, lazy and stupid guy. Yet i like me as myself. I have hope in me that i will one day be “great”. Oh i have define the word i fantasize most “Greatness”. Ever since i started seeing the world in its charm, i have always wanted to be a nerdy,highly sensible , great lover and one and only genius. As days went by i knew i am not even close to being an ordinary human being. What to do,reality is bitter. I do not have guts to say to my heart that i am not that greatness material i had always wanted to be. Yet there is this sense of some thing will work out .

Ultimately its all gets to what i want from my life which is constantly changing by every breath i take in and out. The changing goal is so rapid that i hate myself .

But today i learnt that if i wanted something, i have to spend time on it. Not the mere desire and gravity of wish will give the fruit falling on my hands.

A step towards happiness

I was today watching a video (*https://www.youtube.com/watch?v=irornIAQzQY) on happiness. Its quite interesting to know that most of us really do not know what makes us happy.Yes i too disagreed at first but later i could logically conclude my idea of happiness is just a substitute i have created for my real happiness

.According to Epicurus,an ancient philosopher, the simple ingredient of happiness is

a) Friends

b) Freedom – economically independent and self sufficient

c) Analysed life- Time to think about our worries and reflect on our self.

 

 

 

The most important things that we care for ..?

As human beings… I still cannot understand the full meaning of that word as i feel it encompasses so much …

Yeah i almost forgot what i wanted to convey….. It happens all the time Never mind. As creatures of nature on this planet, we always care for things that we feel precious and that affects us in some way..

We care for things that affect us like a relationship , Money, Pride, Love ….Isn’t it so selfish to care about things or people that only affect us.So i feel every human being here in this not so holy planet is selfish.  The classic example being me i always call somebody when i feel lonely..You may say don’t judge the world by the standards you have set for yourself.. Or you may say not everyone is like myself. True I agree but only to an extent from my experiences..

I used to work for an IT firm in bangalore In  india .. There used to be a blind guy reaching out for help. He would have been a better human being if god had given him eyesight.. He was so courteous that he would not even trouble the office goers for help though he could have.. He would stand in the same place all day in scorching daylight till evening except for occasional eating..

I always wonder why he has been punished … Though i  don’t deserve the comfort that god has gifted me … I feel selfish to lead my own self caring life though i can see not everyone has been gifted. Also my proud fellow human beings who used to go pass him never helped him lead his life better but only took pity on him and gave him a meager rupee for his meal…

I consider giving alms as disrespecting another fellow human being.. I used to get him bundles of hot idly(Indian food) but i always feel a remorse in me that i am selfish to lead a carefree life..

 

How did today go? Is always a question for me from childhood

As a child, i always wanted to be be great and heroic …and I came to know to know if i had to become great in something i have to devote time and energy towards it. I always wanted to concentrate on one goal and study for night night together without any need for food or water…But till today i have never spent a whole day and night for good productive  purpose.The reason being when i had time i wasted it and when i did not have time i rather shouted at GODS…One or the other i have always been wasting time and energy..

 

But i do not want to give up hope on me… I know that i will become great one day …

So i lay the foundation for my future Greatness today…

Hope that the day is well spent…