The Artima Developer Community
Sponsored Link

Articles Forum
First Steps to Scala

32 replies on 3 pages. Most recent reply: May 6, 2010 4:54 PM by Venki Balakrishnan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 32 replies on 3 pages [ « | 1 2 3 | » ]
Bill Venners

Posts: 2284
Nickname: bv
Registered: Jan, 2002

Re: First Steps to Scala Posted: May 17, 2007 5:32 PM
Reply to this message Reply
Advertisement
> One advantage of the -> method is that brackets are not
> required (unlike the example in the article). This makes
> the syntax a lot more readable: e.g.
>
> val romanNumeral = Map(1 -> "I", 2 -> "II", 3 -> "III",
> ", 4 -> "IV", 5 -> "V")
>


You're right, and I updated the article. I had originally had that form, but due to a misunderstanding with Martin added in the parens. Where the parens are needed is this form:


import scala.collection.mutable.HashMap

val romanNumeral = new HashMap[Int, String] + (1 -> "I") + (2 -> "II") + (3 -> "III") + (4 -> "IV") + (5 -> "V")

Mark Engelberg

Posts: 3
Nickname: puzzler
Registered: Jun, 2006

Re: First Steps to Scala Posted: May 26, 2007 11:19 PM
Reply to this message Reply
After compiling WorldlyApp, if I type:
scala WorldlyApp
I get an error message that the file is not found.

It only works if I type:
scala -classpath . WorldlyApp

How can I get scala to recognize the file without explicitly typing the classpath each time?

I'm running on Windows XP Pro

Justin Forder

Posts: 9
Nickname: jufo
Registered: May, 2007

Re: First Steps to Scala Posted: May 28, 2007 11:08 AM
Reply to this message Reply
I found the article really useful, and timely.

While the examples based on printing program arguments clearly need to be run as scripts, everything else can be run interactively in the interpreter. This takes less typing (no need for print statements), and gives more immediate feedback (for example all the snippets demonstrating different ways of using traits can be tried one at a time).

For execution in the interpreter, the WorldlyGreeter example needs to be rearranged, with the object definition coming before the class definition (which refers to it). (Am I alone in finding this example a bit contrived?)

The only problem I had with following along in the interpreter was its lack of line-editing and history - but I just found another Scala tutorial here:

http://blog.circleshare.com/index.php?/archives/41-Scala-Idioms,-Step-1,-Lists-and-Maps.html

recommending the use of rlwrap to wrap the invocation of the interpreter, to provide these functions.

http://freshmeat.net/projects/rlwrap/

I haven't tried that yet - I'm just building it (via DarwinPorts), then I'll alias iscala to 'rlwrap scala', and then I should be happy.

Thaks again for the article - I'm looking forward to the rest of the series.

Jean-Daniel Nicolet

Posts: 13
Nickname: jdnicolet
Registered: Oct, 2003

Re: First Steps to Scala Posted: May 30, 2007 7:45 AM
Reply to this message Reply
Thank you for the great introduction.

It looks like with traits, run-time substitution and functional programming, Scala has all that is needed to offer a STL-like library (STL for Standard Template Library in C++, one of the first attempt to deal with the functional paradigm, container and algorithms). Is there an underway attempt to build such a library ?

Eirik Maus

Posts: 15
Nickname: eirikma
Registered: Oct, 2005

Re: First Steps to Scala Posted: Jun 20, 2007 12:49 AM
Reply to this message Reply
Great article. When do we get the guide for the next step?

Daniel Dekany

Posts: 1
Nickname: ddekany
Registered: Jun, 2007

Re: First Steps to Scala Posted: Jun 21, 2007 5:14 PM
Reply to this message Reply
"If an anonymous function consists of one method application that takes a single argument, you need not explicitly name and specify the argument. Thus, the following code also works:
args.foreach(println)"


Well, I know it's scandalous to ask but... is that because of that? Is that an anonymous function there at all, or rather I'm passing the println function itself? Because if I have a function with, for example, two arguments then I can also omit the parameters:

def foo(f: (Int, Int) => Unit) {
f(10, 20)
}

def bar(a: Int, b: Int) {
println(a + " and " + b)
}

foo(bar) // <-- there!


It seems to me pretty consistent with this, that also works:

var f: (Int, Int) => Unit = bar

Eric Schwarzenbach

Posts: 1
Nickname: ericjs
Registered: Aug, 2007

Re: First Steps to Scala Posted: Aug 27, 2007 1:08 PM
Reply to this message Reply
> One advantage of the -> method is that brackets are not
> required (unlike the example in the article). This makes
> the syntax a lot more readable: e.g.
>
> val romanNumeral = Map(1 -> "I", 2 -> "II", 3 -> "III",
> ", 4 -> "IV", 5 -> "V")
>

> versus
>
> val romanNumeral = Map((1, "I"), (2, "II"), (3, "III"),
> ), (4, "IV"), (5, "V"))
>



I have a hard time believing that most people will find the first construction more readable than the second. The parens provide a clear visual grouping of the pairs and the "->" makes for more clutter between the pairs where the lesser visual emphasis of the "," also helps make the grouping of pairs clearer.

Stanislas Klimoff

Posts: 6
Nickname: graven
Registered: May, 2007

Re: First Steps to Scala Posted: Sep 7, 2007 2:36 AM
Reply to this message Reply
Well, lots of people find 2 + 3 * 4 + 5 * 6 + 7 clearer than 2 + (3 * 4) + (5 * 6) + 7. It's just a matter of habit, i think.

Jonathan Feinberg

Posts: 3
Nickname: jdf
Registered: Aug, 2003

Re: First Steps to Scala Posted: Dec 16, 2007 8:50 AM
Reply to this message Reply
This article seems to require a few corrections to be up-to-date with the current release. In particular, Unit expressions (such as println), no longer result in a value being printed out by the interpreter:
scala> println("Schmeggegge")
Schmeggegge

scala>

I was especially stunned, and consider it kind of a bug, to see that vals can be reassigned:
scala> val foo = "Hello, world!"
foo: java.lang.String = Hello, world!

scala> val foo = 12
foo: Int = 12

scala>

Morgan Conrad

Posts: 307
Nickname: miata71
Registered: Mar, 2006

Re: First Steps to Scala Posted: Dec 30, 2007 8:47 AM
Reply to this message Reply
I am a long time C / Java programmer. Never used any functional languages, (well, I did use LISP very briefly many many years ago) most of this is new to me. Perhaps this ignorance shows in my (admittedly somewhat cynical) comments below.

Overall, I love the fact that Scala runs in the JVM and I can import familiar Java classes. (And, I assume, 3rd party jars such as Apache) The parts work, I can use Eclipse, I'll almost certainly give Scala a try. But I have a lot of quibbles with the syntax. IMO, they got too creative / cutesie.

You define an object as Singleton by calling it, DOH, object. Why not use the term "singleton"!

If :: is pronounced "cons", why not type it as cons? Geez, its all of 2 extra characters. And it's clearer - I see "cons", I wikipedia LISP, and I get a lot of info on what it means, plus links to cdr, etc... And it's less likely to get confused with :::. A common programming bug in confusing = and ==, why design in :: and ::: which are visually even closer?

treasureMap += 1 -> "Go to island.";

for adding to a map is truly the most disgusting syntax ever. This is almost enough to make me writeoff the language. BTW, this syntax is longer than Java's

treasureMap.put(1,"Go to island.");

I'm extremely leery of syntactically important white space which seems common in Scala (if I'm understanding some of these alternative syntaxes right).

"Sometimes the Scala compiler will require you to specify the result type of a method"

I saw something like this in another article about variables. leave off declarations till it complains and you learn better. So you "save" typing and are ultra efficient by leaving off "int" or "void", but then you waste time when the compiler complains. Call me a sceptic.

BTW, they should use void for void, not "unit". What the heck is unit?


Finally, about all the functional parts. Will somebody please please post a useful example other than printing an array of strings? When the key benefit of FP that 1000s of posts of Artima are demanding be added to Java is the ability to go


args.foreach((arg: String) => println(arg))


instead of


for (String arg : args)
System.out.println(arg);


I am, well, completely underwhelmed.

Ryan Ingram

Posts: 2
Nickname: ryani
Registered: Jan, 2008

Re: First Steps to Scala Posted: Jan 3, 2008 4:26 PM
Reply to this message Reply
> Finally, about all the functional parts. Will somebody
> please please post a useful example other than printing an
> array of strings?

Here's a quick&dirty tool that I needed last year:
- Read text from standard input until end of file, and output a histogram showing how often each line occurred in the file, in order from most common to least.

Here's the first few lines of the output from my sample run:
("PIIIIIP",139627)
("CFPICFP",32482)
("PCCCCCP",28267)
("PFFFFFP",14319)
("PFFICCP",13534)
("CCICIIC",12192)

Go ahead & write this in Java to start out.

Here's my Haskell implementation:
module Main where
import qualified Data.Map as M
import Data.List (foldl', sortBy)

main = interact process

process :: String -> String
process = unlines . map show . organize . unpackHisto . buildHisto . lines

buildHisto :: [String] -> M.Map String Int
buildHisto = foldl' insertHisto M.empty

insertHisto :: M.Map String Int -> String -> M.Map String Int
insertHisto m s = M.insertWith (+) s 1 m

unpackHisto = M.toList

organize = sortBy s where s x y = compare (snd y) (snd x)

A couple of comments about this:
1) Sorry about the formatting; the forum seems to be eating the double-newlines in my code.

