This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Scala Puzzlers?

13 replies
Andrew Phillips
Joined: 2009-09-10,
User offline. Last seen 42 years 45 weeks ago.

Would there be any interest in creating a collection of Scala Puzzlers
- similar in concept to Josh and Neal's Java Puzzlers book?

There already appear to be a scattering of examples in various blog
posts (cf. http://www.google.com/search?hl=en&q=scala+puzzlers), but I
feel it would be useful (and fun!) to sort, generalise and discuss
them. As well as being a good reference for more advanced or esoteric
topics, where quirks are not uncommon.

Perhaps it could even become a useful source of comment and debate for
language improvements..?

Regards

Andrew Phillips

Kevin Wright
Joined: 2009-06-09,
User offline. Last seen 49 weeks 3 days ago.
Re: Scala Puzzlers?
+1Bring em on!

On Thu, Sep 10, 2009 at 1:09 PM, Andrew Phillips <demobox1 [at] yahoo [dot] co [dot] uk> wrote:
Would there be any interest in creating a collection of Scala Puzzlers
- similar in concept to Josh and Neal's Java Puzzlers book?

There already appear to be a scattering of examples in various blog
posts (cf. http://www.google.com/search?hl=en&q=scala+puzzlers), but I
feel it would be useful (and fun!) to sort, generalise and discuss
them. As well as being a good reference for more advanced or esoteric
topics, where quirks are not uncommon.

Perhaps it could even become a useful source of comment and debate for
language improvements..?

Regards


Andrew Phillips




extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: Scala Puzzlers?

On Thu, Sep 10, 2009 at 12:09:34PM +0000, Andrew Phillips wrote:
> Would there be any interest in creating a collection of Scala Puzzlers
> - similar in concept to Josh and Neal's Java Puzzlers book?

They don't make enough paper.

I have been meaning to organize my own little collection. Here are a
couple.

1) Why does this compile, and what does it print if you run it?

object nutty
{
def main(args: Array[String]): Unit = {
val x = "one" // \u000a + "two" /*
println(x)
val y = x // */
val y = x * 2
println(y)
}
}

2) Here is the second line from a repl session. What was the first?
(Multiple possible answers, shortest wins.)

scala> b b b b b b b b b b
res0: b.type = b

Andrew Phillips
Joined: 2009-09-10,
User offline. Last seen 42 years 45 weeks ago.
Re: Scala Puzzlers?

> They don't make enough paper.

Most likely - not yet, certainly. Hence the idea of collecting them in a wiki-like environment until we have enough to pulp a few more trees. I guess you're not the only one with his own little collection of quirks and oddities, and if we can bring them all together who knows how many will turn up?

The key thing would be of course be to try to make this "the" place to have your particular puzzler published - if we use scalapuzzlers.com (I could try to get a basic wiki set up there soon) at least we'd have a fairly memorable domain...

ap

PS: Still working on the second one. Feeling a bit guilty, I'm supposed to be working ;-))

nilskp
Joined: 2009-01-30,
User offline. Last seen 1 year 27 weeks ago.
Re: Scala Puzzlers?
On Thu, Sep 10, 2009 at 8:15 AM, Paul Phillips <paulp [at] improving [dot] org> wrote:
They don't make enough paper.

Just wait :-)

With implicits and all the other Scala goodness, it has the potential for many more puzzlers than Java.
Colin Bullock
Joined: 2009-01-23,
User offline. Last seen 42 years 45 weeks ago.
Re: Scala Puzzlers?


2) Here is the second line from a repl session.  What was the first?
(Multiple possible answers, shortest wins.)

scala> b b b b b b b b b b
res0: b.type = b


scala> case object b { def apply(x: this.type) = x; val b = this }

