Let’s Play Ivmgolf

After my last VimGolf post I thought it would be fun to try another so I headed over to the VimGolf site to find a challenge. The first one I stumbled across was Let’s play some Imvgolf. The challenge is simple: starting with

Ivm is an awesome text editor based on
Iv, and is used to play a game called
Ivmgolf. A challenge, simple for many
Ivmgolfers, can still hide secrets.

transform it to

Vim is an awesome text editor based on
Vi, and is used to play a game called
Vimgolf. A challenge, simple for many
Vimgolfers, can still hide secrets.

in the smallest number of keystrokes possible.

My first thought was that it was too simple. Surely, it would hard to beat a simple query-replace: 【Meta+%ivReturnviReturn】 【!】 for a total of 8 keystrokes. Hard to see how you could do much better than that. Except that the winning VimGolf entries solved it in only 5 keystrokes1

Using Magnor Sveen’s awesome multiple-cursors package, which should and probably will be part of Emacs core, I was able to get that down to 6 keystrokes.

It’s easy to see what you need to do: switch the first two characters of each line and adjust the capitalization. I tried that using keyboard macros and even apply-macro-to-region-lines but I couldn’t do better than query-replace using core Emacs functionality. The problem is that any macro that requires typing in Vi has already used 4 keystrokes, and if you use a macro and something like 【Ctrl+t】 to switch characters you’ve used 3 keystrokes and still have to adjust the capitalization, get to the start of the next line, and run the macro on the rest of the lines. If you can do better than 8 keystrokes with stock Emacs, be sure to leave a comment.

Footnotes:

1 The VimGolf rules say you have to save the file too so that adds an extra 2 keystrokes in both Vim and Emacs but I’m ignoring them since they add nothing useful to the challenge. All scores in this post are without saving the file.

Posted in General | Tagged | 5 Comments

Autocompleting

Xah Lee has a nice post on the various methods of autocompleting in Emacs (search for 2012-12-01). Most Emacs users will be familiar with the majority of the material but it’s helpful to have it all written down in one place. For most of the methods he gives pointers to additional material that he’s written for that particular mode or package. Very helpful.

Of the minibuffer methods that he mentions, I use ido and smex and I’m experimenting with icomplete. For the general methods, I use hippie-expand, dabbrev-expand, and abbrev-mode. I couldn’t live without ido and smex and I use the others regularly. I find them all very handy so if you aren’t already using them you should give them a try.

I don’t use YASnippet but I’ve been meaning to install it. When guys like Lee and Magnar Sveen keep recommending it, that’s a pretty good indication that I’d find it useful. What do you think? If you have any strong feelings about the above packages or use something else that really works for you, leave a comment.

Update:

Lee has made the post a standalone page.

Posted in General | Tagged | 3 Comments

New Emacs Movie: Dired

Noufal Ibrahim has started “season two” of his Emacs Movies. This is the second section that will describe various Emacs modes and tools such as

  1. Dired
  2. Org-mode
  3. Eshell
  4. Diary
  5. Calc
  6. Ediff
  7. Gnus
  8. Vmail
  9. Erc
  10. BBDB
  11. Info

His latest movie is an excellent summary of Dired. He covers all the usual Dired tricks but includes a few that I didn’t know. For example, it’s possible to bring up a thumbnail of an image in Dired and then, if you like, open the image in another window The video is about 20 minutes long and well worth watching. Unless you’re already very familiar with Dired, you’re sure to learn something new. Definitely recommended.

Posted in General | Tagged | 1 Comment

More News From the Cursive Front

Yahoo News has an interesting article on the cursive wars. Should cursive handwriting be abandoned as an anachronism or should it be preserved as an important life skill? Regular Irreal readers already know where I stand on the matter but might want to see the arguments for the other side in Yahoo’s article.

Some schools are hanging onto cursive tenaciously, insisting that their students receive at least some instruction in it. All the usual reasons are given but there’s a new entrant in the article: cursive is necessary to teach fine motor skills. The same folks pushing that idea would doubtless become apoplectic if someone suggested that improving fine motor skills was a good reason to encourage video games.

By far the most interesting thing about the article was the comments. I read a couple of pages worth and didn’t see a single comment that wasn’t hostile to the idea of eliminating cursive. People feel extraordinarily threatened by the idea. I’ve suggested before that this has a lot to do with “I suffered through learning it, why shouldn’t they?” but no one admits to that. Instead they all give the usual silly arguments such as “this is dumbing down our students,” “if they don’t learn cursive they won’t be able to sign their names” and others that are just too embarrassing to repeat. Many are simply irrational and brimming with anger. If you’re interested and feel up to it, see the comments to Yahoo’s article; it has a good representation.

There’s another Yahoo article that explores these points a little further and discusses various home-based resources for parents who feel their children need instruction in cursive. It’s worth looking at these two articles if only to see what arguments can be brought to bear in favor of cursive.

Posted in General | 9 Comments

Icomplete Mode

Per Xah Lee (search for 2012-11-28), I’ve experimentally turned on icomplete-mode. I’m not sure how I like it so I haven’t included it in my init.el file yet.

According the internal documentation, icomplete-mode “implements a more fine-grained minibuffer completion feedback scheme.” You can toggle it with 【Meta+xicomplete-mode or turn it on/off unconditionally with a positive/non-positive prefix argument.

Lee says it’s part of Emacs 24.2 but the source file indicates it’s been around a lot longer. If you have an older version of Emacs and it works for you (or you get documentation for it with 【Ctrl+h ficomplete-mode) leave a comment so that others will know.

Posted in General | Tagged | 3 Comments

SBCL 1.1.2 Is Released

Christophe Rhodes is announcing that he has released Steel Banks Common Lisp 1.1.2. You can get the release at the SBCL Download Page. As always, installation is simple. I did my usual dance of

sh make.sh
cd test
sh run-tests.sh
cd ..
sudo sh install.sh

but binaries are available for Linux and Windows for those who don’t want to bother with compiling.

The Windows version features increased stability for threads and some other enhancements. You can see the complete list of changes on the NEWS page.

Posted in Programming | Tagged , | Leave a comment

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