Showing posts with label scala. Show all posts
Showing posts with label scala. Show all posts

Monday, June 27, 2016

Play Framework 2.5 sample CRUD application

I have had an interest in the Play framework for a number of year now and I have built a few small applications using the framework. The Play framework has evolved over time and in order to keep up to date with the most recent changes it's useful to try and build your own application. I have built a very simple CRUD application for this purpose.

If you are interested in application you can clone / downloaded it from here.

Introduction

This application is used to showcase Play framework while learning basic Spanish phrases. This application makes use of the following:

Installing

Running

  • Open a command terminal and change into the sample application root directory
  • Run activator:
    • activator run

Screenshots






Thursday, November 21, 2013

Learning Scala with Scala Koans

A few days back I blogged about learning Ruby with the Neo Ruby Koans, I found a similar page for the Scala programming language called Scala Koans.

Koans are small lessons on the path to enlightenment. The aim of the Scala Koans project is to provide an easy learning environment in Scala. Your insight will be derived by encountering failing tests and fixing them so that they pass. A testing framework is used to simplify this process and to get you off to a good start with using Scala.


Play 2 + Scala + Slick + more sample application

In my attempts to learn the Play Framework I decided to build my own sandbox project called Resource Vault. If you are curious about what I did you can download the project and run it in your local environment.

Resource Vault core design choices 


  • Play for Scala - Scala API for Play application developers
  • Slick - Modern database query and access library for Scala
  • Bootstrap - Mobile first front-end framework for faster and easier web development. Includes 200 glyphs in font format from the Glyphicon Halflings set
  • Webjars - WebJars are client-side web libraries (e.g. jQuery & Bootstrap) packaged into JAR files
  • Specs2 - Library for writing Acceptance and Unit tests


Resource Vault application overview

Resource Vault is a virtual vault for referencing resources, a central location to bookmark important links and resources. A user can add any number of resources and group them accordingly using basic CRUD actions. Below is a screen shot of what the home page looks like:


Running the application

In order to get Resource Vault running in your local environment you can try the following steps:



I won’t go into any other detail describing every file so if you want to dig deeper please have a look at the code.

I am going to continue building adding to this project so that I can grow and improve my knowledge of Play and Scala and all the rest that comes with it. I will look at adding more useful unit specifications and acceptance specifications with Specs2 as well as FluentLenium. I want to add authentication and authorisation and then deploy and run the application on Heroku. I have a number of other use cases in mind and hopefully I can get time to cover most of them.

Please feel free to comment and give suggestions on areas where you think I could have done things in a different way.


Wednesday, November 20, 2013

Atomic Scala

I recently purchased the AtomicScala eBook written by Bruce Eckel and Dianne Marsh. I am enjoying reading this book as I find it to be a much easier read than some other programming books on Scala that I have read.

We wrote Atomic Scala to appeal to beginning programmers as well as experienced programmers who have been frustrated with the sharp learning curve often described when learning Scala. In the atom “How to Use This Book”, we encourage experienced programmers to move through the material more quickly by skipping to the summaries (Summary 1 and Summary 2). Those summaries also help beginners by reinforcing the information that we previously presented in detailed atoms and with additional exercises.

I would recommend this book to anyone wanting to learn Scala, you can also download the first 100 pages for free to get a taste for it.

Wednesday, June 16, 2010

Group pattern matching with regular expressions in Java and Scala

Even though this has been around for some time I have only recently used it and think it is quite nice and worth blogging about.

The use case is pretty straight forward, you have a string of data and you want to extract values out of the string based on a pattern. An example would be a date “16-Jun-2010” and you want to extract the day, month and year. Another example could be an email address where you want to extract the username and domain. I will show you an example of extracting the day, month and year values from a string using regular expressions. Regular expressions allows us to match the format of the string as well as to group matches within the string so that we can get our day, month and year values.

Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GroupCaptureEx {
  public static void main(String[] args) {
   String input = "16-Jun-2010";
   String patternStr = "(\\d{2})-([a-zA-Z]{3})-(\\d{4})";

   Pattern pattern = Pattern.compile(patternStr);
   Matcher matcher = pattern.matcher(input);
   if (matcher.find() && matcher.groupCount() == 3) {
      System.out.println("Day is: " + matcher.group(1));
      System.out.println("Month is: " + matcher.group(2));
      System.out.println("Year is: " + matcher.group(3));
    } else {
      System.out.println("No match found or unexpected match found");
    }
  }
}


The Scala version uses a Scala Regex class to simplify matters a little. It compiles the pattern by default so you don’t have to explicitly do that. It is also an Extractor which is used to extract the data you are looking for from the string based on the group matching and then to bind those values to the returned elements. The only thing we need to concern ourselves with is Scala’s pattern matching ability. The pattern we are interested in matching on would look like:

DateRegex(day, month, year)

We then use Scala’s match expression (similar to switch in Java) on the input string. If the pattern DateRegex(day, month, year) matches the string than we have a match

Scala
object RegExGroupCapture {
  def main(args : Array[String]) : Unit = {
    val Input = "16-Jun-2010"
    val DateRegex = """(\d{2})-([a-zA-Z]{3})-(\d{4})""".r
  
    Input match {
      case DateRegex(day, month, year) => {
        println("match found")
        println("Day: " + day)
        println("Month: " + month)
        println("Year: " + year)
      } case _ => println("No match found")
    }
  }
}

