## 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)) {
}
}
}
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.

## Tuesday, August 20, 2013

### Chapter 6 - Functional Objects

I've made it to Chapter 6 - Function Objects, and by the title of this chapter it should be a very interesting one. Seeing as I am a java guy, almost all functional ideologies are new to me and learning is good. Now, I don't expect this chapter to go into super deep detail about functional programming, but hopefully it gives me enough of a taste so that I am eager to learn more.

Now time for some more scala.

## Monday, August 12, 2013

### Done With Chapter 3

As you know, I've been working through Programming in Scala and I've recently finished Chapter 3: Next Steps in Scala. The first 3 chapters really expose the reader to the over-arching idea of the Scala language, what's the intention of it, how one might use it, and small scripts. I'm just now starting Chapter 4: Classes and Objects which should give me a more in-depth look to how to really setup an application in Scala. I'm still enjoying this new language a lot and I am posting all of the examples I come across to my github.

## Sunday, July 14, 2013

### Chapter 1 - A Scalable Language

Since my last post, I've been working through chapter 1 (A Scalable Language) in Programming in Scala. This chapter was an introduction to the language itself, showing the reader the advantages to using scala versus other OOP and functional languages. There were very few code examples in this chapter, however the ones that I did come across I've copied to my github project. I will continue to add code examples I come across in the following chapters to that project and make them available to anyone who wants to see scala programming.

On to chapter 2: First Steps in Scala.

## Friday, July 12, 2013

### Scala Time

Seeing as I'm still really interesting in learning scala, I decided to go ahead and order two scala books this week:

The first book is a beginner's guide to the language and more of a textbook whereas the second is more of advanced guidelines to follow to be a good scala programmer. I definitely look forward to starting the first book next week and learning this hybrid language in more depth.

## Tuesday, June 25, 2013

### Lambda Lounge KC

So today I attended my first ever developers meetup and it happened to be the inaugural Lambda Lounge KC (LLKC). The goal of this meetup group is to talk about and inform people of the power of functional programming. The reason why this meetup sparked my interest is because I've recently tried to learn scala. However, while trying to learn scala, I felt myself using much more OOP than functional programming because scala provides both options to the programmer. So I thought attending this meetup would be a good way to hear from others on the proper way to use functional languages.

During tonight's meetup, we were lucky enough to have 2 presenters:

• Jim Duey - Who gave a brief introduction to functional programming through the use of Clojure
I thought both of these presentations were very interesting and helpful to understand why functional programming can be better in situations than OOP.

## Friday, June 21, 2013

### Babou

Starting work on a GUI framework for ocelot this weekend with my good buddy Josh Branchaud. Hopefully we have something up by the end of this weekend to show off.

## Tuesday, May 7, 2013

### Perforce

As I stated in my last post I am currently starting to work on a 3d game with the Unreal Development Kit. Obviously when working with such a system version control is a must. They recommend to use perforce, which I thought was a pain to setup, so I thought I'd write down how I did it in case others need some help.

First of all, the reason why they suggest to use perforce instead of svn, git, etc is because UDK files are binary. That means there is no such thing as merging, rebasing, branching, etc. With perforce, when you check out a file, you lock it. This means no one else can work on it while you have it checked out which obviously prevents any possibility of having to merge, which in the case of a binary file doesn't exist.

For my repository, I found assembla which offers free, private perforce repositories. This was perfect for me so I created one according to the directions and it was up in a matter of minutes.

Then, I installed both the perforce command line and visual clients from the perforce clients page. You only really need the command line client but I'm a visual person so I prefer to use the visual client when I check-out and check-in my files.

Once you have the command line client installed, you need to make a config file. The instructions on this page were adequate enough to get me going. This config files makes perforce recognize what files you would like to be candidates for version control. Though, where you put the p4config.txt and your workspace names are very important and that's what was messing me up. For example, my install of UDK is C:\UDK\UDK-2013-02\... What this means is that if I want all of UDK to be available to perforce, I needed to put my p4config.txt in C:\ and my workspace name is UDK. It then put two and two together to say, aha, you want to be able to use files from C:\UDK for perforce huh? Gotcha.

Once that was finally setup correctly, the next few things I did were trivial. I had my simple level that I wanted to added so I navigated to it on my Workspace tab in p4v. Then right-click, Mark For Add. Then when I wanted to put it online, in the Files tab I right-click, Submit with my commit message.

Now that this file is in perforce, before you start to edit it you need to check it out. So again I found the file, right-click, Checkout. Do my edits, and then re-submit it.

Again, this actually isn't too bad it's just the fact of where to put the p4config.txt file and the name of the workspace you input are very sensitive.

I hope this helps anyone else that has troubles setting up perforce.

### Shift of Focus

