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

Running your own one person company

Recently there was a post on PuneTech on mom's re-entering the IT work force after a break. Two of the biggest concerns mentioned were : Coping with vast advances (changes) in the IT landscape Balancing work and family responsibilities Since I have been running a one person company for a good amount of time, I suggested that as an option. In this post I will discuss various aspects of running a one person company. Advantages: You have full control of your time. You can choose to spend as much or as little time as you would like. There is also a good chance that you will be able to decide when you want to spend that time. You get to work on something that you enjoy doing. Tremendous work satisfaction. You have the option of working from home. Disadvantages: It can take a little while for the work to get set, so you may not be able to see revenues for some time. It takes a huge amount of discipline to work without a boss, and without deadlines. You will not get the benefits (insuranc

Testing Groovy domain classes

If you are trying to test Grails domain class constraints by putting your unit test cases in the 'test/unit' directory, then your tests will fail because the domain objects will not have the 'valdate' method. This can be resolved in two ways: Place the test cases inside test/integration (which will slow things down) Use the method 'mockForConstraintsTests(Trail)' to create mock method in your domain class and continue writing your test cases in 'test/unit' What follows is some example code around this finding. I am working on a Groovy on Grails project for a website to help programmers keep up and refresh their skills. I started with some domain classes and then moved on to write some unit tests. When we create a Grails project using grails create-app , it creates several directories, one of which is a directory called 'test' for holding unit tests. This directory contains two directories, 'unit', and 'integration' for unit and

Planning a User Guide - Part 3/5 - Co-ordinate the Team

Photo by  Helloquence  on  Unsplash This is the third post in a series of five posts on how to plan a user guide. In the first post , I wrote about how to conduct an audience analysis and the second post discussed how to define the overall scope of the manual. Once the overall scope of the user guide is defined, the next step is to coordinate the team that will work on creating the manual. A typical team will consist of the following roles. Many of these roles will be fulfilled by freelancers since they are one-off or intermittent work engagements. At the end of the article, I have provided a list of websites where you can find good freelancers. Creative Artist You'll need to work with a creative artist to design the cover page and any other images for the user guide. Most small to mid-sized companies don't have a dedicated creative artist on their rolls. But that's not a problem. There are several freelancing websites where you can work with great creative ar