Thursday, June 3, 2010

Programming in Scala is fun

In learning more about what Scala can do I decided to work on the problems listed in the Project Euler website.

"Project Euler is a series of challenging mathematical/computer programming problems that will require more than just mathematical insights to solve. Although mathematics will help you arrive at elegant and efficient methods, the use of a computer and programming skills will be required to solve most problems."

I have only completed the first ten and have thoroughly enjoyed it. Not only have a learnt some neat tricks one can do with Scala I have also learnt about a number of different mathematical algorithms. At some point I would like to go back over some of the problems and see how one would do them in Java just to compare the difference between Java and Scala. I can only imagine it would be a lot less enjoyable and perhaps even a little painful.

Saturday, November 7, 2009

Scala – Transition from Java to Scala - My initial experience and thoughts

A couple of months ago I was given a task at work that required code to be written in Scala. At first I wasn't so thrilled about the fact that I would be learning a new language but I must say after spending some time with it I am glad I did. The hardest part in learning Scala was the "getting started" part. The syntax of the Scala code is very different from Java and it required me to "concentrate" and to think a little before I could start my very first "Hello World" application…as you can see I wasn't very motivated. The sample applications were also a little difficult to follow because I wasn’t familiar with the syntax it required a lot more thinking and a lot more concentration…sigh…yeah like I said in the beginning I wasn't very motivated. However having spent a couple of months programming in Scala I am a lot more familiar with the syntax and believe it or not  the code has become a lot more readable, a lot more flexible and more desirable to write than Java code. I am amazed at how much you can do with so little effort.

It doesn't take too much effort for somebody who is a Java programmer to learn Scala. Like I said in the beginning the hardest part for me was getting started but after that I became excited about what I could do with this language and I became motivated to learn more. Scala compiles to Java byte code and runs on the Java VM. You can reference existing Java libraries in your code and make full use of their classes. Scala pretty much does everything Java does but on top of that it offers a whole lot more. I am also enjoying learning other programming concepts and techniques. Scala is an object-oriented and functional programming language. I am learning a lot about functional programming and I have started thinking differently about how I can use these techniques and concepts to produce better code.

So if you are interested in learning Scala and come from a Java programming background like me than I would recommend the following links:

There are also a number of open source projects written in Scala and I would recommend anyone getting started to download the projects and take a look at the source code. Below are a few I have come across and learnt from:
  • Lift - Scala web framework
  • TalkingPuffin - Twitter client
  • Scalaffinity - library containing core functionality used by any social networking web site

Tuesday, April 28, 2009

Scalaffinity - Scala, Spring, Hibernate, Maven project

I was looking around to see how to build a JUnit test case using Scala with Spring. I came across this project (currently still in development) called Scalaffinity. You can connect to the SVN repository and download the project. That’s what I did and it has everything I was looking for and more. Thanks to Álvaro Martínez Hernández who put this project together.

Friday, April 24, 2009

Scala IDE -- What Scala IDE?

So I am learning Scala...why one might ask? I was one of the ones who asked and the answer was quite straight forward and direct: "Do it or you're fired you lazy bum!" I knew nothing about the Scala language up until a few weeks ago and I wasn't over the moon to find out I would have to learn yet another programming language but those words were ringing in the back of my head and it was just  enough to motivate me and get me started on my journey in the Scala language.  

Having said that and after doing a little reading up on Scala and playing around with it I have changed my tune...from death metal rock to dance. I am feeling a little excited (even at my age) about this language and can already start to see a number of the benefits this little yet powerful language has to offer.

However...having said ALL that the development tools I have tried have been anything but pleasurable. I was hoping for chocolate nut sundae but got a whopping bowl of fruit salad with cream. 

I use Eclipse (Ganymede)  as Eclipse is my friend. Yes we have good times as well as bad. But we understand each other. I was using the 2.7.3 Scala Eclipse plugin and it was giving me a little hassle. Every now and again the same file would look as though it would duplicate itself and now today while in the middle of editing a Scala class my file became read only for some reason. I noticed a newer version of Scala is available (2.7.4 final) and I found a newer Eclipse plugin...so I uninstalled the Scala plugin and restarted Eclipse. To my surprise Eclipse doesn't start up...well it tries to start and then it dies and then it tries to start and then it dies. It got stuck in a loop and the only way to kill it was to reboot my pc. Sigh. 

I still have this problem and I will spend more time on it tonight but in the meantime I played around with NetBeans 6.5...hahaha my allegiance, what allegiance you say? I was able to install the Scala plugin for NetBeans and created a project without much hassle. Besides that I haven't done much but will report on how things go. My first impressions are a lot better than with the Eclipse Scala plugin. 



Update
I spent a couple of hours today working on this issue. I tried many things and seemed to be making progress until I completely corrupted my Eclipse installation. 

I did happen to find some useful information on the scala-lang web site. They seem to have addressed a number of problems in the previous version. You need at least version 3.4.1 of Eclipse in order for this plugin to work though.  I updated my Eclipse to 3.4.2 and I had to also follow these instructions to get my Scala classes compiling. 

I had a little time to play around with NetBeans and was pleasantly surprised with it however since most of the frustrations with the previous version of the Scala Eclipse plugin have been resolved I am going to stick with Eclipse. 

So to end this discussion there are tools out there to develop Scala applications. They may still carry a number of frustrations with them but for now I am happier than I was last week.