Saturday, October 11, 2014

Scala Simple Build Tool

So, recently as I was working through my scala book, we got to the first chapter where we used 3rd party dependencies - the testing chapter. Now, for the simple examples in the chapter I just downloaded the jars needed and put them on my classpath in Eclipse and got everything working just fine. However, I was interested in looking for a dependency manager for scala and that's when I stumbled up the scala sbt project. With this finding, I decided to mimic the tests done in the testing chapter, but this time use scala sbt to manage my dependencies and put them on the classpath for me (I've pushed this small project to my github account).

Setting Up First Project In Eclipse


After I installed sbt, geting my first project up and running was not trivial so I wanted to walk through the steps I did to get a project from initial commit in github to fully working.
Note: I copied a lot of steps from a SO answer, however it was not exactly the same so I wanted to type up the steps I did.
  • Create your new repo on Github and clone it somewhere locally
  • Add the sbt eclipse plugin to your list of global sbt plugins
    • Now this step was a little confusing for me because the instructions were for *NIX style setups. They say to just add the line:
    •  addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.5.0")  
      To the file:
    •  ~/.sbt/0.13/plugins/plugins.sbt  
      
    • However, I'm working on Windows so obviously this path doesn't exist. Though, eventually I figured it out and needed to add it to the local user's file. So, in my case the file was located at:
    •  C:\Users\Dan\.sbt\0.13\plugins\plugins.sbt  
      
  • Once you add this plugin, open a command prompt to where you cloned your repo and type the following:
  •  sbt eclipse  
    
    • This will generate all of the eclipse project, classpath, and other various files needed to work properly in Eclipse
  • Import the project into Eclipse and start coding:
    • File --> Import --> General --> Existing Projects into Workspace
And that's it, now you can start working on your scala project inside of eclipse and get all of the helpfulness from the scala Eclipse plugin.

build.sbt


Once you have your project initialized and ready to go in Eclipse, it's time to make our build.sbt file. This file serves the same purpose as the file "pom.xml" for maven projects - it defines your jar name, version, dependencies, etc etc. Now, my build.sbt is very simple, however there a lot of different things you can do in this file and they are explained on the sbt site.

Adding Dependencies


Now, adding dependencies to your project is clearly defined on the sbt site, however there was one issue I ran into that I wanted to write down. By default, Eclipse will not reload the dependencies when you add them to your build.sbt file. Instead, you need to reload the Eclipse definition with the sbt plugin we added earlier. This is done by just typing a couple of commands into a command prompt and refreshing the Eclipse project - the steps to do this are listed on this SO answer.

Change SBT Download Location


By default, sbt will download jars to folder in the user's directory, in my case it was:
 C:\Users\Dan\.ivy2\cache  
However, I did not want my downloaded jars to reside there because that meant each user would have their own copies of all of the scala jars used. So, I found yet another SO answer that showed me how to change the download location. I simply had to edit my sbtconfig.txt file, which was located at - C:\Program Files (x86)\sbt\conf\sbtconfig.txt - and had to add the following 2 lines:
 -Dsbt.ivy.home=S:\SBTRepo  
 -Divy.home=S:\SBTRepo  
So now all jars will download to the folder - S:\SBTRepo - for all users and not to each user's individual local files.

Now, all of these steps might seem like a lot, but it really didn't take that long - there was definitely more investigate than implementation. It's just that this was my first time building a dependency-managed scala project so inevitably took the longest to get properly setup. And once all of these steps were done, my scala project was up and running perfectly in Eclipse and I was able to test everything properly on the command line.

Wednesday, August 27, 2014

My First Website

Today I took down my wedding website (code still available on GitHub) and wanted to reflect on the experience.

Now, creating a website for me was actually pretty difficult. Not only was this my first website I've created, but I also don't do user interface work/design. I am a back-end programmer that likes to work with databases, business logic, the occasional command line program, etc. - but never do I do any front-facing UI work. I just personally don't like working on them. However, my wife wanted me to make a website seeing that I'm a programmer and thought it would be a good challenge. So, I ended up making my own website completely from scratch: all of the coding, hosting the domain, running the server, everything.

Hosting

For hosting my website, I chose to go through Digital Ocean. I chose to host my site with them due to a few reasons:
  • It was cheap - I was only paying $10/month for 1GB ram, 30GB SSD, and 2TB network.
  • It was extremely easy to use - Their  console was super simple to understand and to manage my "droplet" with.
  • Their user guides are incredible - This is the main reason why I went with them. They have tons and tons of guides that are super easy to follow. Some of the better ones I used were:
    • Initial server setup - Which covered everything from the very first login to how to properly secure your system by removing the root login and changing the ssh port.
    • Setting up a LAMP server - Teaches you how to install Apache, MySQL, and PHP on a Linux box.
    • Fail2Ban - Shows how to properly configure fail2ban so your system is not continually tried to be compromised.

