Scala for loop

0 comments

In my previous post I've explained how to use while and do-while loops. In today's post I will show you how to use a for loop. For loop allows you to do quite a lot. It was designed to iterate through a collection, do range iterations, producing a new collection, perform nested iterations easily, filter and mid-stream variable bindings. I will skip mid-stream variable binding and producing new collections since these are advanced topics. I will explain them in the future.


Iterate through a collection


Let's say that you want to perform some operations on each element of the collection. In the following example I want to print out each element of my list. I will use a for loop to do so.




val yourLuckyNumbers = List(23, 0, 512, 3, 4)

println("Your lucky numbers: ")
for (number <- yourLuckyNumbers) {
println(number)
}

Output would be:



Your lucky numbers:


23


0


512


3


4



Another example would be to execute an operation on each argument of Scala script (in this case the printout is reversed):




for (arg <- args) {
println(arg.reverse)
}

If you called the sample script: reverse.bat as:




reverse.bat abc efg

the output would be:



cba


gfe



Ranges: to and until


You can use a for loop also if you want to iterate over some range of values. Let's say that you want to iterate from 1 to 12:




for (i <- 1 to 12) {
println(i)
}

This one would generate the following output:



1


2


...


10


11


12



You can exclude the last value from iteration by replacing to with until. Let's say that you want to iterate from 0 to 59 using until:




for (i <- 0 until 60) {
println(i)
}

The output is obvious:



0


1


2


...


59



Filtering


If you want to iterate over a subset of collection you can use filters. Let's use a range and filters to generate even numbers from a range of 0 to 10:




for (i <- 0 to 10 if i % 2 == 0) {
println(i)
}

it's a shorter form of:




for (i <- 0 to 10) {
if (i % 2 == 0) {
println(i)
}
}

You can apply multiple filters. Let me generate numbers from 0 to 10 which are even and divisible by 3:




for (i <- 0 to 10
if i % 2 == 0
if i % 3 == 0) {
println(i)
}

Nested iterations


You can put multiple <- clauses separated with semicolons. This will tell Scala that loops are nested. Let's see the example below:




val multiDim = List(List(11, 12, 13),
List(21, 22, 23),
List(31, 32, 33))

for (
row <- multiDim;
element <- row
) println(element)

This actually behaves the same as:




val multiDim = List(List(11, 12, 13),
List(21, 22, 23),
List(31, 32, 33))

for ( row <- multiDim )
for (element <- row)
println(element)

Filtering and nested loops


You can use filtering in nested loops just like in normal loops. Let's see the following example:




val multiDim = List(List(11, 12, 13, 14),
List(21, 22, 23),
List(31, 32))

for (
row <- multiDim
if row.length > 2;
element <- row
if element % 2 == 0
) println(element)

In this example we have nested a loop which iterates over multiDim list. The outer loop will return only these lists which have more than two elements:




if row.length > 2;

For the inner loop it will return only even elements:




if element % 2 == 0;

We could implement this loop in a normal way as follows:




for (row <- multiDim if row.length > 2)
for (element <- row if element % 2 == 0)
println(element)

or:




for (row <- multiDim)
if (row.length > 2)
for (element <- row)
if (element % 2 == 0)
println(element)

As you can see you can save a lot of time if you know what is the best way to implement your code.


Comments and questions


I encourage you to comment and write questions regarding this post or loops in general at michaelflowersky at geekingspree.com. Thanks for passing by!


References



  1. Programming in Scala: A Comprehensive Step-by-Step Guide, 2nd Edition by Martin Odersky, Lex Spoon and Bill Venners


My recommendations!


Looking for something compact about Scala? Check out Scala for the Impatient by Cay Horstmann.






via Geeking spree feed ... http://feedproxy.google.com/~r/GeekingSpreeFeed/~3/B_Dy-TfJags/scala-for-loop-13

How to create a simple Java project with Maven 3?