So I haven't had a post in a while and there's some reason to that. These past few weeks instead of messing around with side programming projects I have been dabbling in trying to create my own 3d video game. A video game you say? Yes. The reason is because last summer I worked on a simple 2d shooter that had good intentions but was not followed through properly. Though this time I am determined to make this playable in a 3d world. I would love to post more about the game other than it's a 3d fps, but that's all I can say for now. Anyways, the list of technologies I'm now using has taken quite a long time to figure out and use properly which is the real reason for such a long break between posts.

Originally I was planning on sticking with java for this 3d game and using jMonkeyEngine, but that just didn't seem right. I've been following the development of that open source project on and off for a couple of years and even though it has made great strides, I still feel that some of the other tools out there will just be easier for me to pick up and learn right away. The major problem I didn't like versus some of the other candidates was just the fact everything was still programmed. An example of getting a simple textured box to appear in the screen can be done in the following form:

`````` Box b = new Box(Vector3f.ZERO, 1, 1, 1); // create cube shape at the origin
Geometry geom = new Geometry("Box", b); // create cube geometry from the shape
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); // create a simple material
mat.setColor("Color", ColorRGBA.Blue);  // set color of material to blue
geom.setMaterial(mat);          // set the cube's material
rootNode.attachChild(geom);       // make the cube appear in the scene
``````
I really wanted to just draw a box on the screen and apply a texture which is why I chose not to go with jMonkeyEngine.

So, I started to look commercially for products and found the following 3 that I did some investigation on:

After some evaluation, I have decided to work with UDK. The main reason was just the options of the free vs paid versions. With UDK, you get all the features to design a full game, minus the source code. Then, if you want to commercialize your game, you have two options:
• Pay \$99 and you get 100% of first \$50k and then 75% thereafter
• Buy full commercial license to get source code and 100% of revenue (price not listed)
The reason why I felt this was a better option was due to the fact that with both CryENGINE and Unity 3d seemed to remove some features on the free versions. This meant that even if I never sold the game and wanted to use all of the features, I would have to buy a license.

Though enough about my investigations, on to UDK. This software is pretty damn impressive. Right now I have been following the 3D Buzz tutorials and with each video I finish my impression of the software just gets better and better. Some of the more notable features I've stumbled upon are:
• Light Lag Information - It can show you where you have too many lights on a surface and potential graphics lag.
• Apply Materials - Applying materials to various surfaces is as easy as drag-and-drop. And on top of that, if something is off, you can just tell it to auto-correct the layout of the materials and they re-adjust to properly align with one another.
• Kismet - I've only just begun with the Kismet scripting language but it's really cool. I used it to slide a gate back and forth based on a player's proximity to it. I just told Kismet where the gate will start/stop, how long the animation should go, and if I want any sounds. All of this was drag-and-drop as well. No manual programming to get a moving gate, how cool is that?
So, that's what I've been up to lately. I have completed my first simple level and would like to post a video walk-through of it. Actually, I can setup UDK to record a video walk-through but I haven't gotten that far in the tutorials yet so hopefully I figure it out soon and post a video.

## Wednesday, March 27, 2013

### SCGet Status Update

Recently both Josh and I have been busy with soundcloud-get (scget) and it has been coming along quite nicely. We feel confident that it is currently at a stable place and are looking into how to release a python project and tag it as version 1.0.

The current command line options that scget supports are:

• -u <song_url>- Downloads a song from the specified song url. This parameter can be provided multiple times.
• -a <artist_url> - Downloads all songs from the specified artist url.
• -o <folder> - Folder to download all songs to. If not provided or doesn't exist, all songs are downloaded to the current directory.
• -st <track_name> - Searches for tracks that are similar to the provided name and prints their titles.
• -sa <artist_name> - Searches for artists that are similar to the provided name and prints their names.
• -l <number> - Limits the number of results pulled back from either the search track or search artist queries. If not provided, each search defaults to 20 results.
Once we figure out how to release this project we do have a few ideas of what still needs to come for this project:
• Ways for the user to choose songs/artists to download from the provided search results.
• Being able to provide a yaml configuration instead of having so many command line arguments.
• Track down Unicoder Coercion errors that happen on occasion.
Though, for now it is all working fine and as expected for almost all cases which is why we want to release it now and then tackle these other issues.

Remember you can always watch the progress of this project on our github page.

## Wednesday, March 6, 2013

### SCGet

I currently started to work on a new side project, scget. It's a command line tool that will allow users to easily download songs from SoundCloud. SoundCloud does allow downloads from the browser, but that requires one to navigate to the page, click download on each song they want, and specify an output folder. This command line tool is going to be simple, yet powerful alternative to downloading from the browser.

Some of the options that have already been thought of are:
• `````` scget <trackUrl>
``````
• `````` scget <trackUrl1> <trackUrl2> ... <trackUrlN>
``````
• `` scget -dj <userUrl>  ``
• `````` scget <trackUrl> -o <folder>
``````
Obviously this would be easier than having to navigate the webpage because you could just queue up a bunch of urls to download and then hit go. It will do all of the processing behind the scenes while you get back to what you were doing before.

