C++ Craziness

If someone asked you to write a C++ program to output the squares of the numbers from 0 to 9, what would it look like? Vivek Haldar, whose work I’ve cited approvingly several times (1, 2, 3), has an answer. In fact, he has two answers. The point of his post was how the new C++11 makes better solutions possible.

But here’s the thing: the examples are ridiculous. Here’s what I’d do

#include <iostream>
using namespace std;
int main ()
{
  for ( int i = 0; i < 10; i++ )
    cout << " " << i * i;
  return 0;
}

Now look at Haldar’s solutions. To be fair, my code would look the same in old or new C++ so it doesn’t work well as an example of the new functionality that C++11 brings, which is what Haldar is trying to demonstrate. Still, examples like his could easily lead someone to conclude that C++ programmers are crazy. Ironically, Haldar notes that Python has a much more succinct solution based on list comprehensions and hopes that C++ will soon introduce list comprehensions too. But the Python example looks essentially like the code above except that it makes a list first. Why would you do that?

None of this is not to beat up on Haldar, who’s a smart guy and probably wouldn’t actually write code like his examples if he weren’t trying to make a point. It does illustrate a problem in the C++, and more generally the OOP, worlds though. Just because the language provides all those nice templates and object machinery doesn’t mean you should use them to solve simple problems where they aren’t needed. If you do, you end up with examples like the ones we lamented previously.

Posted in Programming | Tagged | Leave a comment

Let’s Play VimGolf

Or VimGolf in Emacs anyway. Over at Life seen through a coder’s eyes, Yassine Chaouche poses the following problem. Starting with an Emacs buffer containing only

[[Image(grep_1.png)]]

end up with

[[Image(grep_1.png)]]
[[Image(grep_2.png)]]
[[Image(grep_3.png)]]
[[Image(grep_4.png)]]
[[Image(grep_5.png)]]
[[Image(grep_6.png)]]
[[Image(grep_7.png)]]
[[Image(grep_8.png)]]

Chaouche solves this problem using CUA mode. If you use CUA mode, you should take a look at his solution; you may find it will serve as a template for similar problems you may encounter.

I’ve never used CUA mode in Emacs or, before I saw the light, Vim so his solution doesn’t work for me. It also seems as if it uses more keystrokes than necessary. (This is VimGolf in Emacs, remember.) To me, this cries out for a macro. Here’s what I did

Keystrokes Action Buffer
<start state> ▮[[Image(grep_1.png)]‍]
Ctrl+k kill line
Ctrl+1 F3 set macro counter to 1
and begin macro
Ctrl+y yank line [[Image(grep_1.png)]‍]▮
Ctrl+r 1 search backwards for 1 [[Image(grep_▮1.png)]‍]
Ctrl+d delete 1 [[Image(grep_▮.png)]‍]
F3 insert macro counter [[Image(grep_1▮.png)]‍]
Ctrl+e end of line [[Image(grep_1.png)]‍]▮
Return new line [[Image(grep_1.png)]‍]
F4 end macro [[Image(grep_1.png)]‍]
Ctrl+7 F4 play macro 7 times <end state>

That’s a solution in just 13 keystrokes. The problem is simpler, but takes more keystrokes, if you start with an empty buffer. Then it’s just 【Ctrl+1 F3】 to set the macro counter and start recording, the text [‍[Image(grep_, 【F3】 to insert the counter, and finally the text .png)]] followed by 【Return F4】 to move to the next line and stop recording. Then 【Ctrl+7 F4】 inserts the other 7 lines as before.

This post really should have been a video but I’m not set up for that. Besides, I don’t have a cool voice like Magnar Sveen.

Posted in General | Tagged | 4 Comments

Mawking AWK with Lisp

