Monday, September 08, 2014

std::move makes things f**kable

After spending sometime figuring out C++ 11 move semantics,
I wanted to write some semi-formal intuitions about it,
for anyone encountering bugs related to it.

(Google will bring you to more precise, thought-out descriptions than this one for sure)

The most important thing:
std::move(A) does not actually move A! it just tells the compiler - from now on
you can fuck with A.

How does it do this?
It turns A into the most insignificant type of creature in the world of C++.
The type of creature that does not even have a name...
This creature is called - an rvalue. r stands for `right' - not as in correct , but as in the opposite of left.

What are rvalues?
Think of the expression
i = 5;

any programmer is aware that i is something that exists, there  is a place in the memory
that the computer calls i, and that place can store a value.

But what about the 5 on the right hand side?
where was it before it got put into i?
It was in some temporary place in the memory that does not even have a name.
A place that the computer feels free to treat like shit.
Will the 5 still be there after it is put into i?
Nobody gives a fuck.
Nobody's looking out for that place.

This is what it's like being an rvalue.

Sadly, in the world it sometimes pays off to treat people, or variables, like shit.

Suppose you had an expression

A=B;

The compiler would probably decide this means it has to copy all the data stored in B
and store this copy in A.

Suppose we don't care that much about what will happen to B after running this line.
We can tell the compiler - feel free to ruin B while running this line if it pays off.
e.g. if B contains a pointer to some large object, it would be very efficient
to have A point to the same object, and set B's pointer to null, rather than making
a copy of the large object.

writing
A= std::move(B);
will encourage the compiler  to do this.

How does this actually happen?

I like to think of it like this.
Suppose you have a prison named `operator ='

In this prison the are 3 wards -
One for the rich prisoners where they can be sure nothing bad will happen to them.
One for the regular prisoners where, bad things may happen to them, but no one will intentionally
try to screw them over,
and one for the prisoners someone had it in for, and wanted to make sure they would really get screwed.

The entrances of the wards look in C++, respectively, like this

1.operator=(const object& B)
2.operator=(object& B)
3.operator=(object&& B)


The double && is a way to tell C++ - B is an insignificant little worm! (that is, an rvalue). Feel free to tear him apart while running this operator!

The well-known `const' on the other extreme is a way of saying -
don't you dare touch B!

using move can become crucial when dealing with the specie in C++ 11 that's on top of the food chain - the guy nobody can touch.. the Unique Pointer.

A unique pointer in a way, is even tougher than the untouchable const object.
As the name suggests, you cannot even make a copy of a unique pointer.
(This turns out to be extremely useful for preventing memory leaks)

The big mess is that, not only the unique pointer itself, but consequently any thing
containing a unique pointer cannot be messed with. For example, a vector of unique pointers
to String.

For unique pointers, std::move is a way for the gang leader (i.e. the programmer)
to tell the gang's representative in the prison (i.e. the compiler)
`This guy no longer has my protection, you can make him your bitch'

The thing is, the gang representative will hesitate about messing with the unique pointer
if he is not constantly reassured by the gang leader that the u.p really does not have his protection any more.
This hesitation will cause compiling errors.

For example,
Suppose you have a method containing a line
A=B;
where B is a u.p the method got as a parameter.
You may think if I call the method with std::move(B) everything will be fine.
(cause a u.p cannot be copied, but it can be moved)
but that is not enough!Cause by the time you get into the method the compiler will hesitate
whether he can really mess with B,
and you will have to reassure him by writing again
A=std::move(B);

This is worst when calling constructors, where for reasons I do not fully understnad
you need to use std::move(B) in the initializer list - by the time you reach the
start of the method it's too late to mess with B. (well this last part is not completely correct,
but I'm tired and it's late)

Tuesday, September 02, 2014

LIFT

The weird list of 4 things that throughout years has consistently shown to have
a boost on my mood if I do them daily

-Avoid video - TV, youtube, ect.
-Avoid Masturbation.
-Hit on beautiful girls (oddly, this in itself has a more positive effect on my mood,
than having sex with them (in the rare occasions it ends up that way))
-Meditating 2 hours a day

I've been confused by this list.. cause there seems to be some kind of a contradiction between 3 and 4.
Cause it's kind of like taking one step in the direction of eliminating desire,
and one step in the direction of igniting desire.

Related to that , an analogy I had on tantra:
Think of 2 scenarios
1. You go into mcdonalds sweating in the middle of the day in shorts and a t-shirt
and get a Big-Mac

