Donnerstag, 18. Oktober 2012

Functional Programming Misunderstandings

Functional Programming Misunderstandings

While talking with friends and colleagues I noticed that there are (at least)  three widespread misunderstandings associated with the term "functional programming". In this post I will state those misunderstandings and explain for each one of them, why it is not true.
  1. The primary utility of functional programming is the ability to pass functions as parameters to other functions (higher order programming; functions as first-class citizens).
  2. Functional programming cannot be achieved in imperative programming or object oriented programming languages such as Java, Python or C.
  3. Functional programming is just of academic interest and quite useless in practice for its inability to manipulate state.
The following paragraphs explain what is wrong with the statements above.

Primary Utility of Functional Programming

While passing functions as parameters is very beneficial to write clean and short code by using higher order functions such as fold,  filter and map [2] known from functional languages such as Haskell or Scheme, it is not the most important aspect of functional programming.

Instead the most fundamental and beneficial part of functional programming is the isolation of side effects and the explicit flow of information over function parameters and function return values. This isolation of side effects makes code easier to understand, refactor and maintain. Moreover it allows for easier parallelization, which cannot be overestimated using the emergence of multicore processors and multi-machine clusters over the past years. This isolation of side effects also reduces the use of global variables, which are considered evil when they are used in cases where local variables can be used [1].

Functional Programming in Non-Functional Languages

While most imperative and object oriented programming languages, functions cannot be used as values (parameters and return values of other functions), higher order programming can be mimicked using function objects, i.e. objects with only a single method. The Guava Java library [5] adds support for higher order programming by adding function objects, and custom implementations for map and filter.

Yet not only higher order programming can be achieved by using function objects but side effects can be isolated just in the same way as functional programming languages enforce isolation of side effects by not supplying a global state for a program under execution. By reducing the number of functions that modify their parameters or modify global variables, we can make programs just as easy to refactor and reason about as programs written in functional languages are.

Use of Functional Programming in Industry

While originally invented as a research language, Haskell has gained significantly more users in industry over time. The Haskell Wiki has an impressive list of applications that were written in Haskell [3]. Also functional programming (i.e. reduction of side effects, use of pure functions) in imperative or object oriented languages, and using higher order programming in Ruby, Javascript, Scala, etc to remove boiler-plate code is getting more and more popular since the advantages of this coding style have been accepted by mainstream programmers.

References



Dienstag, 16. Oktober 2012

Copying Data between Mongo Databases Using Javascript only

Copying Data between Mongo Databases Using Javascript only

MongoDB is a very popular noSql database with built in replication, horizontal scalability (sharding), Json as native data format and Javascript as the native query language. This blog post deals with copying data between mongo databases.

There are various methods for copying data between different MongoDB databases. The following possibilities come to my mind:
  • Using mongodump and mongorestore. This method is very fast and can be used to copy individual collections or the entire database from the command line.
  • Using mongoexport and mongoimport. This can be used to export data selected by a custom query or entire collections from the command line.
  • Using some programming language of your choice with a mongo driver.
  • Using the mongo shell and the use <database> or the db = connect("...")primitive.
The last solution is very convenient in that it can be used directly from Javascript, and gives you control such as counting the number of documents to copy. Below is an example use case:

> mongo localhost/test
PRIMARY> db.bla.find()
{ "_id" : ObjectId("507d19c4ce52c92d953fe8a1"), "1" : "one" }
{ "_id" : ObjectId("507d19cece52c92d953fe8a2"), "2" : "two" }
PRIMARY> var docs = db.bla.find()
PRIMARY> docs.size()
2
PRIMARY> use test2
switched to db test2
PRIMARY> while(docs.hasNext()) { db.blo.insert(docs.next()); }
PRIMARY> db.blo.find()
{ "_id" : ObjectId("507d19c4ce52c92d953fe8a1"), "1" : "one" }
{ "_id" : ObjectId("507d19cece52c92d953fe8a2"), "2" : "two" }
PRIMARY>

The above session shows how to interactively copy data between two databases on the same host. Note that this can also be done for databases on different machines or databases running on different ports  of the same machine. In this case you would need to switch the connection using db = connect("www.example.org:7777/mydb") or similar instead of the use primitive. 

Also note that you cannot use the iterator ( docs.find())  before the data is inserted into the target collection, otherwise you will loose data.