Coding

As I stated before, I wrote the entire website from scratch. To accomplish this, I used basic HTML5, CSS, and Javascript. Though, I did get help with some of the coding by using some popular libraries:
  • Twitter Bootstrap - Helped with overall design of the site. Also, does auto-adjusting of the page so viewing on different devices (laptop, phone, tablet) all look and feel the same.
  • Galleria - A simple to use picture gallery. This library was fantastic. The setup was super simple, the documentation great, and has a lot of specific configuration options to choose from. Also, it only took me about 2 hours total from initial download until I had working on my live site.
  • Google Forms - I used this for my RSVP. It's a form where I had all of the RSVP questions: are you coming, what do you want for dinner, etc. The awesome part of this tool is that it stores its answer directly to your Google Drive so you don't have to worry about losing any responses.

Final Thoughts

All in all this was a great project to work on because not only did I get the satisfaction of creating my first website, but all of my guests were able to see it as well. However, if I were to do this again, I would re-consider about where I host my site. Now I know that I said I loved Digital Ocean, and I did, however running an entire server for just a simple website seemed like overkill to me. I've heard about PAAS sites such as Heroku, Google App Engine, AWS Elastic Beanstalk, etc. but I didn't want to learn how all of that worked for such a simple goal - to launch a HTML site. Though, I feel if I did a more complex site with java, I would definitely look at one of these before running my own server again.

Wednesday, April 16, 2014

More Scala Projects

As I stated in my last post, my buddy Josh and are I currently working on small little scripts/projects while learning new languages. He is choosing to learn Ruby while I am learning Scala. Since that last post we have done a few more scripts/projects:
Count Vowels
This problem involved reading a file, counting all of the vowels, and then printing out a report. Currently, this program works completely as expected and I'm very happy with my solution.

Example execution using itself as the input file:
 S:\Workspaces\Github\scala-projects\Text>scala VowelCount.scala VowelCount.scala  
 Vowel: A    Count: 29    Percentage: 0.2437%  
 Vowel: E    Count: 32    Percentage: 0.2689%  
 Vowel: I    Count: 19    Percentage: 0.1597%  
 Vowel: O    Count: 31    Percentage: 0.2605%  
 Vowel: U    Count: 8    Percentage: 0.0672%  

Palindrome
As one might expect, this challenge was to determine if a file/string is a palindrome or not; palindrome meaning it's spelled the same was forwards as it is backwards (e.g. "racecar"). Now, to make this challenge a little more difficult, Josh and I added some "bonus point opportunities":
  • Ignore white space so "racecar" and "race car" both return true
  • Ignore case so "Race car" and "race car" both return true
  • If the whole string is not a palindrome, find the largest substring which is a palindrome
I conquered this challenge by breaking this down into a few steps and then putting them altogether:
  • Read the whole text into a string and then remove anything which is not a standard character
  • Find all substrings of the character string
  • Order the substrings by length
  • Loop through them until a palindrome is found, indicating this is the largest substring which is a palindrome
Example execution using itself as the input file:
 S:\Workspaces\Github\scala-projects\Text>scala Palindrome.scala Palindrome.scala  
 Largest substring of file which is a palindrome:  
 srevers  

Zip File Maker
This problem was not necessarily a typical problem but more of a learning challenge. As stated it was: given a file or directory, zip up that file or directory. Now, I say this is more of a learning challenge because there's really not much to solve, but a lot to learn. For my solution, if a directory is given as input, I recursively found all of the sub-directories and files and added them to the zip as well.

Example executing using a file and directory as input:
 S:\Workspaces\Github\scala-projects\Files>scala Zip.scala ..\Text\ Zip.scala  
 Adding file S:\Workspaces\Github\scala-projects\Text\Palindrome.scala to zip.  
 Adding file S:\Workspaces\Github\scala-projects\Text\README.md to zip.  
 Adding file S:\Workspaces\Github\scala-projects\Text\VowelCount.scala to zip.  
 Adding file S:\Workspaces\Github\scala-projects\Text\WordCount.scala to zip.  
 Adding file S:\Workspaces\Github\scala-projects\Files\Zip.scala to zip.  

The zip is created in the local directory called "output.zip" and uses the full path of each file when adding them and zipping them up.