0 comments
Managing your project with Maven gives you a lot of advantages. You can automatically compile, pack into JAR or run tests on your project. You can also manage your project dependencies with almost no effort. To create JAR project I will use a powerful Maven capability called archetypes. Maven gives you a possibility to predefine your project template (archetype in Maven terminology), so that for different projects you can use different archetypes. Maven also comes with predefined archetypes for creating different projects like simple JAR or J2EE project.

How to set up proxy for Maven ?

0 comments
When I started using Apache Maven in my company I realized that, because of proxy settings, I couldn't download dependencies from the Internet. Dependencies are really powerful features of Maven, so it would be unreasonable not to use them. I found a solution to set up proxy so that Maven requests could go out my company's network.

I had to perform a few steps in order to set up proxy for Maven:
  1. I had to find Apache Maven settings.xml file.
  2. I had to modify my proxy settings.

How to install Maven on Mac OS X ?

2 comments
There are many ways to install Apache Maven on Mac OS X. You can use Homebrew, MacPorts or just download it and set up manually. For the sake of this tutorial I will use Apache Maven 3.0.5 and JDK version 1.7.0_25, I'm using Mountain Lion.

There are a few steps which you need to perform:
  1. Check your JDK installation. If you don't have JDK, you will need to install one (1.5 or above).
  2. Install Apache Maven 3.0.5.
  3. Test your installation.

Last but not least ...

0 comments









The early days ...

0 comments

Facebook

eBay

 NASDAQ


 Amazon

 Digg

Microsoft

 Google

 Apple

 Twitter

 Twitter (once again)




Yahoo!


Everyone has started somewhere. Just don't hesitate to start.


 

Tree things to make your OSX terminal typos smart for git, ls and asciiquarium

1 comments
Usually when I'm working on something I write my shell commands really fast, which is why I make mistakes while writing. The most common error is, of course, writing command letters in the wrong order e.g. sl instead of ls or gti instead of git. I'll show you some tools to make these mistakes funny.

gti instead of git

Let's install gti with brew:
Now if you type gti, you will get a GTI car driving through your terminal:


sl instead of ls

You can install sl with brew as well:
Here, a GTI car is replaced by a train:


Asciiquarium in terminal

The last thing I want to show you is how to fill your terminal with water and put fish, ducks, whales and sharks there. Install asciiquarium dependencies:

Finally you can install asciiquarium: Type asciiquarium to get the following result:

 
          
 

Geeking Spree has it's own logo !

0 comments
It's as simple as the whole design of geekingspree.com. Currently I'm using it only as a favicon.

The letter 'S' is a little off, which should make you feel anxious and intrigued just like reading my blog.

Why I chose blogger.com over wordpress.com as a blogging platform ?

1 comments
Some time ago I switched from Wordpress.com to Blogger.com. There are three main reasons why I did that. If you consider blogging on one of these platforms, you should read it. In this post, I will also count what price you would pay for both if you wished to customize some settings of your domain and blog.

Reason #1: Free domain attribution at blogger.com
Let's say that you don't want to have your blog on yourfancyname.blogspot.com or yourfancyname.wordpress.com but on yourfancyname.com. First, you need to register yourfancyname.com domain at one of the domain providers (it's about $10). So you would pay for it and you would just need to go to blogger settings to add a new blog address (because adding a new address on blogger.com is free), while at wordpress.com it would cost you 10$ for a domain plus 12$ for assigning a custom address to your blog. 10$ is less than 22$, so you can save 12$. As you can see, you have to pay 12$/year just because you don't want to have wordpress.com in your address.

Blogger.com 10$/year (+10$/year for domain registration)
Wordpress.com 22$/year (+10$/year for domain registration, +12$/year for a custom domain)

Reason #2: No forced Ads at blogger.com
Ok, let's say that I didn't convince you in Reason #1. 12$/year (for nothing) is not an argument for you. You register your blog at wordpress.com and write your first post, that's great! Congratulations! But wait a second, you view your post and you see that at the bottom of it is an ad (which is not relevant to your content at all). Since you don't like these ads you want to turn them off. You go to wordpress.com store and you see that you can turn them off ... for only 30$/year (again paying for nothing). At blogger.com there are no ads injected by the blogging platform.

Blogger.com 10$/year (+0$/year, because there are no ads to turn off)
Wordpress.com 52$/year (+30$/year, because you don't want to serve wordpress.com ads)

Reason #3: A way to monetize your traffic
So if you are reading the third reason, that means you are still not convinced. Let's say that after some time you've written a few really good posts, you have a nice content and while days roll on, you have more and more people who want to read your content. Maybe you would want to monetize this traffic (of course you are a hobby writer but let's assume that your traffic is so huge that you can't resist to try to monetize it). You go to wordpress.com admin panel to see what you need to do in order to start viewing ads on your site. You see that it will cost you $3,750/month (you need to upgrade to VIP account) at wordpress.com and 0$ at blogger.com (Google Ads). If you want to have ads (for free) which are intended to make money for you at wordpress.com, then you have to apply for the approval. One of the criteria of a positive consideration is big enough number of visitors. What is that number? Plus, they can always reject your request.

Blogger.com 10$/year (+0$/year, because of no cost of displaying Google Ads to earn money)
Wordpress.com 3802$/year (+3,750$/month, because you need to upgrade to VIP account) 

In my opinion free is not free and wordpress.com should not write that they are serving a free blogging platform, if you need to pay money for basic services.

JSF 2.0 Source Code Comment

1 comments
Commenting in JSF 2.0 looks just like commenting in HTML, so you need to write something like this: <!-- this is the comment -->. By default, Facelets xml parser will include your comment in a generated HTML file, so for a file like the one below:


Facelets xml parser would generate the following output:


If you don't want to include comments in a generated HTML file, you need to add the following context-param node to your web.xml file:


With javax.faces.FACELETS_SKIP_COMMENTS, the generated file looks like that:

How to start Jetty 9 on a custom port ?

0 comments
By default, Jetty 9 starts on port 8080. If you want to change it add jetty.port argument to Jetty start command:
java -jar start.jar jetty.port=80

In the above example Jetty starts on port 80.

How to set up Subversion client proxy ?

0 comments
To set up Subversion proxy go to %APPDATA%\Subversion directory on Windows or to ~/.subversion directory on Linux and edit the servers file. Put the following content to this file (under global section):
[global]
http-proxy-host = proxy.example.com
http-proxy-port = 8080

http-proxy-host is a proxy server (in this example it's proxy.example.com). http-proxy-port is a proxy port (in this example it's 8080). Additionally, you can also set up http-proxy-username and http-proxy-password in the same section, if you wish.

Implementing a TODO list application in Hibernate 4.0.1 and EclipseIndigo for JBoss 7.1 AS

2 comments
If you've read my previous post from the series From Zero to PrimeFaces Hero on JBoss 7.1 with Hibernate, MySQL, Eclipse Indigo and JBoss Tools, then you know that I have already explained how to configure your environment for development of JSF/JPA application on JBoss 7.1. I have also showed how to design a domain model for a TODO list application. This application will now be implemented by using Eclipse Indigo, Hibernate 4.0.1. This really simple example is intended to explain how to build a CRUD application in PrimeFaces 3.5, Hibernate 4.0.1, Eclipse Indigo and JBoss 7.1. We will also synchronize our JPA entities to create them in a database. I am going to use MySQL 5.5, so that MySQL JDBC driver is required. Since there is quite a lot to do, I've prepared a plan of tasks to perform, which is presented below:

  1. Configure your environment and create TODO_EAR (EAR project), TODO_EJB (EJB project) and TODO (Dynamic Web Project) in a way I've described in this post.

  2. Prepare your web.xml file and add JBoss 7.1 Adapter to your Eclipse Indigo in a way I've described in this post.

  3. Install MySQL Server 5.5.

  4. Create MySQL Server 5.5 database for our TODO application.

  5. Download and setup Connector/J (JDBC MySQL Driver)

  6. Add JPA 2.0 capabilities to your Dynamic Web Project, so that you are able to synchronize entities classes with tables in MySQL database server from Eclipse Indigo.

  7. Configure persistence settings (persistence.xml).

  8. Implement Hibernate entities.

  9. Synchronize Hibernate entities with database tables.

  10. Setup MySQL Driver as JBoss 7.1 AS module.

  11. Define Datasource.

  12. Deploy EAR on JBoss 7.1 AS. I've already explained how to deploy EAR unit in this context here.

How to completely remove MySQL 5.5 Server on Ubuntu Linux ?

1 comments
The fastest way to remove MySQL Server on Ubuntu Linux is to type the following command in your terminal:


The first command removes software packages, while the second data directory.

Designing a domain model for a TODO list application

0 comments
As I promised in my previous article this one will be about designing a domain model for a TODO list application. Then, We will implement this domain model in Hibernate, make Primefaces user interface, configure MySQL database and deploy this application on JBoss 7.1 AS. The first thing we need to do is to define requirements for our application. Then we will go through designing a database diagram and UML class diagram for application models. To create UML diagrams I am going to use ArgoUML open source modeling tool which runs on Java platform. In order to design a database diagram I am going to use data modeling functionality included in MySQL Workbench.


"Hello PrimeFaces World !" deployed on JBoss 7.1

2 comments
In my previous post I explained how to install and configure an environment to start working on PrimeFaces project. Today I would like to show you how to create a simple JSF page and deploy it to JBoss 7.1 AS from Eclipse Indigo. After reading this part you will understand basics of JSF PrimeFaces and you will have the knowledge of how to adjust your web.xml file to run JSF servlets. In the next part of this series we will go through designing a domain model, implementing Hibernate entities and building JSF interfaces of a TODO list web application. I decided to create a TODO list as an example of PrimeFaces, because it's easy to understand. You can focus rather on JSF, Hibernate and ManageBeans than on complicated business logic of the example.

Prepare your JSF PrimeFaces project for Eclipse Indigo and JBoss AS7.1.1

5 comments
In my opinion, developing PrimeFaces application in Eclipse is the most natural way to do it, if you plan to deploy your webapp on JBoss Application Server. The easiest way to start developing web applications for JBoss in Eclipse is by using JBoss Tools Plugin. I will describe how to install it. Here is a list of things which you need to do, in order to get your IDE work and start a new project:

  1. Choose and install Eclipse version of your choice. In my case it is Eclipse Indigo Sr2 for Java EE developers.

  2. Download and install JBoss Tools 3.3.

  3. Download and define JBoss server instance in Eclipse IDE for JBoss AS 7.1.1.

  4. Create new project Dynamic Web Project. Since I want to place my web project in EAR file, I will define EAR project first.

  5. Download and add primefaces-3.5.jar to your project lib directory.

    From Zero to PrimeFaces Hero on JBoss 7.1 with Hibernate, MySQL, Eclipse Indigo and JBoss Tools

    1 comments
    From Zero to PrimeFaces Hero is a series of articles, in which I indented to explain how to create a full CRUD webapp in PrimeFaces, Hibernate, MySQL, Eclipse Indigo and JBoss Tools. I will describe the way of building a simple CRUD application, which will be deployed on JBoss 7.1. The full path of builiding an application will be described as follows:

    • Configuring IDE. In this case, it will be Eclipse Indigo,

    • Designing and implementing data model in Hibernate,

    • Synchronizing MySQL scheme with Hibernate Entities,

    • Building ManageBeans for business logic and integrating it with data model layer,

    • Building JSF interfaces and integrating it with ManageBeans,

    • Artifacts deployment on JBoss 7.1 Application Server.

    After reading this series you will be able to build a full-featured CRUD JSF application backed by MySQL database with Hibernate mapping. Stay tuned for the next article on this topic.