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

Saturday, September 7, 2013

Developing Trackpad: An 18 second Vine documentary

My goal was simple. I wanted to be able to control my Mac's mouse from my Nexus 7. These following Vines document an evening's attempt at that.

Phase 1 - Basic Android application. Just getting set up.

Phase 2 - Server setup. My mouse now speaks http.

Phase 3 - Hook server up to application. World's hackiest wireless mouse.

Check out the android app here
Accompanying scala Play service here.

Saturday, July 13, 2013

Scheduling background tasks in Play made easy: A mailer bot example

It took a little while for me to figure this out earlier, so I thought I'd share how to easily schedule background tasks in your Play application. Let me illustrate with an example that will send an email to a user every 5 seconds. If you follow all the steps below, you should have your very own spam bot up in no time.

1- Create a new play application in scala
> play new background-email
2- Let's set the Global object location for our project. In your background-email/conf/application.conf file, make sure you have the following set:
application.global=Global
This will tell Play where to look for the Global object class of our application.

3- Under background-email/app/ create Global.scala. This file is loaded at application start time and the onStart method is where we can add calls to schedule tasks. In the example below, we'll have onStart call startAwesomeBackgroundTask which will schedule periodic email sends. Note: If you want this example to work, create a gmail account and replace "user@gmail.com" with your username and "password" with a the real password.


4- Before we can run our code, we'll need to add the right dependencies to our project. Update background-email/project/Build.scala to include a reference to org.codemonkey.simplejavamail:

5- You should now be able to run the application and see it in action:
> play start


Voila! From the console output you should be seeing an email sent every 5 seconds. The proof is in the pudding:



*The above instructions work as of Play 2.1.2.

Monday, May 27, 2013

Visualizing Twitter geo data using Play 2.1 & heatmap.js

My latest weekend project involves visualizing real time Twitter geo data using heatmap.js:


I'll be hosting the app here for the next couple of days: ignite.phyous.com

Check out the code on github if you'e curious: https://github.com/phyous/ignite

Saturday, March 30, 2013

Developing for the Leap Motion controller in Scala


I just got a hold of a Leap Motion controller today and I have to admit it's a neat little device with a lot of potential. The first thing I did was figure out how to interface with the device using Scala. Here are some quick pointers on how to get setup.

Before reading further, entire process I outline below is summed up in the following github repo. You can clone it and run ./install then ./run to produce the following results:


Instructions

1- Download the SDK here.

2- Assuming you have maven installed, create a project using the following command
mvn org.apache.maven.plugins:maven-archetype-plugin:2.2:generate \
-DarchetypeGroupId=org.scala-tools.archetypes \
-DarchetypeArtifactId=scala-archetype-simple \
-DarchetypeVersion=1.3 -DgroupId=leap-scala \
-DartifactId=leap-scala -Dversion=1.0.0 -DinteractiveMode=false

3- Install the leap motion sdk jar in your local maven repository (found in the sdk package from step #1 under /LeapSDK/lib/LeapJava.jar). Use the following command:
mvn org.apache.maven.plugins:maven-install-plugin:2.3.1:install-file \
    -Dfile=PATH_TO_JAR \
    -DgroupId=com.leapmotion.leap -DartifactId=leapMotion \
    -Dversion=1.0.0 -Dpackaging=jar

4- Add a reference to this jar file in the leap-scala/pom.xml that was created in step #2.
    
    <dependencies>
        <dependency>
            <groupid>org.scala-lang</groupid>
            <artifactid>scala-library</artifactid>
            <version>${scala.version}</version>
        </dependency>
        <dependency>
            <groupid>com.leapmotion.leap</groupid>
            <artifactid>leapMotion</artifactid>
            <version>1.0.0</version>
        </dependency>
    </dependencies>

5- Write some code for capturing events from the device. Here are some snippets from the leap provided samples I ported to scala:


6- Once you have some code, you can bundle it along with the leap motion library in a "fat" jar using the maven-assembly-plugin. Check out a sample pom.xml that does this here. You can then bundle the app using the following maven command
 
mvn install

This will produce a jar file called "leap-scala-1.0.0-jar-with-dependencies.jar" under the target folder.

7- Once your code is compiling, it's time to run. You'll need to point the jvm runtime to the native leap motion libraries for interfacing with your OS. On OSX, this is a file called libLeapJava.dylib found in the same directory of the SDK as the jar file. Here is how you would run your app once it's compiled:
 
java -Djava.library.path=".:./libs" -jar target/leap-scala-1.0.0-jar-with-dependencies.jar

Using the code above, you should be able to see input directly form the device on your console.

These instructions were generated while developing on OSX. Things might need a little bit of tweaking if running on other platforms.

Wednesday, December 19, 2012

Bootstrap your Scala skills


I recently decided to take a swing at learning Scala. The following summarizes why I found the language interesting enough to look at and some excellent learning resources.

Why scala?
  • Type inference. When the type for an element is obvious, the Scala compiler is able to automatically make the inference. No need for any additional type annotations. Less typing is great (pun intended).
  • Strong Static typing. As fun as dynamic typing can be, I'm sick of running into type mismatch related bugs in rails. Coupled with inference, you can catch the bugs early with the compiler and write less code.
  • Flexible approach to immutability. Mutable & immutable variants of all collections conveniently provided. Bootstrap your way to functional programming.
  • Higher order functions. Allows the definition of functions that take other functions as input. Makes for very concise and expressive code.
  • Multiple inheritance. Sort of. With traits.
  • Pattern matching. Case statements on steroids. See here for some neat example applications.
  • A JVM language. Can interoperate with Java libraries easily.
  • Increasing enterprise adoption. Twitter, LinkedIn and Foursquare(to name a few) make active use of Scala. It's been claimed to be an active ingredient in the secret sauce that helps them scale.
Learning Resources

Scalatron



I find the premise here brilliant. Scalatron teaches you Scala while you program the AI to compete in a virtual arena game. Play against the computer or other people's bots as you level up your Scala skills.

Coursera



If you prefer the classroom based approach to learning and you have the time to dedicate, coursera is always a great option. Learn about programming the functional way in Scala.

Scala School



Scala school started as a series of lectures at Twitter to prepare experienced engineers to be productive Scala programmers. Geared towards seasoned programmers, it does a great job of exposing the various aspects of the language through concrete examples. It's comprised of 13 lessons, start with the basics and you'll be building a distributed search engine by the end.

Learn in small bites



I ran across this blog and was impressed by the clear and concise nature of the examples provided. Each well-commented "bite" demonstrates a different facet of Scala.

References 


The Scala API


http://www.scala-lang.org/api/current/index.html#package
When in doubt, go to the source. Useful for understanding and discovering methods of common classes.

Effective Scala



The unofficial "best practices" guide for Scala. Definitely worth a look once you've gone over the basics.


Wish me luck as I learn! What has your experience with Scala been? What resources do you rely on for reference and learning?