if ( document.comments_form.url ) { document.comments_form.url.value = getCookie("mtcmthome"); }
Update: I posted a wrap-up here.
Here is an interesting coding challenge: write a counter function that counts from 1 to max but only returns numbers whose digits don't repeat.
For example, part of the output would be:
Post your solution in the comments or on your blog. All languages and all approaches (brute force or not) are welcome.
I'm curious to see if somebody can come up with a clever solution (I don't have one)...
A long time ago, Bertrand Meyer, the creator of Eiffel, made a very strong statement against the use of switch and case. He argued that these constructs were not object-oriented and made future maintenance difficult. And to make his point crystal clear, he decided that Eiffel would not support these statements.
This decision didn't go well with Eiffel users and Meyer eventually gave in and added switch> and case to his language.
Meyer's initial position was certainly extreme but I think the foundation of his argument still stands, and I was reminded of this age old debate just today while attending Martin Odersky's Jazoon keynote, and more specifically, when he described Scala's "case classes".
Ever since I first read about case classes, I have been confused about their utility, and my puzzlement has not abated. Either I am missing something big or this feature is something that has been vastly overhyped and that should be avoided as much as possible
Let's see why.
Here is the example used in the official Scala documentation:
def printTerm(term: Term) {
term match {
case Var(n) =>
print(n)
case Fun(x, b) =>
print("^" + x + ".")
printTerm(b)
case App(f, v) =>
Console.print("(")
printTerm(f)
print(" ")
printTerm(v)
print(")")
}
}
This code is part of a hypothetical parser: it does different things depending on whether we just encountered a variable, a function or an application.
I have two strong objections to this kind of approach:
The second claim is a bit more subtle but is actually the most important.
Obviously, this parser is quite incomplete: what happens if I want support for import clauses?
First, I am probably going to create at least one new class Import and second, I need to remember to add it to this code. Worse: all this code does is print out the type of Term it just parsed. Obviously, we will be needing more logic to actually do the work, such as a function to verify that the Term is syntactically correct (say verify(), and also a function to generate the code or the abstract information related to that term (generateCode()). Each of these methods will also feature a match structure, which will need to be updated for each new class.
In summary: every time that I add a new class, I need to remember to update three functions located somewhere in my code base.
Now we can see why Meyer dislikes switch statements so much...
The preferred way to solve these two problems is to make sure that each class encapsulates its own logic for all these operations:
interface Term {
void print();
void verify();
void generateCode();
}
public Var implements Term {
public void print() {
print(n)
}
public void verify() { ... }
public void generateCode() { ... }
}
// same for Fun and Expr
Here is how we print a term with case classes (from the Scala documentation, again):
Term t = ...; printTerm(t)And with proper encapsulation:
Term t = ...; t.print();With the OO approach, the code shown at the very top is simply no longer needed! If you need to print the content of a Term, you just invoke print() on it. If you add a new class to your parser, all you need to implement is conveniently summarized in the Term interface: no need to go hunting through your entire code base for switch statements. Not only do we have clean encapsulation, but we also have locality.
When I exposed the crux of this argument to a few Scala experts, they overall agreed about the point and responded by pointing out that case classes were more useful as an alternative to the Visitor pattern.
Before turning our attention to this specific case, it's important to note that at this point, we are now looking at solving a niche problem. And quite a rare one, in my experience. If this is really the reason why case classes were invented, I am really left scratching my head about the decision to include such a big feature inside a language to solve such a small class of problems.
Anyway, let's take a look at the Visitor angle.
In a nutshell, Visitors are used to emulate multiple dispatch in languages that don't support it natively. In some way, you are extending virtual invocation to apply to the runtime type of parameters passed to your functions.
I'm not going into a deep discussion of the pros and cons of this approach, but I'll just observe that even in the few cases where I have had such a need, an approach similar to the encapsulation explained above usually turns out to be preferable to a switch statement. In other words, if you need to have different operations depending on whether your function is being invoked with parameters of types A or B is better implemented by hiding this particular detail inside the class handling this dispatch, and not in a global switch.
From my understanding of Scala's history, case classes were added in an attempt to support pattern matching, but after thinking about the consequences of the points I just gave, it's hard for me to see case classes as anything but a failure. Not only do they fail to capture the powerful pattern matching mechanisms that Prolog and Haskell have made popular, but they are actually a step backward from an OO standpoint, something that I know Martin feels very strongly about and that is a full part of Scala's mission statement.
To be fair, Scala navigates in very rough waters: finding a good compromise between running seamlessly on an existing OO platform and inventing a language that will not only accomodate fundamental functional programming paradigms but also leave the door open for advanced dynamic concepts such as open classes is no easy feat, and overall, Scala has done a very good job at finding the right balance. But unfortunately, not with case classes.
Or am I missing something?
Here is the description:
A lot of new languages have emerged to challenge Java these past years: Ruby, Scala, Groovy, Erlang, etc... Can Java survive? In this presentation, I will do a quick analysis of these various languages and explain why, despite its old age, Java is in the best position to face the challenges that await software developers for the next decade. I will cover topics such as dynamic and duck typing, functional programming, concurrency, distribution and much more.As you can see, the topic is very broad and the main challenge will be to keep the presentation within the allotted time. Hopefully, we'll be able to carry on the discussion over beers or dinner.
See you all in Zurich.
I just came across a language that I never heard of before: Fan.
Here is a quick snippet:
// find files less than one day old
files := dir.list.findAll |File f->Bool|
{
return DateTime.now - f.modified < 1day
}
// print the filenames to stdout
files.each |File f|
{
echo("$f.modified.toLocale: $f.name")
}
After a few hours surveying the language, I have to say that Fan seems to get a lot of things right. Here is a short list of its features:
Java and .NET to a lesser degree separate the concepts of namespace and deployment. For example in Java packages are used to organize code into a namespace, but JAR files are used to organize code for deployment. The problem is there isn't any correspondence between these concepts. This only exacerbates classpath hell - you have a missing class, but the class name doesn't give you a clue as to what JAR file the class might live in.
Constructor
class Point
{
new make(Int x, Int y) { this.x = x; this.y = y; }
Int x
Int y
}
// make a point
pt := Point.make(30, 40)
Properties
class Person
{
Str name
Int age { set { checkAge(val); @age = val } }
}
In the code above, @age is used to refer to the actual field and not the property.
Facet
@version=Version("1.2")
@todo=["fix it", "really fix it"]
class Account
{
}
You can pass methods when closures are expected. For example, Thread.make expects a closure that takes a Thread in parameters and returns an Obj:
new make(Str name := null, |Thread -> Obj| run := null)You don't need to create an object to invoke that method:
static Void readerRun(Thread t) {
// ...
}
reader := Thread.make("reader", &readerRun).start
Here are some of my pet peeves about Fan:
Our philosophy is that generics are a pretty complicated solution to a fairly insignificant problem.I'm sure they will change their mind in due time, and in the meantime, List, Map and Func offer a limited type of genericity.
// Valid Fan
class Point {
new make(Int x, Int y) { this.x = x; this.y = y; }
Int x
Int y
}
p = Point.make(2, 3)
Why not:
// Invalid Fan
class Point {
new(Int x, Int y) { this.x = x; this.y = y; }
Int x
Int y
}
p = Point.new(2, 3)
? (the same remark can be made about Ruby)
Overall, it looks like Fan is being driven by motivations that are very similar to what started Groovy: pick the best features of the current popular languages and try to blend them into a coherent set. I find myself particularly fond of Fan because I happen to agree with a lot of the choices that the designers made, which is exactly how I felt about Groovy in the beginning. Of course, Fan has the advantage of hindsight and it borrows from a few additional languages than Groovy did (namely, C#, Scala and a bit of Erlang), so I find the result quite promising.
It's a very impressive demo, but as opposed to the author of the article, I really don't think that much more can be done with this kind of technology. People are quick to point out that we could write fantastic games or develop great applications with this technology, but the fact that you need to keep the screen visible (and at a reasonable angle) as you tilt the phone severely limits the range of what you can actually do.
Recently, I have also started encountering limitations in what you can do with touches on a phone screen. Touch interaction allows for great-looking devices that are not encumbered by ugly-looking keyboards, but it also takes a toll on user interaction in two ways:
There is no doubt that touch and accelerometers are here to stay, but in my opinion, the most revolutionary applications that we are likely to see in phones in these coming years will come from features that don't require any user interaction, such as camera, compass and GPS.