vendredi 18 janvier 2013

Working with multiple files in vim

There is a couple of useful commands that can be used to work with multiple files as tabs. I've mentioned them in a stackoverflow answer:
http://stackoverflow.com/a/14395583/612920

  • Open a file in another tab

    :tabe filepath
  •  
  • save the current session in order to be able to open the same set of files again

    :mksession session_name.vim
  •  
  •  navigate between different tabs

    gt: next tab
    gT: previous tab
  •  
  • close all tabs at once

    :qa
  •  
  • open a saved session

    vim -S session_name.vim


mercredi 23 mai 2012

cryptic perl

"Perl is the only language that looks the same before and after RSA encryption."
Keith Bostic. Programmer, created Sleepycat, contributed to free BSD unices.

Funny C quote

"One of the main causes of the fall of the Roman Empire was that, lacking zero, they had no way to indicate successful termination of their C programs."
Robert Firth. Author of programming books.

lundi 21 mai 2012

one of a million reasons to love programming

From the introduction to the excellent free book Programming ground up,

I love programming. I enjoy the challenge to not only make a working program, but to do so with style. Programming is like poetry. It conveys a message, not only to the computer, but to those who modify and use your program. With a program, you build your own world with your own rules. You create your world according to your conception of both the problem and the solution. Masterful programmers create worlds with programs that are clear and succinct, much like a poem or essay.

vendredi 6 avril 2012

Swapping numbers in java, pass-by-value VS pass-by-reference

I never thought about it, but I just discovered it is not possible to write a method to swap numbers in Java since Java is strictly pass-by-value, not pass by reference.
public void swapNumbers(int x, int y)
{
    int temp = x;
     x = y;
     y = temp;
}
If we call this function to try to swap numbers, the numbers won't be changed.
There is an example of swapping numbers in this link: http://www.roseindia.net/java/beginners/swapping.shtml, but it's confusing for beginners.

public class Swapping{
  static void swap(int i,int j){
  int temp=i;
  i=j;
  j=temp;
  System.out.println("After swapping i = " + i + " j = " + j);
  }
  public static void main(String[] args){
  int i=1;
  int j=2;
 
  System.out.prinln("Before swapping i="+i+" j="+j);
  
swap(i,j);
 
  
}
}


This example doesn't mention that after we call the method swap, the numbers will return to their original values after this call.
The same operation is really simple in C++ which allows us to pass values by reference

void swapNumbers(double& a, double& b)
{
    double temp = a;
    a = b;
    b = temp;
}
 
Now, when we call this function, a and b will be swapped.*

double number1(5.5), number2(7.0)
swapNumbers(number1, number2);
cout << "First number : << number1 << endl;
cout << " Second number :" << number2 << endl;

The output will be
First number : 7.0
Second number 5.5;

A reference is merely an alias, another name for the same variable. Hence, making operations on a variable will also change the variable value.

real life example for passing argument by reference in java

I read this in one of the comments for this answer in SO http://stackoverflow.com/a/40523/612920

My attempt at a good way to visualize object passing: Imagine a balloon.
Calling a function is like tieing a second string to the balloon and handing the line to the function.
parameter = new Balloon(); will cut that string and create a new balloon (but this has no effect on the original balloon).
parameter.pop(); will still pop it though because it follows the string to the same, original balloon. Java is pass by value, but the value passed is not deep, it is at the highest level, i.e. a primitive or a pointer. Don't confuse that with a deep pass-by-value where the object is entirely cloned and passed.

mercredi 4 avril 2012

Everyone should learn Vi / VIm

I always wanted to learn vi. It seems that once you get to know it well, you can be a lot more productive.
The problem is you got to invest some time to learn all the shortcuts that will help you to be productive and practice them often to get used to them. A good start is the vimtutor which is an interactive learning resource that can be started by typing vimtutor in the shell. Once you finish the tutorial, you can find a ton of resources on the internet. Personally, I'm starting with vim tips http://vim.wikia.com/wiki/Vim_Tips_Wiki. There is a lot of good tips, like autocompletion feature which can be done using Ctrl+N for next word and Ctrl+p for previous word.
If I can be as good in vi as this guy http://stackoverflow.com/a/1220118/612920 , I'm sure I'll be at least 10 times faster. If you decide as well to start learning vim, I'm sure you are making a good decision.