Fun with C++11 Lambdas

So I got a c++11 book. I’ve been following a few of the language differences and features for a while, but I decided to break down and actually understand it all. I decided to have some fun with lambdas today. Lambdas are a very useful language feature that lots of dynamic languages have (JavaScript, Python, <insert your favorite there is plenty>). For some additional readying you might read about Closures and Functors.

Anyway, so here you go:

Basic syntax

[...] {...} // simplest form
// or showing all the optional arguments
[...] (...) mutable throwSpec -> retType {...}

What does that all mean?

The brackets are a parameter list of values you can pass in for usage in the lambda from the context in which the lambda is being created. You could pass in local variables or this for instance. You can pass by a value or reference. Unlike other languages, if you pass by reference and the value is deleted, your lambda might have unexpected results or crash.

The parenthesis are the parameters you pass in when you call the lambda. Declared like a parameter list for any other function. auto is nice but if you need the type (like to return a lambda from a function) you can use the functional header.


using namespace std;

function getTheLambda()
    int x=7;
    // will return an undefined value because x is going to be removed from
    // the stack when this function exits.
    return [&x] (int a, int b) { return x+a+b; };

int main(int argc, char* argv[])

    auto m = [] (int y) { cout << "lamda value: " << y << endl; };
    // call it

    auto l=getTheLambda();
    cout << "Lambda Result: " << l(1,2) << endl;

    return 0;

For me it printed:

lamda value: 3
Lambda Result: 32770

If you change the first lamba in the function to "x" instead of "&x" you'll get the expected result of 10.

I can't wait to pass a lambda to a thread and use it as some type of callback!

Posted in Programming | Tagged , | Comments Off on Fun with C++11 Lambdas

git diff with color words

I had these files with very very long lines of text on each line. It was paragraphs worth of words but not word wrapped. These files were stored in a git repository. Now and then, someone would change a word. Diff with line based output was difficult to find what actually changed.

I figured there must be a way to see just the words. A little searching around the docs and I found it.

> git diff --word-diff=color

Perfect. I still got ling scrolling lines though. You can override that with your $GIT_PAGER variable or I just piped the output manually for a one time fix:

> git diff --word-diff=color | less -r

Much easier to see the changes!

Posted in Programming | Tagged , , , | Comments Off on git diff with color words

Storing Lipo batteries for faster charging times and longer lipo life

I just finished a quick flight at the park with my Trex 550e. What a fun bird to fly.

Trex 550e Upside down hover

That picture was taken last year on a nice summer morning. No pictures today but it’s the same heli.

Anyway, the point of this post is about lipo batteries. I just brought up the heli fly because I had such a great experience with it’s battery. Coincidentally, I was just talking today with someone about charging batteries and they wished they could charge 4 6S batteries in 20 minutes. You need a lot of Watts to do that. A 6S lipo is 22.2V nominal and the batteries for these helis are 5000mAh. So if W=A*V then 4 of these things take ~444W to charge in an hour. Triple that to do it in 20 minutes. The only charger I know of that can do close to that is the Powerlab 8. Given a big enough input power supply it can do 1344W output.

Step back a bit. When I first started flying RC airplanes and helis I wanted to fly all the time. Well, I still do but I was pretty gung ho from the beginning. I’d do like 3 or 4 flights per day if the weather wasn’t bad. Morning, evening, it was all good. We live by a park big enough to fly my small trainer so I’d just run out for a couple quick flights and then go back to doing whatever. I didn’t have time to charge my batteries before every flight so at the time I just charged them after the flights and left them charged all the time. It worked out pretty well except after a while I started learning that it might be best to not store them charged all the time. Apparently it breaks down the chemistry faster when they have a full charge. A day before you’re probably fine but charged all the time is going to wear them out much faster.

Ok, side note again. All my batteries that I did that with are still fine. I use them in planes and even an EDF still and as far as I can tell that early storing stuff didn’t do much harm to them. BUT… you know, the experts must have something they know so I better heed advice.