For this project, I am working in collaboration with Josh Branchaud on this project because not only are we great friends, but we both love SoundCloud. You can follow our progress on this command line tool on our github page.

P.S. - If you have any suggestions about other functionality that should be included in this tool feel free to leave it in a comment.

## Friday, March 1, 2013

Currently, I use apache maven to handle all of my dependency management, though lately I have heard much interest in a new project: gradle. After reading some posts about it, it seems that not only does it handle dependencies like maven does, but also helps the build cycle be much better with the use of groovy scripts. This should be interesting because maven only allows certain life cycles to be ran and if you want to deviate from that, you need to write your own plugins.

I don't know what to expect while messing around with gradle, but I'm currently following the tutorials and have made it to chapter 7 thus far. To follow my progress, you can watch my github gradle project.

## Saturday, February 16, 2013

### Project Euler - Problem 005

So I just finished problem 5 and it was actually quite easy. I was able to just put a while loop around a simple function call to see if all numbers 1 through 20 evenly divided into a number:

`````` def isEvenlyDivisible(n: Int) = (1 until 20) forall (n % _ == 0)
``````

Though, my program did take 12 seconds to run so I still think there are some optimizations that could be done to get it to run a little faster.

Actually, I just made a few enhancements and got it to finish in 7 seconds. I'd like to see if this can be done faster but for now, it's time to take on problem 6.

## Saturday, February 9, 2013

### Project Euler - Problem 002

I just finished up the second project euler problem and it definitely took me a littler longer than expected to figure out. If you should happen to try this one, make sure to read the directions carefully because I feel I would've had the answer several minutes earlier than I did had I paid a little more attention.

On to problem 3.

### Gson and Date Formatting

Gson is a really cool tool to quickly convert regular POJOs to json strings and back. It's super simple to setup and even easier to use:

`````` final Gson gson = new GsonBuilder().create();
final String jsonString = gson.toJson(myPojo);
final Pojo myPojo = gson.fromJson(jsonString, Class);
``````

However, by default it appears that Gson uses a fairly simple date formatter that seems to be locale specific. In most cases, this should be alright unless you need to parse a more fine grained date, such as something like "2013/02/09 15:07:50 +0000". When using gson to parse a date like it does fail, and you get exceptions:

`````` Caused by: java.text.ParseException: Unparseable date: "2013/02/09 15:07:50 +0000"
``````

Obviously this is not wanted and knowing that this is a valid date makes it even stranger that gson couldn't handle it. The answer? You can override the date parsing adapter that gson uses by default with your own version. For my example, I had to override it with the following:

``````final GsonBuilder builder = new GsonBuilder();
// Year in 4, month in 2, day in 2, hour in 24, minutes in hour, seconds in minute, timezone in 4
final DateFormat df = new SimpleDateFormat("yyyy/MM/dd kk:mm:ss Z");
@Override
public Date deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException {
try {
return df.parse(json.getAsString());
} catch (final java.text.ParseException e) {
e.printStackTrace();
return null;
}
}
});
return builder.create();
``````

So, this is a really simple yet powerful way to use gson even when you have difficult dates that need to parsed.

## Friday, February 8, 2013

### Project Euler - Problem 001

I haven't been working with scala too much lately because I really haven't found any projects that I feel I can accomplish yet with a new language. Though, I heard about project euler the other day and noticed that each problem is a simple math calculation to solve. I thought this would be the perfect opportunity to start learning a new language and take advantage of some of scala's functional programming aspects.

So I took on problem1 during lunch today and found it to be not so terrible:

`````` package scala
import java.util.concurrent.TimeUnit
/**
* Problem 001 in scala.
*/
object Problem001 extends App {
val timeUnit = TimeUnit.MILLISECONDS
val startTime = System.currentTimeMillis()
// Create a list of 1000 numbers
val buffer = for (i <- List.range(0, 1000) if (i % 3 == 0) || (i % 5 == 0)) yield i
val sum = buffer.toList.foldLeft(0)(_ + _)
println(sum)
val endTime = System.currentTimeMillis()
println("Time elapsed in seconds: " + timeUnit.toSeconds(endTime - startTime))
}
``````

I definitely see this as a way to keep learning a new language and to keep my mind sharp with these kinds of problems. Hopefully problem 2 goes as well as the first one did.

### A New Language

So lately I’ve been wanting to learn a new language and get out of my java home. I’ve been working with java all throughout college and currently use it at work. I heard of the upcoming language scala and found out it is compatible with java which was a total plus for me (seeing that I’m so used to java technologies and libraries). I do have a mathematical background so some of the more advanced collection operations seem a bit more logical to me and really seem help spruce up the code. I haven’t played around with the language too much as of now, but I definitely think this language looks incredible and can’t wait to see what all I can do with it.
I’m really looking forward to this new adventure of teaching myself a new language. Come on scala.