Back in 2009, Brendan O’Connor over at AI and Social Science posted an article entitled Don’t MAWK AWK—the fastest and most elegant big data munging language! He recently posted an update that caused the original article to pop up on my radar. The problem that the post addressed was transforming about a gigabyte of data into a form that could be used by Matlab and R. The transformation itself was simple and wouldn’t normally be something that Irreal readers would find interesting—it took just 3 lines of AWK code—except for a surprising result.

Here’s the surprise: since the AWK script was so simple, O’Connor and a colleague wrote versions of the program in AWK, Java, Python, Perl, Ruby, and two versions in C++. Altogether they ran 9 tests and the fastest version was in AWK. It turns out that the mawk version of AWK outperformed C++, Java, Python, Perl, Ruby, and its two siblings, nawk and gawk. That’s pretty surprising. Of course the versions in other languages were all longer than the AWK version too; in some cases an order of magnitude larger. See O’Connor’s post for the timings and details.

Naturally, I had to try it in Lisp to see how it would compare in speed and size. In order to get an idea of how Lisp compares to the other languages, I reran the tests for every language I have installed. That leaves out Java and Ruby but there’s enough to get a good idea of how Lisp compares. All the tests were run of my iMac. Here’s the system specifications:

file:///Users/jcs/org/blog/this-mac.png

Language Version Time (min:sec) Lines of Code
mawk 1.3.4 0:44.634 3
C-ish C++ 4.2.1 llvm-g++ 0:53.255 42
Python 2.7.1 1:21.003 20
Lisp-opt SBCL 1.1.1 1:54.576 19
Perl 5.12.3 1:57.928 17
Lisp SBCL 1.1.1 1:58.193 16
C++ 4.2.1.llvm-g++ 2:49.368 48
awk 20070501 3:18.187 3
Ruby 1.8.7 4:45.760 22

Here’s the Lisp code (see O’Connor’s post for the other code):

