Sunday, September 4, 2011

Right question in scala world

When I started some real small project using scala. I read some rows of data from database and as usual wanted to
save it in some list. So first question appears in my mind
How to add some element to list ?
def testPersons(): List[Person] = {
    val conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl")
    var persons = new List[Person]()
    try {
     val statement = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)
     val rs = statement.executeQuery("SELECT * FROM Person where user_name like '%test%'")
     while (rs.next) {
       personsBuf.add(new Person(rs.getString("user_name"), rs.getString("first_name"), rs.getString("last_name")))
     }
    } finally {
     conn.close
    }
    persons
 }
 but after some investigation I realized the question is completely wrong.
 Philosophy of scala differ from philosophy of java. and first of all you must think immutably.
 You create list in variety of ways
 val fruit = List("apples", "oranges", "pears")
val nums = List(1, 2, 3, 4)
val diag3 = List(List(1, 0, 0), List(0, 1, 0), List(0, 0, 1))
val empty = List()
val fruit = "apples" :: ("oranges" :: ("pears" :: Nil))
val nums = 1 :: (2 :: (3 :: (4 :: Nil)))
val diag3 = (1 :: (0 :: (0 :: Nil))) ::
(0 :: (1 :: (0 :: Nil))) ::
(0 :: (0 :: (1 :: Nil))) :: Nil
val empty = Nil
but you after creating you can not change it because it is immutable. You can only create new one. It is like string in java.
the right way of my function is :
def testPersons(): List[Person2] = {
    val conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl")
    var personsBuf = new scala.collection.mutable.ListBuffer[Person2]()
    try {
     val statement = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)
     val rs = statement.executeQuery("SELECT * FROM Person where user_name like '%test%'")
     while (rs.next) {
       personsBuf += new Person2(rs.getString("user_name"), rs.getString("first_name"), rs.getString("last_name"))
     }
    } finally {
     conn.close
    }
    personsBuf.toList
 }
I really need to use ListBuffer like mutable list inside function and than return immutable list to outside world.
And you can relay to this list on any place of you program, no one can change it or corrupt it. It is immutable.
It is scala world.  

Saturday, July 23, 2011

There are things
I have done
There's a place
I have gone
There's a beast
And I let it run
Now it's running . . .
My way

There are things
I regret
To can't forgive
You can't forget
There's a gift
That you sent
You sent it . . .
My way

(Chorus)
So take this night
Wrap it around me like a sheet
I know I'm not forgiven
But I need a place to sleep
So take this night
And lay me down on the street
I know I'm not forgiven
But I hope that I'll be given . . .
Some peace

There's a game
That I play
There are rules
I had to break
There's mistakes
That I made
But I made them . . .
My way

(chorus)
So take this night
Wrap it around me like a sheet
I know I'm not forgiven
But I need a place to sleep
So take this night
And lay me down on the street
I know I'm not forgiven
But I hope that I'll be given . . .
Some peace . . .
Some peace . . .
Some peace

http://www.youtube.com/watch?v=8cucFfpsqf8

Wednesday, June 29, 2011

The Tao Te Ching (2)

When people see some things as beautiful,
other things become ugly.
When people see some things as good,
other things become bad.

Being and non-being create each other.
Difficult and easy support each other.
Long and short define each other.
High and low depend on each other.
Before and after follow each other.

Therefore the Master
acts without doing anything
and teaches without saying anything.
Things arise and she lets them come;
things disappear and she lets them go.
She has but doesn't possess,
acts but doesn't expect.
When her work is done, she forgets it.
That is why it lasts forever.

Sunday, May 8, 2011

The longer I live, the more I realize the impact of attitude on life.

Attitude, to me, is more important than facts. It is more important than the past, than education, than money, than circumstances, than failures, than successes, than what other people think or say or do. It is more important than appearance, giftedness or skill. It will make or break a company... a church... a home.

The remarkable thing is we have a choice every day regarding the attitude we will embrace for that day. We cannot change our past... we cannot change the fact that people will act in a certain way. We cannot change the inevitable. The only thing we can do is play on the one string we have, and that is our attitude... I am convinced that life is 10% what happens to me and 90% how I react to it.

And so it is with you... we are in charge of our attitudes.

by: Charles Swindoll