Vim how to replace leading line number from source file

Today I needed to replace the leading line number from a source file that was forwarded to me probably gotten from an html representation of a source repository of some sort. I know, definitely not the best way to acquire a source file this way but a good exercise nonetheless.

In Vim the following would do the trick:

:%s/^\s*\d\+//g

%    --> for each line
^    --> beginning of the current line
\s*  --> match any leading space characters
\d\+ --> match any number of digits (one or more)
     --> replace with nothing

Lambda functions in Scala

Scala supports fuctions as first class values therefore bringing the fuctional world closer to the imperative system we are so used to in Java.

Thus in scala it is possible to associate generic functions to variables using the => symbol, delivering a more mathematical touch with this so called lambda function in-language construct.

scala> val f = (x:String) => x.toBoolean
f: (String) => Boolean = <function1>

scala> f("false")
res58: Boolean = false

scala> f("true")
res59: Boolean = true

scala> f("tru")
java.lang.NumberFormatException: For input string: "tru"
	at scala.collection.immutable.StringLike$class.parseBoolean(StringLike.scala:240)
	at scala.collection.immutable.StringLike$class.toBoolean(StringLike.scala:228)
	at scala.collection.immutable.StringOps.toBoolean(StringOps.scala:31)

I love this beautiful formation that reminds me of the way we defined mathematical functions in university.

In the above example function f is itself an instance living on its own without the need of an enclosing class that takes as an argument a String and gives back a Boolean object based on the string provided. Of course we get a runtime error when passing a non-boolean String into the function.

Scala Case Class disassembled

If we create a very simple Case Class in Scala:

$ echo 'case class TestCaseClass(a:Int)' > TestCaseClass.scala

and after compiling it we attempt to disassemble with:

javap -private TestCaseClass

we get:

public class TestCaseClass extends java.lang.Object implements scala.ScalaObject,scala.Product,scala.Serializable{
    private final int a;
    public static final scala.Function1 andThen(scala.Function1);
    public static final scala.Function1 compose(scala.Function1);
    public scala.collection.Iterator productIterator();
    public scala.collection.Iterator productElements();
    public int a();
    public TestCaseClass copy(int);
    public int copy$default$1();
    public int hashCode();
    public java.lang.String toString();
    public boolean equals(java.lang.Object);
    public java.lang.String productPrefix();
    public int productArity();
    public java.lang.Object productElement(int);
    public boolean canEqual(java.lang.Object);
    private final boolean gd1$1(int);
    public TestCaseClass(int);
}

Notice the wealth of helper classes created for us from the Scala compiler like the hashCode(), toString() and equals(..) methods that we routinely create in every new Java class we construct. Also our state variable is encapsulated as a constant and surprisingly we even get a factory method therefore reducing the need for calling new per each creation of instance objects.

Scala Class and Object disassembled

A Scala Class translates into an equivalent Java Class implementing scala.ScalaObject:

In Scala:

class TestScalaClass

if disassembled produces the following equivalent Java code:

public class TestScalaClass extends java.lang.Object implements scala.ScalaObject{
    public TestScalaClass();
}

The Scala Object which is Scala’s in-language support for Singleton Pattern:

object TestScalaObject

compiles into two bytecode files: TestScalaObject.class and TestScalaObject$.class.

TestScalaObject.class disassembles into:

public final class TestScalaObject extends java.lang.Object{
}

whereas TestScalaObject$.class disassembles into:

public final class TestScalaObject$ extends java.lang.Object implements scala.ScalaObject{
    public static final TestScalaObject$ MODULE$;
    public static {};
}

MODULE$ makes Scala to be accessible through Java.