Skip to main content

Fuctional Programming Principles in Scala - Getting Started

Sometime back I registered for the Functional Programming Principles in Scala, on Coursera. I have been meaning to learn Scala from a while, but have been putting it on the back burner because of other commitments. But  when I saw this course being offered by Martin Odersky, on Coursera, I just had to enroll in it.

This course is a 7 week course. I will blog my learning experience and notes here for the next seven weeks (well actually six, since the course started on Sept 18th).

The first step was to install the required tools:
  • JDK - Since this is my work machine, I already have a couple of JDK's installed
  • SBT - SBT is the Scala Build Tool. Even though I have not looked into it in detail, it seems like a replacement for Maven. I am sure we will use it for several things, however upto now I only know about two uses for it - to submit assignments (which must be a feature added by the course team), and to start the Scala console. Installed sbt from here, and added the path to it's bin directory in my .bashrc file. When I first started sbt, I did not realize that sbt would need to connect to the Internet to download it's dependencies. My computer was disconnected from the Internet, which caused sbt to give up. This is the first time I realized that sbt seems to be doing something that I would expect Maven to do.
  •  Next, I installed the Scala IDE, which seems to be Eclipse with a Scala plugin. Since I already have Eclipse, I wonder if I cold have simply installed a plugin.
Once I had the tools setup ready, I created a workspace for all my DIYLearning courses (since I plan to always be enrolled on at least one course, from Coursera, Udacity, edX, or any other open courseware offering that is good).

Then I downloaded the example project, made a few modifications to it, ran unit tests and submitted it. I learned quite a few things by wandering around this example project.

Directory structure is similar to Maven:
The directory structure of the projects is similar to what it might have been had I used Maven. The sources were in 'src/main/scala', and tests in 'src/test/scala'. sbt seems to have a directory structure similar to Maven. I like that. It does not make sense to relearn everything from scratch for every single tool.

Scala imports are slightly different
This is how Scala imports everything from a namespace. Notice how they have deviated from Java imports by using the '_' character for the wildcard. I asked a question on the forum, and was told that functional programming languages by convention use the '_' character for wildcards. So ok, that too makes sense.

import Lists._


Unit tests in Scala
Unit tests in Scala look similar to unit tests in Java, but there are a few things which stand out as different. Scala uses ScalaTest, as the unit testing tool in Scala. To create a ScalaTest test case, we have to extend FunSuite. Their website also mentions something about traits. I have heard this term before, but do not really know what it is. Need to look into what it really means. I also noticed that ScalaTest's have only one type of assert, which is equivalent to assertTrue. This also makes sense to me, because everything can be reduced to an assertTrue assertion.

ScalaTest introduces an operator called 'test'. This operator takes two arguments, the first one being the name of the test, and the second one being the body of the test. The body usually consists on an assert. Here is how a test operator looks

test("testing equality")(assert(1==1))


The body of the test can also be given as a block, which makes it more readable.

test("testing equality") {assert(1==1)}



Comments

Thuong Nguyen said…
một số người lao động tại công ty Kim Quý đang làm công việc nhúng trái sầu riêng vào hóa chất ép chín trái cây thì bị chóng mặt, mẩn ngứa, cay mắt, nôn ói.

Sau lúc công an huyện can thiệp thì chủ hạ tầng mới cho đoàn liên lĩnh vực vào. đồng thời, điều khó hiểu là khi liên hệ với các người bị dị ứng keo tu PAC thì họ đều từ chối tiếp xúc.

Về lý do tại sao phải sử dụng hóa chất ép chín trái cây, ông Luận nói do trái cây ko cộng chín một lúc nên phải sử dụng thuốc để cho trái chín đều, đẹp.

Popular posts from this blog

My HSQLDB schema inspection story

This is a simple story of my need to inspect the schema of an HSQLDB database for a participar FOREIGN KEY, and the interesting things I had to do to actually inspect it. I am using an HSQLDB 1.8 database in one of my web applications. The application has been developed using the Play framework , which by default uses JPA and Hibernate . A few days back, I wanted to inspect the schema which Hibernate had created for one of my model objects. I started the HSQLDB database on my local machine, and then started the database manager with the following command java -cp ./hsqldb-1.8.0.7.jar org.hsqldb.util.DatabaseManagerSwing When I tried the view the schema of my table, it showed me the columns and column types on that table, but it did not show me columns were FOREIGN KEYs. Image 1: Table schema as shown by HSQLDB's database manager I decided to search on StackOverflow and find out how I could view the full schema of the table in question. I got a few hints, and they all pointed to

Five Reasons Why Your Product Needs an Awesome User Guide

Photo Credit: Peter Merholz ( Creative Commons 2.0 SA License ) A user guide is essentially a book-length document containing instructions for installing, using or troubleshooting a hardware or software product. A user guide can be very brief - for example, only 10 or 20 pages or it can be a full-length book of 200 pages or more. -- prismnet.com As engineers, we give a lot of importance to product design, architecture, code quality, and UX. However, when it comes to the user manual, we often only manage to pay lip service. This is not good. A usable manual is as important as usable software because it is the first line of help for the user and the first line of customer service for the organization. Any organization that prides itself on great customer service must have an awesome user manual for the product. In the spirit of listicles - here are at least five reasons why you should have an awesome user manual! Enhance User Satisfaction In my fourteen years as a