Word Count
This problem was just an extension of the "Vowel Count" we did, except this time we counted all words in a file. Now, this problem was up for more individual interpretation than the others because each person could have their own rules on hyphenated words, contractions, punctuation, etc. Though, for my solution the approach I took was:
  • Read all of the lines from the input file
  • Remove all punctuation that had a space on either side of it; this allows hyphenated and contractions to remain
  • Remove all periods - this could be extended to include commas, question marks, and exclamation marks in the future
  • Count how many times each word was found
  • Write to a properties file
Now, I know the solution approach I made is not as elegant as it can be, but it's a start and maybe when I get better with Scala, I'll come back later and spruce it up a little.

Example execution using itself as the input file:
 S:\Workspaces\Github\scala-projects\Text>scala WordCount.scala WordCount.scala  

This will put the output of all the words to their total count in a file called "wordcount.properties":
 S:\Workspaces\Github\scala-projects\Text>cat wordcount.properties  
 #Wed Apr 16 17:56:23 CDT 2014  
 properties=1  
 punctuation=1  
 new=4  
 java.io.FileOutputStream=1  
 import=3  
 read.")=1  
 \\\\p{Punct}*=1  
 //=1  
 HashMap[String,=1  
 line=1  
 "))=1  
 properties.store(output,=1  
 throw=1  
 (line=1  
 line.split("=1  
 from=1  
 provide=1  
 +\==1  
 \==6  
 else=1  
 all=2  
 v.toString)=1  
 IllegalArgumentException("Must=1  
 file=1  
 scala.collection.mutable.HashMap=1  
 1=2  
 lines)=1  
 output=1  
 val=5  
 ((k,v)=1  
 1)=1  
 \!\==1  
 ").toLowerCase()=1  
 wordCounts=1  
 wordCounts)=1  
 line.replaceAll("=1  
 Properties()=1  
 (\!wordCounts.contains(word))=1  
 "=3  
 lines=1  
 if=2  
 }=2  
 |\\\\.",=1  
 "."=1  
 {=2  
 properties.setProperty(k,=1  
 trimmedLine=1  
 null)=1  
 by=1  
 (args.size=1  
 for=3  
 surrounded=1  
 (word=1  
 and=1  
 Removing=1  
 scala.io.Source.fromFile(args(0)).getLines=1  
 FileOutputStream("wordcount.properties")=1  
 <-=3  
 Int]()=1  
 java.util.Properties=1  
 wordCounts(word)=2  
 a=1  
 to=1  
 =20  

So ya, that's what Josh and I have been up to. If you want more up to date information on how our projects are going you can always look at our code on our github repos:

Saturday, April 5, 2014

Scala Projects

So a Josh Branchaud (a friend of mine) and I have currently started working on learning new languages through a series of small scale problems. We stumbled upon this github repo that has a lot of both small and large problem ideas that this guy wrote solutions to in python for his own learning. We have decided to work through the same problems he outlines but in the languages we want to learn: I have chosen to write the solutions in scala while Josh is working with ruby. Currently, I have completed 3 of the problems we have decided to do:
CalcPi
This problem is to write an algorithm that will compute the digits of pi to 'n' precision (digits). Currently, my solution supports up to 16 digits of precision for pi and I have yet to find the effort to support more. I might come back and re-visit this one in the future when I'm more familiar with floating point precision in scala.

Example execution with 10 digits of precision:
 S:\Workspaces\Github\scala-projects\Numbers\scala>scala CalcPi.scala 10  
 3.1415926536  

Fibonacci
With this problem, the user enters a number 'n' where 'n' can either mean to calculate the Fibonacci sequence up to the Nth term or calculate the Fibonacci sequence up to 'n'. My solution works fine except that with large inputs of 'n', it takes exponential time to compute due to the recursive nature of the Fibonacci sequence.

Example execution of the first 15 Fibonacci numbers:
 S:\Workspaces\Github\scala-projects\Numbers\scala>scala Fibonacci.scala 15 Nth  
 The first 15 Fibonacci numbers are:  
 0,1,1,2,3,5,8,13,21,34,55,89,144,233,377  

Example execution of the Fibonacci sequence up to 1000:
 S:\Workspaces\Github\scala-projects\Numbers\scala>scala Fibonacci.scala 1000 toN  
 The Fibonacci numbers up to 1000 are:  
 0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987  

PrimeFactors
In this problem, a user is allowed to enter a number 'n' and the program will output all of the prime factors of 'n'. The algorithm I used to computer whether or not a number was prime I found on Wikipedia.

Example execution with 'n' = 113679:
 S:\Workspaces\Github\scala-projects\Numbers\scala>scala PrimeFactors.scala 113679  
 The prime factors of 113679 are:  
 1,3,9,17,51,153,113679  

Hopefully Josh and I will continue working on these sets of problems for the foreseeable future because they are fun, simple, and definitely are a huge help in learning a new programming language.

Saturday, March 29, 2014

I'm Back

So, I've been quite hectic these past few months with wedding planning and work, though I just wanted to say that I'm back and am trying to get started again with working on some side projects. However, not all of my time has been completely wasted so here's a quick run-down of a few things I have done in the meantime:

  • Wedding planning. Yes, I am engaged and am getting married on Saturday, July 26th, 2014 in Omaha, NE. For my wedding, I decided to try and take a stab at making my own website. I am usually not a UI developer, but I stayed to simple things such as HTML5, CSS, and a little bit of javascript and think it turned out well - http://www.wiechertwedding.com/. I am hosting the site on Digital Ocean because they're cheap (my plan has a max of $10 a month) and their documentation is great. After I had the site complete and a domain name purchased, I had it fully up and running within about 30 minutes by following their simple Ubuntu LAMP setup tutorial.
  • Finished reading A Clash of Kings, the 2nd book in the series "A Song of Fire and Ice", or as most people know, the 2nd book in the Game of Thrones series. Yes, I know this isn't coding related, but it was an awesome book and much better than the 2nd season of the show so I thought I should mention that I finished it. Now, we just have to wait for Game of Thrones season 4 to start on April 6th.
  • Scala. Yes, I have slowly continued with my scala learning though I fully intend to keep going now that I have more free time. I don't know what I posted about scala last time, but I have just finished chapter 11 in my scala book. The past few chapters started talking about scala's type system while looking at some simple examples of inheritance and polymorphism. I believe in the next few chapters it will start putting together some more concrete examples of how these things work together and that should really help me in learn this new language.
As I stated before, I plan on updating this blog more often now that work has calmed down a little and my wedding planning comes to an end.

P.S. - I'd like to give a shout-out to my friend Josh Branchaud because he started a github repo called splitting-atoms which has really taken off. It's a community driven tutorial/guide on how to build/best practices while building extensions for the new github atom text editor.

Wednesday, October 23, 2013

Scala For-Expressions

So as you know from my last post I've been taking an online scala course through Coursera. I am currently in week 6 of 7 and it's been a lot of tough fun so far. It's definitely hard to wrap my mind around all of these functional concepts in 6 weeks time, but I'm learning more and more everyday.

This week we are focusing on Collections. Throughout this course we've been introduced and done a lot of things with Lists, but now we're doing more: Ranges, Sequences, Vectors, Maps, etc. Yesterday's example was especially intriguing to me in just the robustness and conciseness of the scala language.

The example was: Find all pairs of numbers (i, j) such that i + j is prime, where 1 < j < i < n, n is a natural number.

To start, we need a isPrime function. In java it might look like:
 private static boolean isPrime(int n) {  
      for (int i = 2; i < n; i++) {  
           if (n % i == 0) {  
                return false;  
           }  
      }  
      return true;  
 }  

But in scala, we were able to write it as:
 def isPrime(n: Int): Boolean =  
   (2 until n) forall (i => n % i != 0)   

Already, we can see the power of Collections in scala by being able to define a Range (2 until n) and then a forall loop over all integers in that Range.

Next, we need to loop over the range 1 < j < i < n and check to see if any i + j pairs are prime. In java, this might look like the following:
 int n = 7;  
 List<Pair> pairs = new ArrayList<Pair>();  
 for (int i = 1; i < n; i++) {  
      for (int j = 1; j < i; j++) {  
           if (isPrime(i + j)) {  
                pairs.add(new Pair(i, j));  
           }  
      }  
 }  
 System.out.println(pairs);  
Note: Pairs is a simple class that I made for this example

And in scala, we did this 2 separate ways. The first is using maps and filtering:
 val pairs = (1 until n) flatMap (i =>  
   (1 until i) map (j => (i, j))) filter (pair =>  
   isPrime(pair._1 + pair._2))  
 println(pairs)  

And the second way we used for-expressions:
 val pairs = for {  
   i <- 1 until n  
   j <- 1 until i  
   if isPrime(i + j)  
  } yield (i, j)  
 println(pairs)  

We can clearly see that the for-expression is the most concise and arguably the most readable out of the three implementations.

I just thought this for-expression example we did was pretty neat and worth writing about. I'm definitely looking forward to the rest of this week and next week to see what other cool features scala has to offer with its Collection manipulation.


Thursday, September 12, 2013

Functional Programming Principles in Scala

As of late I've been doing less and less work in scala and feel bad because I've been enjoying the language quite a bit. Luckily for me, there is a scala Coursera class starting September 16th, 2013 (next Monday). I have enrolled in this course and look forward to learning from the creator of scala himself, Martin Odersky. Hopefully this class will re-ignite my desire to keep learning and exploring this language and gets me back on track to finish my scala book.