2. You go to a fancy restaurant, dressed perfectly, for a candlelight dinner, with
a wonderful woman who is also dressed amazingly. There is classical music in the background
and you have a wonderful view of the city from a window.
You are enjoying a tender steak of very high quality cooked by a gourmet chef.

You could say the second experience is more `spiritual' than the first,
but both experiences have at their core something that is non-spiritual  - eating meat.


Similarly, Tantric breathing exercises, postures, ect. can enhance the sexual experience,
and  given that you are going to have sex, why not indeed make it a special experience and a bonding experience with your partner;
but to say it is a spiritual practice when at it's core it's based on sexual desire -  the precise thing that spiritual practice is supposed to gradually dissolve, is misleading in my opinion.


btw If I had choose to #5 on this list it would probably be physical exercise , something I am only doing once a week lately, specifically Pilates in the last few weeks.

Anyway I digressed.
As for #2 on the list, for a long time I could keep it under control using a combination of meditation,
and this website http://www.stickk.com/
where you can define goals and even put money on the line.
But at a certain point I began to not take what I wrote on the site too seriously
(the credit card # I gave them also expired by the time I stopped meeting the goal, so didn't lose money)
But recently, Tim Ferriss created a community of guys trying to meet the no M goal -
actually he defined it as NOBNOM - no booze no masturbation, but I haven't been into alcohol for many years anyway -
turns out a lot of guys find this having tremendous positive effect.
This community of guys holding each other accountable has helped finally get back on the wagon
in the M category.
I recommend looking at the lift site.
This concept of choosing a few things that have positive effect,
and seeing the accumulative effect of doing the consistently (even ,say 85% consistently)
for months or years is simple and poweful..
and so different from the one I feel I was raised on by the media..
where most of the time it doesn't matter what you do,
but there are just a few critical moments, e.g., in Back to the Future where Marty's dad punches Biff
that determine everything.

Subconciously, this encourages us to spend most of our time doing nothing cause we think
all there is to do is watch TV and wait for that fateful moment when we open a car door and a guy is getting rough with the girl of our dreams, and we have to save our energy for that one punch we'll have to deliver.

I really like the phrase `Luck is when preparation meets opportunity'


Monday, September 01, 2014

I try to build a whole world from a few words you've given me.
 I'm hanging on a distant echo trying to hear what seemed so pure and right.
 And no matter how much time passes, when I start to listen a huge wave comes towards me. One particle containing the whole universe.
Poking at it, thinking it is small and weak now, it answers back, almost screaming:
You cannot deny this bond!!
It's power hasn't gone anywhere, just because you tried to forget it.

Monday, August 18, 2014

Steve Jobs: Secrets of Life

There is a kind of subtle depression that comes from assuming you are just going to live in a routine dictated by others. Recently I've gotten into a big programming project.

Programming is somewhere I've always felt at home. Perhaps, cause I've done it from a very early age.

Maybe math was a childhood fling, and programming a life-long committed relationship.

Where I feel comfortable experimenting, playing with the parts, and seeing what happens.

And it helps me take it as an analogy.

Everything - the plumbing in my house, social structures, financial structures we live in,

are just a sum of parts that were made by people, and an interface that interacts with you

that was made by people.

They are not solid black boxes put there by god, where every detail is perfected.






Sunday, August 17, 2014

I feel it can time for some people  30-40 years
to be able to treat women like human beings.
Before that every time you think `treat her like a human being'
you just shift from thinking of her as a sex object,
to thinking of her as a perfect delicate angel.

Finally, you start to be able to think of her as
a person with preferences, self-interests, desires.

You start to try to understand what makes her tick? What does she want?
Before that, you wouldn't do that, because both as a sex object and as a perfect angel,
there's nothing she wants in your mind.

But I guess it's just a special case of viewing people as one dimension - 100% greedy or 100% kind, ect.
One time ago,
these were all just sounds to me.
They didn't mean anything.
They didn't hold back the sounds behind them.
They had their time.
She said we had our time, together.
I feel it's still our time, For-
ever, we'll play the same guitar.
The strings will become rusty,
but not our love.
This sound..never dies.

Sunday, August 10, 2014

Two movements of thought.
One, encouraged very much by movies, media, that you can spend the young part of your life in
is that your real life hasn't started yet. Everything is going wrong, but there's no point or need to do something about it, because it's just a sort of nightmare, and you will wake up from it at some point into your perfect life.

But years pass.. you're 30, 33, 34, 35, almost 36.
and you see the world, this life, is not going anywhere.

You see also, that although there is a lot of shit in it, it's not random, not arbitrary.
There are patterns. You behave in certain ways, and you get certain types of results.
and the patterns are hard to see, because your emotions only see this moment,
but the patters of cause and effect, in life quality, in relationships with people,
span over a longer period - from a day to years.

Finally you say.. OK, given that is my life situation, that I am not going to suddenly wake up from it,
how can I make the best of it.

Suddenly, you find a lot of satisfaction in small improvements..

like OK - when I was 18 I spent the whole year watching TV, jerking off, and perhaps not having one friend or date.

Now at 35, I only spend 20/30% of the time doing that, and I have 3 girls that don't run away when I start talking to them.

At this rate, things will be awesome by the time I'm 85.

Tuesday, July 29, 2014

Monday, July 28, 2014

 I remember seeing a group of ants a few days ago, some of them lifting a piece of food, and others walking around those lifting the food. I thought - maybe one of these ants is thinking `I'm not going to be doing this for long. My life's gonna change. I'm gonna start my own company and leave this shitty job' but looking at him from above, it is so clear he is part of a pattern, maybe he will shift by 1/2% in some direction, but that's it.

