aspose file tools*
The moose likes Scala and the fly likes Anyone using Scala? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Languages » Scala
Bookmark "Anyone using Scala?" Watch "Anyone using Scala?" New topic
Author

Anyone using Scala?

Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
I have been reading through the tutorials and finally downloaded Scala last night. It looks pretty cool to me. Most of the functional stuff of a language like Haskell or OCaml, tail call elimination, higher order functions, semantics for easily encoding algebraic data types. The syntax isn't as clean as Haskell, but there's less boilerplate than Java. Plus it compiles to JVM bytecode so it can interact seamlessly with Java classes, it has the same "compile once, run everywhere" flavor of Java, and it's comparable to Java in terms of speed. But I haven't done anything serious with it yet so I might just like the spec. So does anyone have any experiences with it whether good, bad, or indifferent?


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
OK from the deafening silence I'll assume the answer is no. So I'll respond a little about what I've found. The reason I was drawn to Scala was for its support of a functional style of programming. I've read other places that most people who use Scala use it in an Object Oriented style, but I feel like you could just use Java for that. So I figured I'd play around a little bit by solving the first few problems in Project Euler.

Here's the Scala version of the answer to Problem 2



It could probably have been done in a one-liner but whats the fun in that?

Anyway after I got done with that I figured I would write a small Mastermind that had a small GUI component. Since I was dong something that would require more than 10 lines of code I figured I needed to write some tests. So I thought this was as good a time as any for me to get used to writing tests in SUnit, JUnit style unit testng tool that ships with the Scala development kit. I was severely disappointed. It was poorly documented and nearly unusable. By default, if an assertion fails it prints a message to the console and the entire stack trace of the method call up to that point, usually about 50 lines of library calls that can safely be ignored, but must be sifted through to find the message provided by the next failing test. When I say its badly documented, this is what I mean. Here is the entire documentation of the SUnit object, the object that contains all the testing classes:
That seems like a pretty fragile way of running tests. Seriously. I guess I could use JUnit to test the Scala code, but since I'm stubborn, I think I'll try to patch together something to make SUnit usable. I'm still going to finish the Mastermind project. I'll post back if I find out anything else useful.
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
I'm gonna go buy a copy of Mastermind. For real, right now. I brought up the Scala root page, then moved on. I recognize this type of work ( as your experience reveals ) by how the page runs on page-load. I like to look at the code they produce because it is often a masterwork by accomplished masters of the craft. I want to implement the board game with an AI engine behind it. Java can run ai sufficiently well and provides a prototyping tools that can be fast-path (ed) where need be with it's call native construct.



"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
Gabriel Claramunt
Ranch Hand

Joined: May 26, 2007
Posts: 375

I've been playing with Scala too and really liked it.
As an exercise, I've implemented Dijkstra´┐Żs shortest path (Dsp) algorithm and blogged about it.
For better testing maybe you can consider ScalaTest and ScalaCheck
Although philosophically, OO and FP are incompatible, I think the pragmatic approach of Scala hits a sweet spot between both paradigms and can lead to very interesting results...
[ April 29, 2008: Message edited by: Gabriel Claramunt ]

Gabriel
Software Surgeon
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Thanks for the link Gabriel. Looks like you've explored the language a little deeper than I have thus far. I really like Scala, not as much as Haskell but close. I hadn't even read about explicit self-typing until I looked at your blog. Mark that down as something else I have to learn. I knew about ScalaCheck, but this is the first I've heard about ScalaTest. ScalaTest looks like it might be exactly what I'm looking for. What I've liked the most about Scala in the little bit I've looked at it is how it emphasizes short concise interfaces that are easily and painlessly reused. Since the documentation is so bad for many portions of the API, I find myself reading through a lot of the source code to figure out how to use certain features. It seems to me that a lot of the API that could be written in a more functional style (no mutable state, using recursion instead of looping as a control structure) is instead written in a much more imperative style with a lot of mutable variables and explicit while loops. I'm not sure if this was done for performance reasons or if the authors were just more comfortable with that style. Anyway, thanks for the link to your blog. I'll keep the interested updated on how the Mastermind game is going.
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
So I've been working on the Mastermind game. I still don't have a GUI, but I have the console version working pretty well. For any interested, here's the code to the main game logic.


As you can see, I've just been experimenting with Scala's abstract class and trait facilities. Scala doesn't have the restriction that every Scala class must go in its own file, so this is all in one file. It seems to be a common Scala idiom (at least in the library classes) to have a class and a "companion object" with the same names. The companion object is a singleton object that mostly carries utility method that are usually analogous to static methods in Java (a little like the relationship between the Collection classes and Collections class). Scala doesn't have static methods.

In all I'd say the code is probably just a little smaller than the comparable Java code. The style is pretty different. I intentionally tried to use as little mutable state as I could. I'm not sure if its clearer or not.
[ May 10, 2008: Message edited by: Garrett Rowe ]
Gabriel Claramunt
Ranch Hand

Joined: May 26, 2007
Posts: 375

Maybe you can do an experiment: remove all the type declarations you can and see what the compiler can infer.
As an example, you can probably replace:

with:

(Nil is the empty list and '::' appends the element at the beginning)
[ May 15, 2008: Message edited by: Gabriel Claramunt ]
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

Gee, I posted nearly this exact question over in meanless drivel.

I haven't used it, yet, but I'm learning a bunch.

Java is getting too ugly for my tastes....
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
PF: Java is .....

Typing probably, one general purpose do-all linguistic a good area for prototyping. I suggest re-examining the decision after designing an overview in Java. I will probably start an attempt to write a Collection manager as soon as I close the window. The JList / JScroll pane has some 'extensions' ( ... ) that I would like to have that are in the direction of simplification. I noticed the typing issue as soon as I looked at the Scala page, you are not alone. Google for Ravi Mohan.

- do you drink?... perhaps this evening would be a good time. ( humor )
[ May 14, 2008: Message edited by: Nicholas Jordan ]
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

Originally posted by Pat Farrell:
Gee, I posted nearly this exact question over in meanless drivel.

I haven't used it, yet, but I'm learning a bunch.

Java is getting too ugly for my tastes....

Unless you're tossing Java over completely, have you investigated Groovy at all? Nice way to leverage what you already know about Java and write a whole lot less of it.


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

Originally posted by Michael Ernest:

Unless you're tossing Java over completely, have you investigated Groovy at all? Nice way to leverage what you already know about Java and write a whole lot less of it.


No, not yet. I've been doing Java so long that I need to look around and see what else is happening.

Groovy has one major downside, its name is way too early 70s-ish.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14428
    
  23

Scala is on my list of things to learn someday.

In December 2007 I attended a talk by Martin Odersky (the inventor of Scala) at JavaPolis. The guys of the JavaPosse (one of the most popular podcasts about Java) seem to be quite interested in Scala too.

Someday, when I have time...


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Gabriel Claramunt
Ranch Hand

Joined: May 26, 2007
Posts: 375

The new eclipse's Scala plugin looks very nice (lot better than the old one).
Syntax highlighting, code completion, and all the nice goodies expected from a full blown IDE. No refactoring though, but one can argue that Scala makes refactoring less necessary (just joking).
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

Originally posted by Jesper Young:

In December 2007 I attended a talk by Martin Odersky


Another talk, I think this one is newer.
Martin on Scala
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Originally posted by Pat Farrell:
Another talk, I think this one is newer.
Martin on Scala

That talk is from the JavaPolis conference in December, 2007.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Anyone using Scala?