- Colin
dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: Scala Puzzlers?
The first one is pretty obvious, though it does annoy me.   Actually, the following code is pretty common Scala code, and yet it relies on a little known feature, and it is certainly a puzzler if people stop to think about it:   List(1,2) match {   case 1 :: 2 :: Nil => println("Yep")   case _ => println("Nope") }
Why does "::" work in that context? How do you replicate such behavior with a class of your own?
On Thu, Sep 10, 2009 at 10:15 AM, Paul Phillips <paulp [at] improving [dot] org> wrote:
On Thu, Sep 10, 2009 at 12:09:34PM +0000, Andrew Phillips wrote:
> Would there be any interest in creating a collection of Scala Puzzlers
> - similar in concept to Josh and Neal's Java Puzzlers book?

They don't make enough paper.

I have been meaning to organize my own little collection.  Here are a
couple.

1) Why does this compile, and what does it print if you run it?

object nutty
{
 def main(args: Array[String]): Unit = {
   val x = "one"   // \u000a + "two" /*
   println(x)
   val y = x       // */
   val y = x * 2
   println(y)
 }
}

2) Here is the second line from a repl session.  What was the first?
(Multiple possible answers, shortest wins.)

scala> b b b b b b b b b b
res0: b.type = b

--
Paul Phillips      | A national political campaign is better than the
Moral Alien        | best circus ever heard of, with a mass baptism and
Empiricist         | a couple of hangings thrown in.
i pull his palp!   |     -- H. L. Mencken



--
Daniel C. Sobral

Something I learned in academia: there are three kinds of academic reviews: review by name, review by reference and review by value.
Kevin Wright
Joined: 2009-06-09,
User offline. Last seen 49 weeks 3 days ago.
Re: Scala Puzzlers?

On Thu, Sep 10, 2009 at 4:03 PM, Daniel Sobral <dcsobral [at] gmail [dot] com> wrote:
The first one is pretty obvious, though it does annoy me.   Actually, the following code is pretty common Scala code, and yet it relies on a little known feature, and it is certainly a puzzler if people stop to think about it:   List(1,2) match {   case 1 :: 2 :: Nil => println("Yep")   case _ => println("Nope") }

You'd then have to cross-reference several chapters on extractors, sounds fun! :) 
Adriaan Moors
Joined: 2009-04-03,
User offline. Last seen 42 years 45 weeks ago.
Re: Scala Puzzlers?
2) Here is the second line from a repl session.  What was the first?
(Multiple possible answers, shortest wins.)

scala> b b b b b b b b b b
res0: b.type = b


scala> case object b { def apply(x: this.type) = x; val b = this }

- Colin 
you can still strip some whitespace and "type safety":
case object b{def apply(x:Any)=b;val b=this}

just my 44 characters,adriaan
David Pollak
Joined: 2008-12-16,
User offline. Last seen 42 years 45 weeks ago.
Re: Scala Puzzlers?
Sounds wicked cool!!

On Thu, Sep 10, 2009 at 5:09 AM, Andrew Phillips <demobox1 [at] yahoo [dot] co [dot] uk> wrote:
Would there be any interest in creating a collection of Scala Puzzlers
- similar in concept to Josh and Neal's Java Puzzlers book?

