Summary of Hack

Hey yall ,

 

It’s been real fun showing you the features of Hack let’s just quickly review.

Hack is a backend web language. Hack was designed by Facebook and is used throughout the site as an improvement to PHP. Hack programs run on the HHVM (Hip-Hop Virtual Machine). Much like Java, Hack has collections which can take generics (Your own datatype). Most interesting I found about the collection I used Vector was that you can access data from it the same you would an array in Java or any other language like it (vector[index]). Hack functions unlike PHP functions can return datatypes. Hack uses TypeChecker to check the code.

 

Hack uses classes and other object oriented concepts such as inheritance and polymorphism.

 

HHVM is only compatible on Linux operating systems.

 

Note: I despite the fact that the official Hack documentation indicates that Facebook has created for Hack called Nuclide. It appears this Atom plugin is not in a working state now. Nuclide is a plugin (Hack) for the Atom text editor.  If you are able to get Nuclide installed could you please comment ,and share your installation procedure with all of us please.

 

 

 

Hack Project Part III (ATM)

Hello it’s Niko today is the day I will show you the ATM program I have created in Hack. I initially designed in Java a few weeks ago to take user input. However, I realize Hack, a back end web language does not take user input how most languages we are familiar with but rather takes user input from an HTML form. For this project I wanted the Hack language to be the center focus , so I have will not be providing input when I run the ATM but rather it will be provided in the source.

 

First I’ll show you the accounts in the bank then we’ll start with the demonstration.

Accounts on File
The accounts of Niko and George are added to the accounts of the bank.

I am showing you this code so there isn’t questions of why is the balance this and not . It’s because I decided to make this problem kind of real and gave this guy Niko an initial balance of $5.

Now to demonstrate I will login into Niko’s Account ,make a deposit and a withdrawl.

Setup Deposit
We are making deposit of $25 into our
Account Operations
Account Operations to apply to our account

This was an interesting project to work on.

Hack Project (ATM) Part II

Hello, Welcome back today I can happily tell you that my ATM program is nearly complete. Today I’d like to share my code, go through the design of my program , and show you some object oriented programming in Hack.

 

Here is a link to my code I have been working on.

Source File of my project

Feel free to download the file and try to run it.

 

This program features 4 classes . They are BankAccount, NewBankAccount, AccountSession, and Bank. AccountSession is a BankAccount that is being used currently it must be authenticated (correct name and pin) in order to instantiate. NewBankAccount is a BankAccount that describes accounts that are new to this bank therefore they (the user ) would input their initial balance as well as name and pin.  BankAccount contians a name , pin , and balance.

The Bank class holds the information  on all BankAccounts through a Vector. A vector in Hack is a collection much like in Java collections and can take types of data that we create and that is what I did here. I created a vector of BankAccount.

 

Hack supports inheritance by using the keyword extends much like in Java. According to the official Hack tutorial there is also support for Interfaces, but I didn’t feel the need to use one for this project.

 

Referencing methods bound by the object is done by objectname->methodname. If the object is not bound or static we reference the syntax is slightly different classname::methodname.

 

Next week I will demonstrate this ATM for you all.

 

 

Hack Project(ATM) Part I

To show all what I have learned about Hack I have decided to create a project involving Hack.

The project I will be working on will  simulate an ATM. This program will allow authenticated users to deposit and withdraw from their bank accounts. Additionally, if the user overdrafts from their or withdraws more money from their account than they currently have then a fee will be imposed on their account. The balance shall increase when the user deposits money and decrease when a withdrawal occurs. The account will be authenticated by communicating with the Bank (class). If during authentication the AccountSession is unable to authenticate itself then a message will alert the user. The Bank will not assume all accounts have 0 balance, initial balance will be what the user has declared as their initial balance once the account has been created.

 

 

Hopefully you’ll find this project interesting and learn some interesting aspects of Hack along the way. Join me on this magnificent journey as I work to create an ATM based application.

 

Below is a link to my code I am working on for this project

https://1drv.ms/u/s!Aklz7yNfyhN5oO5jLauAT4Q4GxVlHA

This code is from a working prototype in Java I have created.

 

 

 

Setting Up Hack

Hello, and welcome to another exciting blog post about the Hack programming language. Today I will show you how to setup and install the Hip-Hop Virtual Machine to allow you to start using the Hack programming language. Before we begin I must inform you that HHVM (Hip-Hop Virtual Machine) is only available for Linux distributions. If you do not have a Linux machine like many of us, then I would encourage to use a virtual environment. I use a free tool called VirtualBox to run the Ubuntu operating system while I’m using Windows.

 

Here is a link to download VirtualBox if you’d like to use it

https://www.virtualbox.org/wiki/Downloads

Here is a link to Ubuntu OS downloads (my preference Linux distrobution)

https://www.ubuntu.com/desktop/1710

 

Feel free to ask any questions on installation if you should run into any problems.

 

From then we are going to open execute our virtual machine, start up Ubuntu and open the terminal application.

Openning terminal

Now that we are in the terminal let’s install the HHVM. In the terminal type

apt-get install hhvm. Hit enter and hhvm should install on your machine.

install hhvm from terminal.png

To test if you have successfully installed HHVM type hhvm -m server -p 8080. This calls the package that is associated with the HHVM and gives it parameters m which specifies the mode in this case the hhvm will be in server mode. The p parameter is the port at which this hhvm will communicate from.

HHVM activation

Congratulations, you have successfully setup and installed hhvm , you can now freely experiment with Hack. Now you too can join in on the fun of Hack !

 

 

What’s so cool About Hack ?

Hack is an interesting scripting language for how it can seemlessly integrate itself with PHP. This makes learning Hack for someone who already has knowledge of PHP. Hack also shares some similarities to JavaScript in declaring functions.

 

Just making the syntax similar to another language doesn’t set it apart from the rest. Hack has a type checker which checks your code and you if not just if the code is the bad but if there are things that are unnecessary within it. Type checker can inform you upon execution of Hack programs that if variables are never used. This is a great feature to have considering  it could happen that someone misspelled a variable they had already created and instead has now created a new variable that is never used. In TypeChecker has 3 modes, default which I have briefly mentioned, strict, and off. Strict mode demands clean and sound code otherwise it will not execute. Off simply assumes all the code is good up until a syntax error occurs. This setting can be changed when turning on the HHVM (Hip-Hop Virtual Machine) by changing the value assigned to the d parameter.

 

Below I have started the HHVM with TypeChecker off in the terminal.

Typechecker setup

This setting is not recommended, but is the genuine way to turn off TypeChecker. If you want more information on the Hack programming language please visit hacklang.org.

Binary Search Implementation

Hello today , I will show you an implementation of binary search I have constructed using the Hack programming language. Binary search is a very efficient searching procedure in which we have comparisons from the middle and will either go to the left of the middle, the right , or we are the middle and therefore we stop. By using binary search eliminate nearly half the elements in the first pass.

 

In this implementation I created an object of the Vector class provided in Hack.

BinarySearch
Code for Hack binary search

First we compute the middle index then check if we are the value at the middle index. Note: During all the comparisons I realized that Hack will only accept integers as index values. To overcome this I casted the middle to an integer for each of these comparisons.

If it is not the middle then it will perform binary search on the left side of the vector. Next we perform binary search on  the right side of the vector. We do this until we find our value and return mid or we the element fails to exist in the vector and we return -1.

binarysearch_output
Our binary search program outputs letting us know that the integer 5 is located at index 3

 

 

Functions with Recursion

Today I will be showing you all how functions are defined and executed in the Hack programming languages. I will be using a recursive function in this example. More specifically this example will a given number to its equivalent to the Fibonacci sequence.

 

Functions in are defined using the function keyword followed by parameters (in parentheses )and then colon the type of data to return.

Fibonacci numbers are commonly associated with recursion. The fibonacci sequcence consists of summing the previous numbers of the current number.

Below is the Hack code for fibonacci notice I take an integer parameter and return one.

 

Fibonacci_code
This program computes the Fibonacci of 15

 

As you can see it is recursion by calling itself to come to an eventual answer. I say eventual answer because this is not the most efficient way to solve this problem.

 

Below is the output to the recursive program I have constructed.

 

Answer
The program computes the answer

There it is the program computed the Fibonacci of 15 as 610. Tune in next week for more excitement on the Hack language

Looping

Today I’ll show you how to perform iteration in the hack programming language.

Iteration in the Hack language is very much like that in other programming languages. There is a counter variable initialized , a condition involving the counter , and some means of changing the counter variable so the loop may end. This is very similar to many of you who are familiar with other languages such as Java, C++, and especially PHP.

Below is Hack code for a program that will print out a sequence of numbers using a for loop.

Loop Code
Hack iteration code is nearly identical to that of other languages !!!

 

Iteration in Hack is fairly straight forward. Here is the output to my code.

Output
The program outputs the digits 0 through 10 using a for loop

 

 

 

Next week I’ll cover foreach loops and functions in Hack.

For  more information on Hack please visit hacklang.org (the official site of the Hack language). Please note that for now at least Hack is only available to use on Linux based operating systems.

IF Statements

Now we know how to construct variables and functions in a Hack program. It would be nice to have some logical controls such as IF statements.

 

In Hack  an if statement is syntactically the same as one in C++ or Java. The keyword “if” followed by parentheses  that have the condition inside and open /closing braces which contain instructions to do if the the condition succeeds.

Chaining if statements is also the same as in Java and C++ “else if(x > 1)

do this;. Below is a program that uses to a small chain of if statements to determine if a number is even or odd. Java developers may really like Hack because of its similarities but also appears to be more ideal for web content retrieval.

 

Is Even or odd code
This Hack program tests if the number 4 is even or odd
Output Number state
The Number is an even number so therefore it prints “This number is even”