Archive

Archive for the ‘programming language’ Category

Getting started with C# on Linux

June 30, 2017 Leave a comment

I’ve heard lots of good things about C# but I’ve never tried it. I saw some codes and thought “fine, it’s like Java”. As I also have Linux on my primary machine, I was not interested in Microsoft technologies. However, a few years ago .NET Core was open sourced and it reached version 1.0 (now it’s 1.1). It’s a cross platform framework and it works well under Linux too, so I thought it was time to try it. Project Mono has also existed for a long time but .NET Core comes directly from Microsoft.

Under Manjaro, install these packages with yaourt: dotnet, dotnet-cli, dotnet-sdk. We will also try Mono, so install the package mono too. For Ubuntu, see the instructions here: https://www.microsoft.com/net/core#linuxubuntu .

Create a project

Create a folder for holding your C# projects, e.g. ~/CSharpProjects (optional), and create a folder for a sample project (e.g. ~/CSharpProjects/Sample). Enter it, and create a source file called hello.cs with the following content:

using System;
using static System.Console;
using System.Linq;

namespace SampleApp
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            WriteLine("Hello World!");
        }
    }
}

Compile with Mono

If you installed Mono, you have the command “csc” (C Sharp Compiler).

$ csc hello.cs
$ mono hello.exe 
Hello World!
$ chmod u+x hello.exe
$ ./hello.exe 
Hello World!

Compile with dotnet

Now let’s use the official command-line tools. You can also delete the file “hello.exe”. I suppose you are in the project’s folder. Issue the following command:

$ dotnet new console

It creates a project file (with extension .csproj), and a sample file called Program.cs . As we already have hello.cs, Program.cs is not needed so simply delete it (leave only one source file, but it doesn’t matter which one). Then,

$ dotnet restore    # installs some necessary packages, using the .csproj project file
Restoring packages for /home/jabba/Dropbox/csharp/Sample/Sample.csproj...
...
$ dotnet run        # compile and run
Hello World!

The binary output is here: bin/Debug/netcoreapp1.1/Sample.dll . If you use the command “dotnet”, you get a .dll, not an .exe . However, it can be executed:

$ cd bin/Debug/netcoreapp1.1
$ ./Sample.dll 
Hello World!
$ dotnet Sample.dll 
Hello World!

When you are ready with the development and ready for deployment, issue this command:

$ dotnet publish
Microsoft (R) Build Engine version 15.1.1012.6693
Copyright (C) Microsoft Corporation. All rights reserved.

  Sample -> /home/jabba/Dropbox/csharp/Sample/bin/Debug/netcoreapp1.1/Sample.dll

According to the docs, “dotnet publish compiles the application, reads through its dependencies specified in the project file, and publishes the resulting set of files to a directory. The dotnet publish command’s output is ready for deployment to a hosting system (for example, a server, PC, Mac, laptop) for execution and is the only officially supported way to prepare the application for deployment.”

REPL

Mono ships a REPL too called “csharp”.

$ csharp 
Mono C# Shell, type "help;" for help

Enter statements below.
csharp> 1+1
2
csharp>

Visual Studio Code support

Programming C# with VS Code is a joy. When you open a .cs file, the editor offers immediately to install the official C# extension. In order to execute a program, install the Code Runner extension. Then, add the following lines to your user settings:

    "code-runner.executorMap": {
        // "python": "python3",
        // "csharp": "echo '# calling mono\n' && cd $dir && csc /nologo $fileName && mono $dir$fileNameWithoutExt.exe",
        "csharp": "echo '# calling dotnet run\n' && dotnet run"
    }

Choose either Mono or dotnet.

Summary

If you have Linux, no problem, you can try C#. I started to play with it yesterday, but I like it. It seems a saner language than Java :)

Links

TODO…

An awesome book for Scala

March 10, 2015 Leave a comment

scalaI decided to learn Scala as a new language. I’ve heard a lot of good things about it. I looked at some basic stuffs in it and it seems to be a cool language. It’s based on the JVM, thus it stands on the shoulders of giants :)

I found an excellent book for learning it called “Scala for the Impatient” (Amazon link). Here you can download the first 9 chapters legally, and they nicely cover the basics that let you get started. I just finished the first chapter (13 pages) and it teaches you a lot. At the end of each chapter there are exercises, so you can test your knowledge and you have a feedback of your progress. I think a programming book without exercises is worthless.

Last week I started to work with the book “Programming in Scala” 2nd ed., which is written by the author of the language. I read the first four chapters (116 pages) but it goes in all directions. It talks about advanced stuffs at the beginning and it doesn’t explain the basics well. After the four chapters I had the impression that I still know nothing about Scala…

“Scala for the Impatient” seems better to my taste. I should buy the complete book.

Getting started with the Rust programming language

December 24, 2014 Leave a comment

I heard a lot of good things about Mozilla’s Rust prog. language, so I decided to give it a try. A very nice starting point is The Rust Guide.

Installation

$ curl -s https://static.rust-lang.org/rustup.sh | sudo sh

I modified this script a little bit:

#!/bin/sh

DEST=/tmp

cd $DEST
rm -f $DEST/rustup.sh
wget https://static.rust-lang.org/rustup.sh -O $DEST/rustup.sh
sudo sh $DEST/rustup.sh

But it does the same thing. If you install Rust via this rustup.sh script, it has two advantages: (1) it installs the latest version of Rust, and (2) it also installs Cargo, the package manager and build system of Rust.

Print the version number: “rustc -V“.

Hello World

// hello_world.rs
fn main() {
    println!("Hello, World!");
}

Compile it with “rustc hello_world.rs“.

Troubleshooting
Under Ubuntu I had no problems, but under Manjaro got the following error: “rustc: error while loading shared libraries: librustc_driver-4e7c5e5c.so: cannot open shared object file: No such file or directory“.

Solution #1
Add the following line to your ~/.bashrc:

export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/usr/local/lib

Solution #2
Create the file /etc/ld.so.conf.d/rust.conf with the following content:

/usr/local/lib

Then execute the command “ldconfig” as root.

More info
Check out The Rust Guide.

Categories: manjaro, programming language, ubuntu Tags:

Learn a language in a few minutes

November 16, 2014 Leave a comment

Short summaries of different programming languages: http://learnxinyminutes.com/ .

Getting started with Clojure

December 6, 2013 Leave a comment

I wrote my first “Hello World” program in Clojure :) Here I sum up what I managed to figure out.

Clojure (pronounced as “closure“) is a dialect of the Lisp programming language created by Rich Hickey. Clojure is a functional general-purpose language, and runs on the Java Virtual Machine, Common Language Runtime, and JavaScript engines.” (via wikipedia)

Learning a new language, especially a new programming paradigm is always useful. I’ve been interested in functional programming for a long time but I could never find time to dive into it. Once I started to learn Haskell but at Chapter 2 I gave up. Maybe once… However, I heard very good things about Clojure, so I would like to investigate it from a closer range. I also like the idea that it’s built upon the JVM.

Mark Volkmann wrote an excellent introduction to the language, it’s a very good starting point.

Hello World
Clojure is built upon the JVM. The good news is that you don’t need to download different JAR files and put them to the classpath. There is an excellent command-line tool called Leiningen (“lein” for short) that does this job for you. Steps to follow:

  • download the lein script
  • put it in your PATH (I put it to ~/bin)
  • make it executable (chmod u+x lein)
  • launch it (it will download the necessary files)

Create a new project:

lein new app hello

Lein will make the folder “hello” and it will create a simple project structure.

Open the file src/hello/core.clj and edit the last line:

  ...
  (println "Hello Clojure!"))

Enter the project “hello” (the folder where the file project.clj is placed) and run the project:

lein run

Normally you should see the “Hello Clojure!” output. Starting the JVM is expensive, the runtime of this simple basic script is more than 4 seconds on my laptop. But I’m pretty sure that once the JVM is loaded, it’s fast.

Links

Categories: programming language Tags:

Cobol Tutorial

January 14, 2013 Leave a comment

Just for fun: Cobol tutorial.

To be clear, I don’t ever want to touch Cobol. This is just for the record :)

Categories: fun, programming language Tags: ,

Side-by-side comparisons of programming languages