Fast forward a bit. I decide I should probably not store my batteries charged. I think this is the point I started flying less often. If I wanted to go fly something I had to take a moment and charge some batteries. You can see why someone would want to be able to charge their batteries FAST.

I got a better charger.

OK, so I have this charger that can charge faster than the original one I had but I have a lot of batteries by this time too. I hook them all up and charge most of them at the same time. It can take over an hour still. In the mean time, I keep learning and reading things and I find out that the experts are advocating not storing the batteries all the way discharged too. Apparently storing them discharged has a similar effect to when you store them charged. How much? I have no idea. My philosophy is that I’ll probably destroy the battery before I find out if the way I stored it has much effect. However, this is good news because 1) My fancy pants charger has “store” mode and 2) I get to fly faster if they are already charged a bit.

Store mode on these chargers puts the charge on the pack back to about 70% capacity. Somewhere around 3.85-3.9V/cell. So now I get to feel all dandy because I’m storing my batteries in such a way that the experts won’t complain AND, my charging time is cut in half. I just charge the packs before a flight, go fly, then stick the packs back on the charger for their storage charge and I’m good to go for next time.

Back to the heli flight today. I was smiling because I just stuck that one 6S 5000mAh pack on for a quick charge and 20 minutes later I was ready to go.

Posted in Radio Control | Tagged , , , , | Comments Off on Storing Lipo batteries for faster charging times and longer lipo life

Quick convert raw g711 uLaw audio to a .au file

I had reason to playback some raw g.711 audio data. I made the following script to convert the data to a .au file that is playable.

From the AU file spec.. which is way simple by the way:
You just need to add 6 32-bit header flags.

import struct

header = [ 0x2e736e64, 24, 0xffffffff, 1, 8000, 1 ]
o.write ( struct.pack ( ">IIIIII", *header ) )

raw = open('in.raw','rb').read()

You can probably see that this would be extremely simple to use with other audio formats too. Modify as you need.
Header Bytes:

  1. Const AU identifier: 0x2e736e64
  2. Size of Header: 24.
  3. Size of File: use 0xffffffff for unknown, or you could replace with the actual size.
  4. Audio format. 1 is uLaw. From the spec choose whatever you need if it isn’t uLaw.
  5. Sample rate. uLaw is always 8000.
  6. Channels. 1 channel in this case.
Posted in Programming | Tagged , , , , | 1 Comment

How to correctly calculate break even time on a refinance – For Math Geeks

After my last post on refinancing, I found a refinance calculator on that compared loans the way I suggested. It has a shortfall though, it calculates break even time with the difference in your new payment compared to the difference in your old payment. This is actually quite incorrect. That number is good to show you how much money you’ll have extra in your bank account each month if you are making only the minimum payment. What if you pay extra on the mortgage though? In order to make almost any refinance the most beneficial, you need to pay extra on the new mortgage so that it doesn’t take as long as the new term of the loan to actually pay off the mortgage. i.e., you should at least pay what you were paying on your old mortgage.

Continue reading

Posted in Miscellaneous | Tagged , , , , , | Comments Off on How to correctly calculate break even time on a refinance – For Math Geeks

Should I refinance my house?

OK, disclaimer: I’m not a financial professional. That said, I do enjoy financial topics and am somewhat of a hobbyist. I’ve written articles in the past about how I do envelope budgeting with Gnucash for instance. That said, I’ve been thinking a lot on these interest rates lately and thought I’d write down a few thoughts.

Anyway, if you haven’t noticed, long term interest rates are quite low. At time of writing, my local credit union is offering 30 year mortgages at 4% and 15 years for 3.25%. Naturally, I got an email from the broker we worked with last time we refinanced saying we might be interested in another refinance. I figured lots of other people are in a similar type of situation so I thought I’d write down a few tips.

Continue reading

Posted in Miscellaneous | Tagged , , , , | 4 Comments