2) I'm not golfing on this at all; this is the first code I wrote, it did what I wanted, and I was done with it.

3) This is really just the tip of the iceberg; this is just a simple "pipeline" example. There's a lot more you can do; the functional style gives you the ability to factor out common functionality everywhere; if there's a certain type of for loop you write a lot, you can factor it out into a function.

4) "interact" is just a regular function; in Scala it would have the type (String => String) => Unit. It takes a regular function that does string processing and applies it to stdin, writing the output to stdout. You could reasonably easily write "perlInteract" of the same type which read from files specified on the command line instead, with special treatment of "-" as stdin, but you wouldn't have to change the definition of "process" at all.

Morgan Conrad

Posts: 307
Nickname: miata71
Registered: Mar, 2006

Re: First Steps to Scala Posted: Jan 3, 2008 9:42 PM
Reply to this message Reply
Here's a java version of line count. If I really wanted to I could save a few lines by converting the if else into a gross ? construct.


public class Histo {

static Comparator<Map.Entry<Comparable, Comparable>> VALUE_COMPARATOR = new Comparator<Map.Entry<Comparable, Comparable>>() {
public int compare(Map.Entry<Comparable, Comparable> o1, Map.Entry<Comparable, Comparable> o2) {
int result = o2.getValue().compareTo(o1.getValue());
return (result != 0) ? result : o2.getKey().compareTo(o1.getKey());
}
};

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
HashMap<String, Integer> map = new HashMap();
for (String s = br.readLine(); s != null; s=br.readLine()) {
if (map.containsKey(s))
map.put(s, new Integer(map.get(s) + 1));
else
map.put(s, 1);
}

ArrayList list = new ArrayList(map.entrySet());
Collections.sort(list, VALUE_COMPARATOR);
System.out.println(list.toString());
}
}

Ryan Ingram

Posts: 2
Nickname: ryani
Registered: Jan, 2008

Re: First Steps to Scala Posted: Jan 4, 2008 12:51 AM
Reply to this message Reply
Pretty good; my experience with Java has been that it's always really verbose, but this is reasonably short.

My main comment revolves around this line:
> Collections.sort(list, VALUE_COMPARATOR);

VALUE_COMPARATOR is defined a long way away in the code, and it's 6 lines of code, the first of which is 125 characters long. Compare (pun not intended) to

> sortBy (\o1 o2 -> compare (snd o1) (snd o2)) list
or, in a "OO-functional hybrid" language, something like:
> list.sortBy ((o1,o2) => o1.value.compareTo(o2.value))

I think you'll agree that this is definitely more useful than "printing everything in a list" and demonstrates very well the power & succinctness of the style.

An OO way to think about this is that closures/anonymous functions/whatever are just really lightweight classes with only one method (apply) and lots of nice syntax; you're just declaring a specialized class inline that automatically captures relevant local state into member variables.

Morgan Conrad

Posts: 307
Nickname: miata71
Registered: Mar, 2006

Re: First Steps to Scala Posted: Jan 5, 2008 2:20 PM
Reply to this message Reply
> Pretty good; my experience with Java has been that it's
> always really verbose, but this is reasonably short.

Thanks. Java is more verbose, but only partly due to Java. IMO much of it is due to poor coding at the tactical or strategic level.

>
> My main comment revolves around this line:
> > Collections.sort(list, VALUE_COMPARATOR);
>
> VALUE_COMPARATOR is defined a long way away in the code,
> and it's 6 lines of code, the first of which is 125
> characters long.

Agreed. Partly the Generics syntax.

Java Comparators are certainly more verbose than in your example Haskell / Scala code. But one nice feature of them being more "real classes" is that one can chain them together, you can use a Factory pattern or similar to change things around (e.g., comparing European names vs. Chinese where the order switches) etc.

The advantage of defining the VALUE_COMPARATOR several lines away in the code is that it is really a general purpose Comparator than much other code can now use or extend. Closures / anonymous classes are neither reusable nor extensible.

I think the extra power is usually worth the extra work, but YMMV.

Chris McIntosh

Posts: 4
Nickname: machershel
Registered: Feb, 2008

Re: First Steps to Scala Posted: Feb 10, 2008 11:52 PM
Reply to this message Reply
Here's my thought on the article:

class HungryDeveloper extends Friendly {
override def greet() = "Weow! I need more Scalazine"
}

Integrate this with Step 12 exercises and you'll know what I'm asking.... When do we get some more good stuff???! LOL

Thanks for the article!

Chris

Flat View: This topic has 32 replies on 3 pages [ « | 1  2  3 | » ]
Topic: Developer Productivity in Flash Builder 4 Previous Topic   Next Topic Topic: Never Call Virtual Functions during Construction or Destruction

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use