Friday, 17 November 2017

Introduction to Brainfuck

Brainfuck is a programming language created by Urban Müller in 1993. Brainfuck comes under esolang (esoteric programming language) category.

What is esoteric programming language?
As per Wikipedia “An esoteric programming language (sometimes shortened to esolang) is a programming language designed to test the boundaries of computer programming language design, as a proof of concept, as software art, as a hacking interface to another language (particularly functional programming or procedural programming languages), or as a joke.”

Reference

Architecture of Brainfuck
Brainfuck consist of
a.   Array of at least 30000 bytes, all are initialized to zero by default
b.   A data pointer, used to move around the 30000 byte array. At the time of program initialization, it points to left most byte of the array
c.   Two streams of bytes for input and output

Commands
Brainfuck programming language operates with only eight commands. Below table summarizes all the eight commands.

Command
Description
> 
Move the pointer to next cell in the right
< 
Move the pointer to previous cell in the left
+
Increment the byte at the pointer
-
Decrement the byte at the pointer
.
Output the byte at the pointer
,
Input a byte and store it in the byte at the pointer
[
if the byte at the data pointer is zero, then instead of moving the instruction pointer forward to the next command, jump it forward to the command after the matching ] command.
]
if the byte at the data pointer is nonzero, then instead of moving the instruction pointer forward to the next command, jump it back to the command after the matching [ command.

What are the brainfuck implementations?
There are many open source implementation available to experiment with brainfuck language. Refer https://esolangs.org/wiki/Brainfuck_implementations

Can I run brainfuck code in online?
Yes, there are many available. I am going to use below one.
Previous                                                 Next                                                 Home

Brainfuck Tutorial


      Introduction to Brainfuck
      Setting up Visual brainfuck
      Brainfuck: Hello World application
      Brainfuck: Read value
      Brainfuck: Read and print infinite data
      Brainfuck: print ASCII Table
      Brainfuck: Convert ASCII to decimal number
      Brainfuck: Sum of two numbers
      Brainfuck: Double a number

Previous                                                 Next                                                 Home

Thursday, 16 November 2017

Setting up Kotlin in Eclipse

Open Eclipse. Help -> Eclipse Marketplace.
Search for kotlin.
Click on the button ‘Install’ to install the kotlin plugin.

Once the installation is successful, Eclipse asks for a restart. Restart the Eclipse.

Create new Kotlin Project
Open Eclipse. File -> New -> Other.
Kotlin -> Kotlin Project.
Press Next.

In the ‘Kotlin Project’ window, give the project name as ‘Hello World’ and click on the button Finish.

Kotlin project structure looks like below.
Create HelloWorld.kt file
Right click on ‘src’ folder -> New -> Other.
Select ‘Kotlin File’ and press Next.
Give the file name as ‘HelloWorld’ and press the button ‘Finish’.
Copy below snippet to HelloWorld.kt file.

HelloWorld.kt
fun main(args: Array<String>) {
 println("Hello World")
}


Run HelloWorld.kt file
Right click on source code of HelloWorld.kt file -> Run As -> Kotlin Application.
If everything goes well, you can see the message ‘Hello World’ in console.



Previous                                                 Next                                                 Home

Wednesday, 15 November 2017

Kotlin: Hello World program

 ‘.kt’ is the extension for kotlin files. Create a file ‘HelloWorld.kt’ with below content.

HelloWorld.kt
fun main(args:Array<String>){
 println("Hello World")
}

Keyword ‘fun’ is used to define a function.
‘main’ is the main function of the application. Program execution starts from here.

args:Array<String>
In Kotlin variable name is declared first, followed by the type. In above case, args is variable of type array of strings.

println("Hello World")
Above statement prints the message ‘Hello World’ to the console. As you see, unlike java, Kotlin statements do not end with ‘;’.

Compile HelloWorld.kt
Open command prompt and type the command ‘kotlinc HelloWorld.kt’.

C:\Users\Krishna\Documents\Study\Kotlin\Programs>kotlinc HelloWorld.kt

C:\Users\Krishna\Documents\Study\Kotlin\Programs>dir
 Volume in drive C is OSDisk
 Volume Serial Number is 1034-4F6F

 Directory of C:\Users\Krishna\Documents\Study\Kotlin\Programs

09/02/2017  11:18 AM    <DIR>          .
09/02/2017  11:18 AM    <DIR>          ..
09/02/2017  11:09 AM                57 HelloWorld.kt
09/02/2017  11:18 AM               980 HelloWorldKt.class
09/02/2017  11:18 AM    <DIR>          META-INF
               2 File(s)          1,037 bytes
               3 Dir(s)  63,190,188,032 bytes free

As you see the output of dir command, you can observe, kotlin generates ‘HelloWorldKt.class’ and a META-INF directory.

Run HelloWorld.kt
You can run the HelloWorldkt file using ‘kotlin HelloWorldKt’ command.

C:\Users\krishna\Documents\Study\Kotlin\Programs>kotlin HelloWorldKt
Hello World

Creating and run jar
You can also create a jar file and run the jar file using java launcher.

Use the command ‘kotlinc HelloWorld.kt -include-runtime -d HelloWorld.jar
‘ to create a jar file.


The ‘include-runtime’ option tells kotlin compiler, include kotlin run time in HelloWorld.jar file.

C:\Users\Krishna\Documents\Study\Kotlin\Programs>kotlinc HelloWorld.kt -include-runtime -d HelloWorld.jar

C:\Users\Krishna\Documents\Study\Kotlin\Programs>dir
 Volume in drive C is OSDisk
 Volume Serial Number is 1034-4F6F

 Directory of C:\Users\Krishna\Documents\Study\Kotlin\Programs

09/02/2017  11:24 AM    <DIR>          .
09/02/2017  11:24 AM    <DIR>          ..
09/02/2017  11:24 AM           869,262 HelloWorld.jar
09/02/2017  11:09 AM                57 HelloWorld.kt
               2 File(s)        869,319 bytes
               2 Dir(s)  63,401,816,064 bytes free


Once the jar file is created, you can call the jar file, just like how you run other jar files. Use the command ‘java -jar HelloWorld.jar’

C:\Users\Krishna\Documents\Study\Kotlin\Programs>java -jar HelloWorld.jar
Hello World




Previous                                                 Next                                                 Home

Tuesday, 14 November 2017

Installing Kotlin in command line mode

Download kotlin from below location. At the time of writing this article, 1.1.4 is the latest version.

Unzip the downloaded zip file, and add the bin directory to system path.

Open new command prompt and type ‘kotlinc’ command.

C:\>kotlinc
Welcome to Kotlin version 1.1.4 (JRE 1.8.0_102-b14)
Type :help for help, :quit for quit
>>>

As you see above output, it shows the kotlin version and underlying jre version.

Type the command ‘:help’ to list available commands.

C:\>kotlinc
Welcome to Kotlin version 1.1.4 (JRE 1.8.0_102-b14)
Type :help for help, :quit for quit
>>>

You can evaluate expressions in Kotlin prompt.

>>> var a =10
>>> var b = 20
>>> a + b
30
>>> a-b
-10
>>> a*b
200
>>> a/b
0
>>> println(a)
10
>>> println("value of b : " + b)
value of b : 20




Previous                                                 Next                                                 Home

Install Kotlin

Kotlin can be installed in command line mode (or) can be installed in IDEs like Eclipse, Intelij.
b.   Install Kotlin in Eclipse
c.   Install Kotlin in Intelj
Previous                                                 Next                                                 Home

Monday, 13 November 2017

Why Generic types in Java are invariant?

Generic types in Java are invariant. That means, if T2 is a subtype of T1, List<T2> is not a sub type of List<T1>.


Ex
Integer is a subtype of Number.

But Stack<Integer> is not a sub type of Stack<Number>.

Why java makes generics as invariant?
Let me explain with an example. 
  /* Define a character list */
  List<Character> chars = new ArrayList<Character>();

  /* Java do not allow this */
  List<Object> objs = chars;

  /* Adding double to character list */
  objs.add(1.23);

  /* ClassCastException: Cannot cast double to Character */
  Character s = chars.get(0);

As you notify above snippet. I defined a character list ‘chars’ and cast it to list of objects ‘objs’. I added a double to ‘objs’. Last statement cast the double to character, which is not possible. To prohibit these kind of things and ensure run time safety of the application, java generics are invariant.

You may like