Thursday, June 30, 2011

Factorial Function One-Liner in Python

I recently got a copy of the Python Cookbook. This is the first programming book I've picked up that one would call "advanced." That is, it assumes you know what an if-statement does. As a result, the book is rather refreshing.

I picked this book up because I've been looking for a solid place to read code, something I've heard advocated frequently among more experienced hackers. The book is full of code examples and explanations, and seeing how other hackers solved particular problems makes it clear why reading code is so crucial to coding better. In nearly every piece of code I've read through, I've encountered something new. My favorite so far is 17.9: Computing Factorials with lambda:
    f = lambda n: n-1 + abs(n-1) and f(n-1)*n or 1

So, this is the factorial function, contained entirely on a single line. A few subtleties of Python allow it to work, which the book explains quite well.

First of all, when a lambda form is defined, it isn't evaluated. This allows lambda forms to be defined recursively. Generally, lambda forms are not assigned names--they're "anonymous functions"--but they can be, as was done here.

The other trick--and the part that wasn't obvious to me--was the use of the ternary operator, which I'd never heard of before this. In C, there's a statement with the syntax:
    condition?iftrue:iffalse

So, effectively, this can be thought of as "Is condition true? Do iftrue. If it is false, do iffalse." In Python, the syntax is:
    condition and iftrue or iffalse

This allows you to write one line if statements. Combined with lambda, you can write one line recursive functions this way, where condition is your continue/escape condition.

Consider, also, the condition. It's not an explicit boolean statement. However, Python has implicit boolean evaluations. For example, an empty list "[]" evaluates as false. Similarly, "0" evaluates to false.

If you're doing anything more sophisticated, this is rather pointless. However, if you need merely choose between a base case or recurring, this is a simple, concise way to do it.

Tuesday, June 21, 2011

Natural Gas

A lot of fossil fuel companies have been pushing natural gas as the next big solution to our energy problems. This doesn't surprise me, and for the short term it's the way to go. However, we shouldn't stop developing alternatives.

First of all, there is more natural gas available than crude. This is because of what happens to fossil fuels as time progresses. They begin as peat, then are compressed into coal. After a few more million years, as they're buried deeper and deeper by the rock cycle, they're compressed into what's known as oil shale--the middle state between coal and oil.

With a little more heat and pressure, the oil shale "cracks" into crude. However, we can speed up this process a bit, which is what we do when we dig up oil shale. It's rather inefficient; the amount of energy we get out of the oil is drastically reduced when we have to force it out of the ground. When we first started digging up crude, it would literally gush out of the ground. Now-a-days, not so much.

Deeper still under greater heat and pressure, crude oil can be cracked further; this is when natural gas is formed. It is the end result of the chain; there is no other fossil fuel with a better energy yield for the minimal amount of carbon.

As a consequence of this, natural gas supplies are far greater than crude. For the most part, we can keep digging deeper for natural gas; for crude, there's a limit. With large supplies, we could move to a relatively stable natural gas economy for the 21st century.

Although with advantages, natural gas has its problems. First of all, it's still a fossil fuel, which means it exists in limited quantities. If we are idiotic, as history has demonstrated, we will repeat the problem we are experiencing with oil right now. This time, as we burn through our next energy source, we can hopefully master the technology to rely on completely renewable alternatives.

Also, being a fossil fuel, it still produces greenhouse gasses. In fact, natural gas is a greenhouse gas--methane is the most potent of them all. Combustion cannot completely burn off all of the methane, so using it as a fuel source would increase the amount in the atmosphere. Additionally, any form of leakage during use would release methane.

In fact, leakage is a subset of another problem with natural gas--transportation and storage. Unlike gasoline and crude oil, which stay where you put them, natural gas likes to mix and mingle with the atmosphere. As a result, the containers needed to store and transport it have to be heavier and sealed better than those with liquid fossil fuels. This means that there's more overhead when using natural gas as a fuel source--for example, your car would have to be heavier, so its efficiency would be worse or range would be reduced.

This also begs the question of a natural gas wreck. Gasoline was chosen as a fuel in part due to its relative stability. You've probably seen plenty of movies where cars' gas tanks explode. For the most part, that's exaggeration. In fact, gasoline is rather hard to blow up, at least compared to natural gas. Liquid gasoline itself does not burn--it must be vaporized to do so. When it bursts into flame, the heat sets off a chain reaction, vaporizing more gasoline, allowing more gasoline to burn. Only with that chain reaction can gasoline burn.

Natural gas, on the other hand, vaporizes under normal atmospheric conditions, ready to burn. This higher volatility translates into a much greater explosive tendency. In a bad wreck, if someone broke the seals to their gas lines, their car would be ready to explode on the spot. No one wants that to happen; generally, lag time before the car explodes is helpful.

Another problem with natural gas is the extent to which we are willing to push for it. A process called fracking has been drawing a lot of attention as of late where natural gas deposits are pressurized to release previously unreachable natural gas. This process sounds gentle enough--however, many of these deposits are located near the aquifer, the place in the ground from where people obtain well water. The pressure seems to diffuse natural gas into the surrounding aquifer, resulting in flammable, bubbling well water.

Despite the potential risks, natural gas can solve our energy problems in the short term better than any other source. Like any other fossil fuel, however, it exists in limited quantities and is bad for the environment. Nevertheless, it's the next step in our progression of energy sources and buys us more time to get to something fully renewable.

If you'd like to read more about fossil fuels and peak oil, I recommend this book.

Saturday, June 18, 2011

The Cost of Ads

Recently, I installed Adblock and Flashblock on Google Chrome. I hesitated for a while--after all, websites had to get revenue somehow, and if it involved nothing more than a few pictures in constant motion, I was willing to accept that.

Although weary for the survival of the internet, I broke down and installed it because ads can be more than annoying. At times, the downright hinder access to information, like the pop-up hell pages in days yonder gone. Sometimes, they're downright malicious.

I couldn't take it anymore; between the paranoia and the nuisance, internet advertising had lost my trust. I was surprised with the results:

First of all, the internet was smoother. It was like the first time I used Chrome, all over again. No giant flash scripts to execute; no overloaded adserve servers to access.

More so, though, I was stunned how much cleaner everything is. Look at the website to the left--it's clear, for the first time, someone actually spent time designing it. The information is presented with out noise or irritation. It's quite incredible how much simplicity is lost when the advertising is there; the agitation that distraction causes detracts in a way that's hard to describe and costly in immeasurable ways.

After all, I don't want a free iPad. I know I'm not your "winner." I want to know the lyrics to this song, and I want to leave. Everything else is just noise. However, I'm surprised how annoying noise actually is. I thought I was a master at tuning it out, but now it's clear how exhausting tuning is.
-->