I'm thinking lately, we need to get out of this inner conflict, thinking we have to be completely different.
Be a super-social family oriented person when we're not even close to that.
Be a super-athlete, doing flips in the air and being an expert in martial arts, when we could never do a cartwheel or decent somersault.

Just accepts our strengths and weaknesses, accept the path we're heading in, and just stir the 1/2% we have control over in the better direction.

Friday, July 25, 2014

to any fellow academics - on a scale of 1 to 10, how unethical would you rate the following: A fellow co-author retracts a paper that has already been accepted to a conference. He does this without the consent of other coauthors, and without even telling them he did it. They only find out, by accident, after the camera ready version was due.

I never thought academic papers could be like the stock market - you could wake up one morning with less

Friday, July 18, 2014

Low influence functions

It is quite nice to see theory and practice connect.
I've recently become interested in digital crypto-currencies - a hot topic about a market currently worth at least 8 billion dollars, and I would bet, will easily go to a trillion in the next decades.
I started thinking, `ahh, these theory people spending so much time and effort and obscure mathematical questions. I'm never going to do that again..'

But suddenly, this research on crypto-currencies brought me back to old papers that seemed too theoretical to me even when I was interested  100% only in theory.

Let me describe their subject a little through the following story.

Suppose there is a tribe with a council of 99 men that needs to choose
a new leader.

Everybody agrees that in the tribe, there are two members- Xena and Hercules,
that are the most deserving.
They have supernatural powers, and are half-gods.
Both seem to equally deserve the job, so it seems to make sense to just flip a coin
and decide who will be the new leader.
The problem is, who can we trust to flip this coin?
If we assign a particular member of the council, he might use some trick to make, say, Xena come out as the leader - like a coin that seems normal but has one side slightly heavier and almost always falls on that side.
Xena would agree with him in advance that if she gets elected she will do special favors for him
as the tribe leader.

So we don't want to assign the task of choosing the leader to any particular council member.
It seems better to have all council members flip a coin. Then, combine these 99 coin flips in some way.

How should we combine the coin flips?
Here is a very bad idea:
We will count how many coin flips came out heads.
If it is an even number - Xena will be chosen.
If it is an odd number - Hercules will be chosen.

Why is this a bad idea? Because the last person flipping a coin has total control of who will be chosen.
He counts out of the 98 flips so far if there is an even or odd number of heads, and according to that
can say heads or tails to ensure the decision he likes.

Here is a better idea:
We count again how many heads we had.
If it is at least 50 , Xena will be chosen.
If it is less than 50, Hercules will be chosen.
Now notice that when it's the turn of the last person, it is very likely the result has already been determined.
Specifically, only if exactly 49 of the flips so far came out heads, he can control the result.

Are there ways to combine the coin flips such that each player will have an even smaller chance of controling the result?
There are, and this has to do with what is called `low influence functions'.

A nice thing for me was that this paper of ours was mentioned in the Ethereum blog:
https://blog.ethereum.org/2014/07/05/stake/

Tuesday, July 01, 2014

God is telling you, things are not going to get better..
basically from now, you are just going to get sick and die.
So you might as well,
BE HAPPY NOW!
BE PEACEFUL NOW!

you tell God -
thanks for the advice,
but I think I'm going to focus on getting rich and getting a hot girlfriend..
I'll work on this happiness and peacefulness thing later.

Blog Archive