(load #P "/Users/jcs/quicklisp/setup.lisp")
(require ':jcs-utils)
(require ':split-sequence)
(let ((j 0) (jmap (make-hash-table :test 'equal)))
  (with-open-file (v "vocab" :direction :output )
    (dolist (f (cdr sb-ext:*posix-argv*))
      (let ((i 0) (imap (make-hash-table :test 'equal)))
        (with-open-file (fs (concatenate 'string f "n") :direction :output)
          (jcs-utils:dolines (l f)
            (destructuring-bind (item feature value) (split-sequence:split-sequence #\Space l)
              (unless (gethash item imap)
                (setf (gethash item imap) (incf i)))
              (unless (gethash feature jmap)
                (setf (gethash feature jmap) (incf j))
                (format v "~a~%" feature))
              (format fs "~a ~a ~a~%" (gethash item imap) (gethash feature jmap) value))))))))

The Lisp-opt run used the same code except that I added some optimization declarations and specified an initial size for the hash tables. As you can see, it didn’t make very much difference. I’m a bit surprised that the Python version is faster but the most salient result remains that mawk outperformed everything else, even C++.

Update:

I didn’t realize that Ruby is installed by default on OS X. For completeness, I’ve added Ruby to the results.

Posted in Programming | Tagged , | Leave a comment

A Hyper Key for the Mac

The other day I wrote about using the Mac’s virtual keypad to get some additional key sequences for Emacs. That didn’t work out as well as I’d hoped because, unlike my MacBook Pro, the keyboard for my iMac doesn’t have a virtual keypad. I thought about getting an add-on Bluetooth keypad for my iMac but that seemed like unnecessary expense and clutter for something that I might not use all that much.

All was not lost, though, because Magnar Sveen popped up in the comments to suggest that I map the 【fn】key to 【hyper】. I’d considered doing that before but the 【fn】 key is used to control music functions such as start/stop and volume and I sometimes want to use those functions while I’m in Emacs. Sveen replied that if you map 【fn】 to 【hyper】 it will still work properly when you use it to modify a function key, which is where the music controls are.

So, this is a note to all Emacs users working on a Mac. Just map 【fn】 to 【hyper】 and you have the best of both worlds: easy access to all the controls living on the function keys and an easy-to-use 【hyper】 key that opens up a bunch of key sequences that you can map to your favorite commands. I implemented it by adding

(setq ns-function-modifier 'hyper)

to my darwin.el file, which is where all my Mac specific configurations live.

Thanks to Magnar for setting me straight on this. I’m already loving having the 【hyper】 key in an easy to use place.

Posted in General | Tagged | 4 Comments

Get Rid of the TSA Before They Kill Us

As regular readers know, we here at Irreal are not fans of the TSA. Apparently, Charles Kenny at BloombergBusinessweek isn’t a fan either. He’s posted an article entitled Airport Security Is Killing Us in which he makes the case that the TSA has made air travel so unpleasant that increasing numbers of people are foregoing it in favor of driving whenever they can. The problem is that driving is much more dangerous than flying and causes far more fatalities, with or without terrorists.

Consider: of the 150,000 murders in the U.S. between 9/11 and the end of 2010, Islamic extremism accounted for less than 3 dozen. World wide, outside of Iraq and Afghanistan, Islamic extremism accounted for between 200 and 400 deaths yearly. Just in the U.S., more people die in their bathtubs every year.

Yet despite the minuscule chances of being killed by a terrorist, the TSA managed to spend $580 billion between 2001 and 2011 without catching a single terrorist. Much of that has been spent with little thought or analysis as to how it might be used effectively.

Meanwhile, TSA actions have resulted in increased driving and the concomitant rise in traffic fatalities. Kenny reports on a study by Cornell University suggesting that the increase amounts to an additional 242 traffic deaths per month, greatly eclipsing the death toll of 9/11 itself.

Read Kenny’s article. It’s interesting, persuasive, and has links to the sources for all the statistics that I’ve mentioned here.

Afterword

Rick Falkvinge over at Falkvinge on Infopolicy has crunched the numbers for Europe and comes up with some interesting results:

  • For the years 2000–2009, an average of about 40 people per year died as a result of terrorism (and over half of those were due to a single incident in Spain in 2004).
  • Fives times as many people die every year due to drownings in bathtubs.
  • Six times as many people die every year from falling off chairs.
  • Twenty times as many people die very year from falling out of bed.
  • Over a hundred times as many people die every year from falling down the stairs.

Alas, as Falkvinge points out, politicians will merely use these statistics to say, “See? It’s working.”

Update: or → of

Posted in General | Tagged | 1 Comment

Using the Keypad

Xah Lee has an interesting post on defining keys to open frequently used files (search for 2012-11-21). The idea is that you add lines like

(global-set-key (kbd "<kp-7> <kp-3>") (lambda () "" (interactive) (find-file "~/web/ergoemacs_org/emacs/blog.html")))

for each of the files that you access frequently. That way, you can open the file quickly by pressing just 2 keys.

It’s a cute idea but not one I’d use. That’s because Lee and I have slightly different Emacs work flows. Lee says he likes to close files when he’s finished with them. I, on the other hand, tend to leave files open and occasionally cull files that I’m no longer using. Lee’s idea makes sense for him because he is always opening those files. I rarely have to open the files I use all the time because I keep them open—even across Emacs invocations—so I don’t really need a super efficient way to open them.

Still, I found the post useful because it reminded me that using the keypad is a way to get access to about 15 unused keys for binding to commands. I’ve never thought too much about that because I’m almost always on one of my Macs and their keyboards don’t have a separate keypad. What they do have is a virtual keypad on the regular keyboard that is accessed by the function key. Thus, <kp-2> is accessed by pressing 【fn+k】 and that’s not much harder than, say, 【Ctrl+k】 other than for a slightly longer reach for the 【fn】 key.

So, there are two takeaways from Lee’s post:

  1. If you have a work flow like Lee’s, you might find it useful to add a list of keybindings to open frequently accessed files to your .emacs or init.el file.
  2. Don’t forget about the keypad (even if you’re on a Mac) as a source of keys suitable for binding to frequently used commands.
Posted in General | Tagged | 5 Comments

Debating Copyright Without the RSC

I recently wrote about an excellent policy brief published by the Republican Study Committee that discussed copyright. Sadly, the RSC withdrew the brief within 24 hours after heavy complaints from lobbyists for big media. Now, Techdirt, which also covered the initial story, has taken on the task of continuing the debate that the Republicans (and, of course, the wholly-owned Democrats) are afraid to hold.

They are planning a series of posts that discuss the points raised in the original brief. Follow the above link for the first installment. This is a discussion that we desperately need to have. Big media is rampaging around in the china shop causing all sorts of damage: suing their customers, pushing for Internet destroying laws, and all manner of other actions familiar to us all.

When alleged copyright infringers face 45 years imprisonment and are arrested with the aid of assault rifles and helicopters, you don’t need to be a deep thinker to understand that things are out of control. It really is past time to start having a serious discussion about this. Perhaps Techdirt’s efforts will be the start of that discussion.

Posted in General | Leave a comment

The Y-Combinator

Jim Weirich, of Neo gave a really nice talk on The Y-Combinator at RubyConf2012. Given the venue, the examples are, of course, in Ruby but you should have no difficulty following them: I know no Ruby at all but was able to understand the code easily. The harder part is following some of the ideas—especially the functional refactorings—that may be unfamiliar to you.

Not to worry, Weirich has a series of posts based on the talk that covers the same material so you can go over it at a slower pace after you watch the talk to fill in anything you missed. Currently only two of the three posts are up but they cover the background material that you need to understand the rest of the talk. The posts are very lucid and stand on their own well.

If you’ve ever wondered what the Y-Combinator is (other than an angel investor) and what it’s used for, this talk is an excellent introduction. Weirich is an engaging and entertaining speaker and the material is very interesting. Again, don’t be scared away by Ruby: the examples are easy to follow.

Posted in Programming | Tagged | 1 Comment

Why We Use IDEs

Wille Faler over at recursivity.com has a nice post on IDEs and why we need them. His take is that if you need an IDE it’s because of shortcomings in the language you’re using. He has in mind, of course, Java but it’s a good general principle. Languages like C, Lisp, Python, Ruby, etc. don’t need IDEs; they’re relatively simple and small and most of them have a REPL available that makes interactive development easy and rewarding. Even in C, which does not, of course, have a REPL, development is straightforward and any of the standard editors can easily integrate the compile/edit/debug cycle.

To be clear, neither Faler not I claim that using an IDE is bad; only that if you claim you need one to do your work then that’s an indication of problems with the language you’re using. There’s some interesting (and heated) discussion in the comments including the question of what, exactly, constitutes an IDE. Does Emacs with its language awareness and ability to send code directly to the REPL count? Faler says no. He clarifies that he was mostly thinking about IDEs like Eclipse and IntelliJ that are targeted at a single language and duplicate functionality that could reside in a REPL or build system.

If you’re interested in the question of what constitutes a good tool for development, you might find the post and comments interesting. I know I did.

Posted in Programming | Tagged | 1 Comment

Bashisms

Russell Power over at rjpower.org has a nice post on some Bash tricks. Despite over 20 years of Bash use I learned something new. It turns out that if you type【Ctrl+x Ctrl+e】Bash will bring up an editor session that allows you to edit the control line. The editor called is specified by FCEDIT if set, then EDITOR, and then defaults to Emacs. This will do the right thing for almost everybody. Almost everyone sets EDITOR and almost no one sets FCEDIT so【Ctrl+x Ctrl+e】 will do the right thing for most people.

This isn’t the type of thing you use everyday but if you have a complicated command line that fails and you want to change it a bit this is a nice way of doing it. All the powers of Emacs on your command line. What’s not to like? And don’t forget the other tips—also useful.

Posted in General | Tagged | 2 Comments