Skip to content

Algorithm Mnemonics: Increase Productivity with STL Algorithms

Notifications You must be signed in to change notification settings

ylc/algorithm-mnemonics

 
 

Repository files navigation

Algorithm Mnemonics: Increase Productivity with STL Algorithms

Begin

For the impatient, a picture is worth 764 words, here is a video demonstrating algorithm mnemonics:

Video Demonstration of Algorithm Mnemonics

Are you tired of typing begin and end? Do you keep looking up the STL algorithms in a reference? Would you like to use STL algorithms more?

Introducing algorithm mnemonics.

Algorithm mnemonics is a way to operate at a higher level while constructing code. Similar to mnemonics in assembly language, there is a mnemonic for every STL algorithm. I call this the STL Instruction Set. The entire STL Instruction Set is listed in the word document called STLInstructionSet.docx.

Many editors include a capability called abbreviations, aliases, template expansion, surround text, or simply code snippets. This feature allows you to expand a code template into the editor by typing an abbreviated form of the expanded text. Algorithm mnemonics is a method of maximizing the use of these facilities in your editor.

If your editor doesn’t have this capability, it can most likely be implemented using the language of the editor. I provide examples of how I have implemented it in SlickEdit and vim below. You can use these examples to foster an idea of how you could implement it in your editor of choice.

How does it work?

There are different ways algorithm mnemonics can be implemented in an editor. It depends on which editing facility you use or if you implement the facility yourself. I will describe how it works in general and then show specific examples in SlickEdit and vim.

In this example, let’s use the STL algorithm std::count_if. The algorithm mnemonic is cni. In the following code, you would type the container v:

  std::vector<int> v;
  v<cursor-here>

followed by cni:

  std::vector<int> v;
  vcni<cursor-here>

the editor would then recognize this sequence and expand to:

  std::vector<int> v;
  auto n = std::count_if( begin( v ), end( v ), []( <placeholder> ) {
    <placeholder>
  } ); 

You would then tab through each placeholder to fill out the algorithm.

This is just one style, there are many different ways it could be implemented and invoked by the editor. The style you use will probably be dictated by the facility of the editor, unless of course you implement it yourself.

SlickEdit

I use SlickEdit in vim emulation mode as my primary editor. In SlickEdit, there is a command called surround_text. When surround_text is invoked with Command-S key binding, a dialog is displayed, you type the letters of the mnemonic, hit enter, and it surrounds the previous word at the cursor with the expanded text. If you type the container before invoking surround_text, the surround_text command will surround the container with the expanded text. Voilà!

SlickEdit Demonstration

You can then step through the placeholders to fill out the algorithm.

The video I used in the presentation was made using SlickEdit. Here is a link to the video demonstrating algorithm mnemonics:

Video Demonstration of Algorithm Mnemonics

vim

Created by Dawid Kurek the repository algorithm-mnemonics.vim contains a version for vim that utilizes ultisnips for vim snippets.

I created a facility for vim available in the vim directory algorithm-mnemonics-vim. It doesn’t support tabbing through the placeholders. It only expands the algorithm. You have to use your vim nimbleness to navigate and fill out the algorithm. In vim, you type the container, Control-K, followed by the mnemonic and it expands.

Emacs

Created by Ludwig Pacifici the repository algorithm-mnemonics-emacs contains a version for Emacs that utilizes YASnippet to implement the algorithm-mnemonics.

Visual Studio Code

Created by David Brötje the repository algorithm-mnemonics-vscode contains a version for the Visual Studio Code editor.

QtCreator

Created by Vahid Dzanic the directory algorithm-mnemonics/QtCreator contains a version for the QtCreator development environment.

QtCreator Demonstration

End

Once you have added this to your editor, I suggest you go through each algorithm, use it in a real working example, or unit test. Going through each algorithm will tattoo the mnemonics and STL algorithms on your brain. You will then be able to summon them at will.

You might be asking yourself if it is worth taking the time to implement this in your editor. I can only tell you this. There is no better feeling than when I am down in the trenches, wired in, cranking out code, and I notice a place where I can use a STL algorithm. I deploy the mnemonic and it is almost like code exploding on my screen. I often call the mnemonics small code generators or code grenades. They detonate with a key press and blast on the screen.

You will begin to actively seek places to use an algorithm. What can be better than that? Eliminating raw loops with the mere thought of three letters and you keep on moving.

It is pure joy.

About

Algorithm Mnemonics: Increase Productivity with STL Algorithms

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Vim Script 80.6%
  • Shell 19.4%