Monthly Archives: November 2014

This is online course, named functional programming principles in Scala. I use this course to practice my Scala programming. So here I do summary of my solutions according to its assignments online. Please I don’t follow its restriction, I only want to implement my solution by Scala. Don’t compare my idea with their online course exactly. Follow your heart. 🙂

Exercise 1: Pascal’s Triangle.

Do this exercise by implementing the pascal function, which takes a column c and a row r, counting from 0 and returns the number at that spot in the triangle. For example,pascal(0,2)=1, pascal(1,2)=2 and pascal(1,3)=3

Exercise 3: Counting Change

Write a recursive function that counts how many different ways you can make change for an amount, given a list of coin denominations. For example, there are 3 ways to give change for 4 if you have coins with denomination 1 and 2: 1+1+1+1, 1+1+2, 2+2.

JDBC means Java Database Connection. So when we need to read data from database, we use JDBC to get connection between application layer and database layer. In fact, we can directly connect with database by JDBC, why we need to use connection pool. The benefit of it is that a connection pool is a cache of database connections maintained so that the connections can be used when future requests to the database are required. Connection pools are used to enhance the performance of executing commands on a database.

Benefit

Dynamic web pages without connection pooling open connections to database services when they are needed and close them when the page is done servicing a particular request. Pages that use connection pooling instead maintain open connections in a pool. When the page requires access to the database, it simply uses an existing connection from the pool, and establishes a new connection only if no pooled connections are available. This reduces the overhead associated with connecting to the database to service individual request. To summarize, there are three advantages:

In connection pool, connection or set of connection objects are created single time (usually at the start of the application)

The connections can be reused when future requests to the database are required.

It can enhance the performance of executing commands on a database.

Here our target database is Hive, first we need to start hive. Its default port is 10000.

./hive --service hiveserver -p 10002
Starting Hive Thrift Server

So you will see that you already start hiveserver on 10002, and then you can use java proxy to connect hiveserver.

There are several methods to do this thing; here we only introduce two ways, BoneCP and c3p0.

BoneCP

The reason why we use BoneCP is that Play 2.0 JDBC datasource is managed by it.

Accessing the JDBC datasource

If you use default configuration, you only need to use “DB.withConnection” to get connection.

c3p0

Configuration

The build.sbt part is partially same with BoneCp. We need to add one more.

"com.mchange" % "c3p0" % "0.9.2.1"

Accessing

import com.mchange.v2.c3p0.ComboPooledDataSource
// implement a datasource
val cpds = new ComboPooledDataSource()
cpds.setDriverClass(...)
cpds.setJdbcUrl(...)
cpds.setUser(...)
cpds.setPassword(...)
cpds.setInitialPoolSize(...)
// when number of connections is over than this value, you only can wait for the connection until other connection is free.
cpds.setMaxPoolSize(...)
cpds.setMinPoolSize(...)
cpds.setMaxStatements(...)
// get a connection
cpds.getConnection
cpds.getJdbcUrl

Scala provides so many powerful collections, but how to select the suitable in different scenario. We need to consider several factors.

Hierarchy

Iterable[T] is any collection that can be iterated over, they provide an iterator method which gives us an Iterator to loop the elements) and foreach, Seq[T]s are collections that are ordered, Set[T]s are mathematical sets (unordered collections of unique items), and Map[T]s are associative arrays, also unordered.

Parametric polymorphism

Obviously, this function can be applied in Int, String and etc. We don’t need to write down two or more functions to satisfy different types; we only use type parameter to give a generic method to implement the function. It saves our time.

And then we talk about in some conditions, we don’t need to point out its type. Scala helps us to infer its type.

As you seen, it is quite easy for us to understand the return type; we don’t need to point out its type by ourselves.

It seems type system is so easy, right? No. Another problem is coming. If T” is a subclass of T, what is relationship between Container[T] and Container[T”]? Let’s talk about variance to express the relationship.

view bounds

To be honest, I don’t know why its name is “view bounds”, here I only talk what I know. “view bounds” is related to implicit class; it demand a function exists for the given type. We can use “<%” to specify a view bound. Here is the example:

Before we enter into “Question and Answer” model, we need to know how to use LinkedList in Scala. In Scala, it already provides LinkedList collection directly. by “import scala.collection.mutable.LinkedList”.

2.4 You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored in reverse order, such that 1’s digit is at the head of the list. Write a function that adds the two numbers and returns the sum as a linked list.

Example:

Input: (3->1->5), (5->9->2)

Output: 8->0->8

This question looks like to implement an add function for linked list.

I use Play Framework to build web application. I also use Akka and Spark in my projects. Currently, my programming language is Scala, even though in the past, I used C/C++, Python, and etc. But I’m still not really understand what is Typesafe Reactive Platform. In this post, I want to reveal its truth.

What is the Problem?

Every solution should try to solve one problem. So what is the problem which Typesafe Reactive Platform wants to deal with?

In the past, a large application had tens or so servers, seconds of response time, hours of offline maintenance and gigabytes of data. Old solutions emphasized on managing servers and containers. Scaling was achieved through buying more larger servers and concurrent processing via multi-threading.

But today applications are deployed on cloud-based clustering running thousands of multicore processors, dealing with thousand requests and handling petabytes. So old traditional scaling method is not suitable for new updated requirements.

What is Reactive Platform?

Because old solution is not good enough, we introduce a new solution, named Reactive Platform. This platform allows developers to build systems which are responsive resilient, elastic, and message-driven in order to deliver highly responsive user experiences with a real-time feel, backed by a elastic and resilient application stack, ready to be deployed on multicore and cloud computing architectures.

So Typesafe Reactive Platform is this kind of platform, which contains Play Framework, Akka, Scala, Activator and Spark. It is powerful tool to build modern applications that react to events, react to load, react to failure, and react to users.