Showing posts with label functional-programming. Show all posts
Showing posts with label functional-programming. Show all posts

Friday, June 4, 2010

Formatting Console Output in Scala

So it's midnight and I'm programming Scala (heh).  I'm actually starting to be able to write little scripts in Scala.  Here's a quick one that I put together, inspired by Chapter 3 of "Programming Scala" which reads a source code file and formats the file to the screen with line numbers.



I tried to be as functional as possible.  The "formatSourceLine" I felt was getting close to the concept, using recursion, immutable values, and always returning a value (avoiding the side effect of printing from within the function).

The actual output from this script is:

D:\Development\Scala>scala file.scala file.scala

    1 | import scala.io.Source
    2 |
    3 | if (args.length > 0) {
    4 |
    5 |     def formatSourcePrefix(line: Int, suffix: String, max: Int = 5):
    6 |         var prefix: String = ""
    7 |         for (i <- 1 to (max - line.toString.length))
    8 |             prefix += " "
    9 |         prefix + line.toString + suffix
   10 |     }
   11 |
   12 |     def formatSourceLine(lines: List[String], line: Int = 1, 
   13 |         val out = output + formatSourcePrefix(line, " | ")
   14 |         if (lines.length > 1)
   15 |             formatSourceLine(lines.tail, line + 1, out)
   16 |         else
   17 |             out
   18 |     }
   19 |
   20 |     // Read file from disk
   21 |     val lines = Source.fromPath(args(0)).getLines().toList
   22 |
   23 |     // Print results to screen
   24 |     println
   25 |     print(formatSourceLine(lines))
   26 |
   27 | }
   28 | else {
   29 |     // Print out proper syntax
   30 |     Console.err.println
   31 |     Console.err.println("scala file.scala [filename]")
   32 | }

Wednesday, May 26, 2010

Language of the Summer: Scala?

I've been quite fascinated with Haskell as a language.  It's purely functional, ridiculously strict typing, and algebraic syntax were completely foreign to me before I began learning about it.  I wouldn't say that I learned Haskell, but I did learn about it.  I learned enough to know that it's a bit over my head at this point and that I need a better theoretical foundation before I attempt to tackle it again.

So in the mean time I wanted to take on something more transitional.  I had considered F#.  It seems like a great choice, especially since I already know C# and am familiar with the .NET platform.  But it's a Microsoft language and I was hoping for something more portable (actually, I'm harboring a secret desire to use a functional programming language to one day code for Lego Mindstorms NXT with).

I happened to stumble upon Lift again yesterday.  I had heard about it before when I was looking at other web frameworks after watching a keynote from the author of Seaside at Djangocon.

However, I realized something important about Lift and Scala which I had never caught before: Scala compiles to java bytecode.  Java can be run on Google App Engine.  I can use Scala/Lift to write for App Engine.

Suddenly I'm hooked.  Oh, awesome, and the book "Programming Scala" is available freely online.  Mmmm... looks like I have my reading for my traveling this summer.

  © Blogger template 'Minimalist G' by Ourblogtemplates.com 2008

Back to TOP