lundi 21 janvier 2013

The expr command

The expr command is used to evaluate an expression and output the corresponding value. It could be used for example to use the terminal as a calculator:

    $ expr 80 - 25
    55
    $ expr 80 / 25
    3
 
Note that there is a space between the different arguments, otherwise the expression might not be evaluated properly.
It can be used as well for string operations and boolean operations

    $ expr length "abc"
    3
    $ expr 1 "&" 0
    0

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.