There already appear to be a scattering of examples in various blog
posts (cf. http://www.google.com/search?hl=en&q=scala+puzzlers), but I
feel it would be useful (and fun!) to sort, generalise and discuss
them. As well as being a good reference for more advanced or esoteric
topics, where quirks are not uncommon.

Perhaps it could even become a useful source of comment and debate for
language improvements..?

Regards


Andrew Phillips






--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Git some: http://github.com/dpp
Dean Wampler
Joined: 2008-12-26,
User offline. Last seen 42 years 45 weeks ago.
Re: Scala Puzzlers?


On Thu, Sep 10, 2009 at 10:08 AM, Kevin Wright <kev [dot] lee [dot] wright [at] googlemail [dot] com> wrote:

On Thu, Sep 10, 2009 at 4:03 PM, Daniel Sobral <dcsobral [at] gmail [dot] com> wrote:
The first one is pretty obvious, though it does annoy me.   Actually, the following code is pretty common Scala code, and yet it relies on a little known feature, and it is certainly a puzzler if people stop to think about it:   List(1,2) match {   case 1 :: 2 :: Nil => println("Yep")   case _ => println("Nope") }

You'd then have to cross-reference several chapters on extractors, sounds fun! :) 

I wondered how this worked, too, so we describe it in "Programming Scala", http://programming-scala.labs.oreilly.com/ch06.html#CaseClasses. Search for "Syntactic Sugar for Binary Operations".

--
Dean Wampler
coauthor of "Programming Scala" (O'Reilly)
-  http://programmingscala.com
twitter: @deanwampler, @chicagoscala
Chicago-Area Scala Enthusiasts (CASE):
-  http://groups.google.com/group/chicagoscala

dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: Scala Puzzlers?
There, you just gave me a reason to upvote your book. :-)

On Thu, Sep 10, 2009 at 12:59 PM, Dean Wampler <deanwampler [at] gmail [dot] com> wrote:


On Thu, Sep 10, 2009 at 10:08 AM, Kevin Wright <kev [dot] lee [dot] wright [at] googlemail [dot] com> wrote:

On Thu, Sep 10, 2009 at 4:03 PM, Daniel Sobral <dcsobral [at] gmail [dot] com> wrote:
The first one is pretty obvious, though it does annoy me.   Actually, the following code is pretty common Scala code, and yet it relies on a little known feature, and it is certainly a puzzler if people stop to think about it:   List(1,2) match {   case 1 :: 2 :: Nil => println("Yep")   case _ => println("Nope") }

You'd then have to cross-reference several chapters on extractors, sounds fun! :)  

I wondered how this worked, too, so we describe it in "Programming Scala", http://programming-scala.labs.oreilly.com/ch06.html#CaseClasses. Search for "Syntactic Sugar for Binary Operations".

--
Dean Wampler
coauthor of "Programming Scala" (O'Reilly)
-  http://programmingscala.com
twitter: @deanwampler, @chicagoscala
Chicago-Area Scala Enthusiasts (CASE):
-  http://groups.google.com/group/chicagoscala




--
Daniel C. Sobral

Something I learned in academia: there are three kinds of academic reviews: review by name, review by reference and review by value.
dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: Scala Puzzlers?

scala> class Test
defined class Test

scala>new Test().isInstanceOf[AnyVal]
res:9: Boolean = true.   Why is it true? Can you think of other consequences resulting from the same root cause?

On Thu, Sep 10, 2009 at 9:09 AM, Andrew Phillips <demobox1 [at] yahoo [dot] co [dot] uk> wrote:
Would there be any interest in creating a collection of Scala Puzzlers
- similar in concept to Josh and Neal's Java Puzzlers book?

There already appear to be a scattering of examples in various blog
posts (cf. http://www.google.com/search?hl=en&q=scala+puzzlers), but I
feel it would be useful (and fun!) to sort, generalise and discuss
them. As well as being a good reference for more advanced or esoteric
topics, where quirks are not uncommon.

Perhaps it could even become a useful source of comment and debate for
language improvements..?

Regards


Andrew Phillips






--
Daniel C. Sobral

Something I learned in academia: there are three kinds of academic reviews: review by name, review by reference and review by value.
odersky
Joined: 2008-07-29,
User offline. Last seen 45 weeks 6 days ago.
Re: Scala Puzzlers?

On Fri, Sep 11, 2009 at 3:43 PM, Daniel Sobral wrote:
> scala> class Test
> defined class Test
>
> scala>new Test().isInstanceOf[AnyVal]
> res:9: Boolean = true.
>
> Why is it true? Can you think of other consequences resulting from the same
> root cause?
>
That's going to be a short-lived puzzle. AnyVal will be disallowed as
an argument for isInstanceOf tests from Scala 2.8.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland